【发布时间】:2021-06-05 00:29:49
【问题描述】:
目前我正在处理一些视频上传,所以我想在上传视频的过程中创建一个缩略图。我安装了 fluent-ffmpeg 包,但在第一次运行时出现错误 Error: cannnot find ffprobe 然后我将 ffmpeg 更改为 ffmpeg.ffprobe 然后我得到了 undefined .on.. 这是我的视频上传完整代码:
const multer = require('multer');
const uuid = require('uuid/v1');
const ffmpeg = require('fluent-ffmpeg');
const MIME_TYPE_MAP = {
'video/MPEG-4': 'MPEG-4',
'video/mpeg-4': 'mpeg-4',
'video/mp4': 'mp4',
'video/MP4': 'MP4'
};
const videoUpload = multer({
limits: 10000000,
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/videos');
},
filename: (req, file, cb) => {
const ext = MIME_TYPE_MAP[file.mimetype];
const currentfilename = uuid() + '.' + ext;
cb(null, currentfilename);
ffmpeg.ffprobe(`uploads/videos/${currentfilename}`)
.on('end', function() {
console.log('Screenshots taken');
})
.on('error', function(err) {
console.error(err);
})
.screenshots({
count: 1,
folder: 'uploads/videos/thumb',
filename: uuid() + '_screenshot.' + ext
});
}
}),
fileFilter: (req, file, cb) => {
const isValid = !!MIME_TYPE_MAP[file.mimetype];
let error = isValid ? null : new Error('Invalid mime type!');
cb(error, isValid);
}
});
module.exports = videoUpload;
【问题讨论】:
-
只验证文件上传部分 - 没有 ffmpeg - 以确保 asnych #1 ( upld ) 在代码中的哪个位置完成。这就是你的回调 w ffmpeg 片段应该去的地方 - 只需遵循流利的文档,因为你有 ffmpeg 部分的正常用例。确保在上传部分的“onComplete”事件之前没有调用 ffmpeg。
-
你能提供一些关于我发布的代码的例子吗?
标签: node.js fluent-ffmpeg video-thumbnails