[英]ffmpeg doesnt wait for function to finish before running callback
I'm using ffmpeg to extract some frames from a video.我正在使用 ffmpeg 从视频中提取一些帧。 Unfortunatley, node js does not wait for ffmpeg to finish the task before running the callback function How do I make sure, the task has finished before running the callback?
不幸的是,节点 js 在运行回调之前不等待 ffmpeg 完成任务 function 我如何确保在运行回调之前任务已经完成?
I have been struggling with this for hours, many thanks for any help我已经为此苦苦挣扎了几个小时,非常感谢您的帮助
function callback() {
console.log("All done!")
}
async function main()
{
try {
var process = new ffmpeg("star_wars_film_scene.mp4");
process.then(function (video) {
video.fnExtractFrameToJPG("frames/", {
every_n_frames : 1500
}), callback()
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
}
main()
I can't say exactly what is going wrong with your installation.我不能确切地说你的安装出了什么问题。 I can say that this sample code, taken directly from the ffmpeg module page (with a slight tweak to add more error handling) works just fine for me.
我可以说这个示例代码直接取自 ffmpeg 模块页面(稍作调整以添加更多错误处理)对我来说很好。 So, if you adapt your code to work like this and make sure that both the ffmpeg module and the ffmpeg executable are properly installed, it should work:
因此,如果您调整代码以像这样工作并确保 ffmpeg 模块和 ffmpeg 可执行文件都已正确安装,它应该可以工作:
const ffmpeg = require('ffmpeg');
try {
const process = new ffmpeg('full path to your video here');
process.then(function(video) {
// Callback mode
video.fnExtractFrameToJPG('D:/temp', {
frame_rate: 1,
number: 5,
file_name: 'my_frame_%t_%s'
}, function(error, files) {
if (error) {
console.log(error);
return;
}
console.log('Frames: ' + files);
});
}, function(err) {
console.log(err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
When I first ran this code on my system, I got an error because the ffmpeg executable (which was installed on my system) was not in my system path so when the ffmpeg module tried to run the ffmpeg executable, it didn't find it and it output an error message to that effect.当我第一次在我的系统上运行此代码时,我收到一个错误,因为 ffmpeg 可执行文件(安装在我的系统上)不在我的系统路径中,所以当 ffmpeg 模块试图运行 ffmpeg 可执行文件时,它没有找到并且它 output 一个错误消息,大意。 Once I fixed that to include it in my path, it worked as it is supposed to and captured 5 frames from the video I passed it.
一旦我修复它以将其包含在我的路径中,它就会按预期工作并从我传递的视频中捕获 5 帧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.