在后台运行ffmpeg进程 [英] Run a ffmpeg process in the background

查看:414
本文介绍了在后台运行ffmpeg进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ffmpeg将视频转换为php中的.flv。目前我有这个工作,但它挂起浏览器,直到文件上传并完成。我一直在查看关于如何在后台运行exec()进程的php文档,同时使用返回的PID更新进程。这是我发现的:

I am wanting to use ffmpeg to convert video to .flv in php. Currently I have this working, but it hangs the browser until the file is uploaded and is finished. I have been looking at the php docs on how to run an exec() process in the background, while updating the process using the returned PID. Here is what I found:

//Run linux command in background and return the PID created by the OS
function run_in_background($Command, $Priority = 0)
{
    if($Priority)
        $PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!");
    else
        $PID = shell_exec("nohup $Command > /dev/null & echo $!");
    return($PID);
}

还有一个技巧,我用来跟踪后台任务是否正在运行使用返回的PID:

There is also a trick which I use to track if the background task is running using the returned PID :

//Verifies if a process is running in linux
function is_process_running($PID)
{
    exec("ps $PID", $ProcessState);
    return(count($ProcessState) >= 2);
}

我想创建一个单独的.php文件,然后从php运行cli执行这些功能之一?

Am I suppose to create a separate .php file which then runs from the php cli to execute one of these functions? I just need a little nudge in getting this working and then I can take it from there.

谢谢!

推荐答案


我想创建一个单独的.php
文件,然后从php cli
运行以执行这些函数之一?

Am I suppose to create a separate .php file which then runs from the php cli to execute one of these functions?

这可能是我会这样做的:

This is probably the way I would do it :


  • PHP页面在数据库中添加一条记录,以指示该文件必须处理


    • 并向用户显示一条消息;像你的文件将很快被处理


    • 首先,将记录标记为处理

    • 执行ffmpeg事件

    • 标记文件作为已处理


    • 如果尚未处理

    • 如果正在处理

    • 或者是已被处理,然后可以给他链接到新的视频文件。

    这里有几个其他想法:


    • 您的应用程序变大的一天,您可以拥有:


      • 一个web服务器

      • 许多处理服务器;在您的应用程序中,这是ffmpeg的事情,将需要大量的CPU,不提供网页;因此,能够缩放该部分是好的(这是另一个锁定文件,指示它们在数据库中为处理:这样,您将不会有多个处理服务器尝试处理相同的文件)


      • 重要/长时间的处理不是Web服务器的工作!

      • 您将要转换为除PHP之外的其他部分的处理部分,这将会更容易。

      您的处理脚本每隔几分钟发射一次;您可以使用 cron ,如果您位于类似Linux的计算机上。

      Your "processing script" would have to be launch every couple of minutes ; you can use cron for that, if you are on a Linux-like machine.

      编辑:看到评论后有更多的信息

      由于处理部分是从CLI完成的,而不是从Apache完成,您不需要任何背景操作:您可以使用 shell_exec ,当完成这项工作后,该命令将返回到您的PHP脚本的整个输出。

      As the processing part is done from CLI, and not from Apache, you don't need anykind of "background" manipulations : you can just use shell_exec, which will return the whole ouput of the command to your PHP script when it's finished doing it's job.

      对于观看网页的用户来说处理,看起来像后台处理;在某种程度上,这将是另一个进程的处理(甚至可能在另一台机器上)。

      For the user watching the web page saying "processing", it will seem like background processing ; and, in a way, it'll be, as the processing will be done by another processus (maybe even on another machine).

      但是,对于你来说,简单一点:

      But, for you, it'll be much simpler :


      • 一个网页(没有背景)

      • 一个CLI脚本,没有背景资料。

      您的处理脚本可能看起来像这样,我想:

      Your processing script could look like something like this, I suppose :

      // Fetch informations from DB about one file to process
      // and mark it as "processing"
      
      // Those would be fetched / determined from the data you just fetched from DB
      $in_file = 'in-file.avi';
      $out_file = 'out-file.avi';
      
      // Launch the ffmpeg processing command (will probably require more options ^^ )
      // The PHP script will wait until it's finished : 
      //   No background work
      //   No need for any kind of polling
      $output = shell_exec('ffmpeg ' . escapeshellarg($in_file) . ' ' . escapeshellarg($out_file));
      
      // File has been processed
      // Store the "output name" to DB
      // Mark the record in DB as "processed"
      

      真的比你想象的更容易,不是吗? ;-)

      只需不要担心背景的东西:只有重要的是处理脚本是定期从crontab启动的。

      Really easier than what you first thought, isn't it ? ;-)
      Just don't worry about the background stuff anymore : only thing important is that the processing script is launched regularly, from crontab.



      希望有助于: - )


      Hope this helps :-)

      这篇关于在后台运行ffmpeg进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆