我编写了下面的代码(当视频上传到 s3 时触发)从 s3 获取图像,使用 fleunt-ffmpeg nodejs 模块调整其大小,然后将其上传到 s3 存储桶。
async.waterfall([
function download(next) {
// Download video
s3.getObject({
Bucket: srcBucket,
Key: srcKey
}, next);
},
function resizeBuffer(response, next) {
// Resize
resizeVideo(response, function (err, response, buffer){
if(err){
console.log(err);
return next(err);
}
next(null, response, buffer);
});
},
function uploadBuffer(response, buffer, next) {
// Upload to s3
let resizedImageName = "resized/" + imageName;
s3.putObject({
Bucket: srcBucket,
Key: resizedImageName,
Body: buffer,
ContentType: response.ContentType
}, function(err, data) {
if (err) {
console.log(err, err.stack);
}else{
console.log('image resized----------------------->');
next(null, response, buffer);
}
});
}
], function (err) {
if (err) {
console.log('Unable to resize image.');
} else {
console.log('Successfully resized image.');
}
callback(null, "done--------------->");
});
function resizeVideo(response, callback){
try {
let command = ffmpeg(response.Body);
console.log('1-------------->');
command.size('50%');
console.log('2-------------->');
callback(null, response, command.pipe());
} catch (e) {
console.log('video resize error------------------------->');
console.log(e);
callback('Video resize error')
}
}
此代码适用于图像(imagemagik 而不是 fluent-ffmpeg),但在我使用 ffmpeg 时显示错误:Error: Stream yields empty buffer