如何使用ffmpeg、fluent ffmpeg和express js从视频中获取缩略图并将其作为响应发送



am使用ffmpeg和流畅的ffmpeg从类似的视频中获取缩略图

ffmpeg({
source: `../../uploadedVideo/${filepath}`,
}).takeScreenshots({
filename: "example.jpg",
timemarks: [2, 4, 6, 8],
folder: "../../thumbnail/",
});

但我想做的不是把缩略图保存到文件夹中,而是把它作为api响应,这里是整个代码。

const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;
const ffmpeg = require("fluent-ffmpeg");
ffmpeg.setFfmpegPath(ffmpegPath);
router.post("/upload", (req, res) => {
const file = req.files.video;
const filepath = `${uuid()}${path.extname(file.name)}`;

// imagine the file is saved to the server at this point

ffmpeg({
source: `../../uploadedVideo/${filepath}`,
}).takeScreenshots({
filename: "example.jpg",
timemarks: [2, 4, 6, 8],
folder: "../../thumbnail/",
});
res.send({ msg: "success", thumbnail });
});

如有任何帮助或建议,我们将不胜感激。

在FFmpeg中,每个屏幕截图时间t:需要运行以下内容

ffmpeg -ss t -i source.mp4 -f mjpeg -vframe 1 -s wxh -

并且子进程的stdout吐出具有宽度CCD_ 2乘高度CCD_。所以你需要在t = 2, 4, 6, & 8调用这条线4次(实际上fluent-ffmpeg就是这么做的

现在,我不确定这一切是如何转化为fluent-ffmpeg的。你是对的,你需要使用pipe()。以下是我的粗略看法(我已经好几年没有使用JavaScript了,所以如果我把事情搞砸了,请原谅我(


var outStream = fs.createWriteStream('/path/to/output.mp4');
ffmpeg(`../../uploadedVideo/${filepath}`)
.format('mjpeg') // -f mjpeg
.frames(1) // -vframes 1
.size('320x240') // -s 320x240 : w = 320, h = 240
.on('error', function(err) {
console.log('An error occurred: ' + err.message);
})
.on('end', function() {
console.log('Processing finished !');
})
.pipe(outStream, { end: true });

我修改了fluent-ffmpeg文档中的一个示例。

最新更新