我一直在尝试使用 nodejs + fluent-ffmpeg 从一系列图像创建幻灯片,但是它无法正常工作或始终如一。ffmpeg 偶尔会发出“错误:ffmpeg 以代码 1 退出:管道:0:处理输入时发现无效数据”,并且如果创建了最终视频 (mp4),它似乎缺少图像/帧。
过程如下:将图像加载到内存中,使用 lwip 将其转换为相同尺寸,依次写入直通流,该流作为输入提供给 ffmpeg。
相关代码片段:
var lwip = require('lwip');
var ffmpeg = require('fluent-ffmpeg');
var stream = require('stream');
var imagesStream = new stream.PassThrough();
...
image.batch()
.contain(options.video.width, options.video.height, 'lanczos')
.toBuffer(options.frames.format, {quality: 100}, (err, buffer) => {
if (err) {
throw ('error convering image to buffer. ' + err);
}
imagesStream.write(buffer, 'utf8');
resolve();
});
...
ffmpeg(imagesStream)
.inputOptions('-framerate 1/' + options.frames.secsPerImage)
.input(path.join(AUDIO_ROOT, options.audio.track))
.save(path.join(path.join(OUTPUT_FOLDER, `${options.video.output.prefix}${timestamp}.${options.video.output.format}`)))
.size(`${options.video.width}x${options.video.height}`)
.on('start', () => {
console.log('creating the clip now...') …Run Code Online (Sandbox Code Playgroud)