简体   繁体   English

ffmpeg 在运行回调之前不等待 function 完成

[英]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.

相关问题 等待javascript回调函数完成 - Wait for javascript callback function to finish 等待Java Web抓取功能完成,然后再运行下一页? - Wait for Javascript Web Scraping Function to finish before running for next page? 如何等待 function 完成后再运行另一个或 rest 的代码? - How to wait for function to finish before running another one or rest of the code? 等待异步函数完成而不添加回调 - Wait for asynchronous function to finish without adding callback 如何在使用Node.js调用回调函数之前等待N个查询完成 - How to wait for N number of queries to finish before calling the callback function with Nodejs 在 Mongoose 中呈现页面之前,如何等待带有回调 function 的 for 循环完成 - How do I wait for a for loop with a callback function inside to finish before I render a page in Mongoose 如何在JS(节点)中使用回调来等待Async函数完成才能继续? - How to use callback in JS (node) to wait for Async function to finish before proceeding? NodeJS 在继续之前等待 for 循环中的函数完成 - NodeJS Wait for function in for loop to finish before continuing 在执行下一个 function 之前等待 writestream 完成 - wait for writestream to finish before executing next function 在节点js中执行下一条语句之前等待回调完成 - Wait for callback to finish before executing next statement in node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM