在后台运行一个程序的ffmpeg [英] Run a ffmpeg process in the background

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

问题描述

我想使用的ffmpeg转换视频在PHP中.FLV。目前我有这个工作,但直到文件上传,并完成它挂起浏览器。我一直在寻找如何在后台运行一个exec()过程中的PHP文件,同时更新使用返回的PID的进程。以下是我发现的:

背景

  //运行Linux命令并返回PID由操作系统创建
功能run_in_background($命令,$优先级= 0)
{
    如果($优先)
        $ PID =了shell_exec(nohup的漂亮-n $ $优先级指挥与GT;的/ dev / null的&安培;!回声$);
    其他
        $ PID =了shell_exec(nohup的$指挥GT;的/ dev / null的&安培;回声$!);
    回报($ PID);
}

还有,我用,如果后台任务使用返回的PID运行跟踪一招:

  //验证,如果一个进程在运行Linux
功能is_process_running($ PID)
{
    EXEC(PS $ PID,$ ProcessState);
    收益率(计数($ ProcessState)> = 2);
}

是我想创建一个单独的PHP文件,然后从PHP CLI运行来执行这些功能呢?我只是需要一点微调在得到这个工作,然后我可以把它从那里。

谢谢!


解决方案

  

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


这可能是我会做的方式:


  • 的PHP网页数据库中添加一条记录,表示该文件已被处​​理

    • 并显示一条消息给用户;类似您的文件将尽快处理


  • 在CLI,有一批工艺,新插入的文件

    • 首先,标记记录处理

    • 请FFmpeg的事情

    • 将文件标记为已处理


  • 而且,在网页上,可以显示在其中说明他文件是用户:

    • ,如果它尚未处理

    • 如果它正在处理

    • 或者如果它被处理 - 你可以再给他链接到新的视频文件


下面是一些其他的想法:


  • 如果有一天你的应用程序变得更大,您可以:

    • 一网络服务器

    • 不少处理服务器;在您的应用程序,那就是需要大量的CPU,而不是网页服务的ffmpeg的事;因此,能够扩大那部分很不错(这是另一个锁定的文件,说明他们作为DB加工:这样一来,你会不会有几个处理服务器尝试处理同​​一个文件)


  • 您只使用PHP从Web服务器生成的网页,这是Web服务器的工作乙脑

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

    • 您将要切换到PHP比别的东西的处理部分的一天,它会更容易。


您处理脚本将不得不推出每两分钟;您可以使用的cron 对于这一点,如果你是一个类似于Linux的机器上。


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

由于处理部分是从CLI完成的,而不是来自Apache,你不需要anykind的背景操作:你可以使用的 了shell_exec ,将它结束时做的工作。该命令的输出中全部返回到你的PHP脚本

有关用户观看网页说:处理,它会看起来像后台处理;并且,在某种程度上,这将是,作为处理将由另一突来完成(甚至在另一台机器)。

但是,对你来说,这将是更简单:


  • 一个网页(没有背景)

  • 一个CLI脚本,没有背景的东西无论是。

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

  //获取从数据库的信息大约一文件来处理
//并将其标记为处理//那些将获取从数据/确定你只是从数据库中取出
$ in_file中='在文件.avi;
$ out_file ='出文件.avi;//启动ffmpeg的处理命令(可能需要更多的选择^^)
// PHP脚本将等待,直到它完成:
//没有后台工作
//无需任何形式的投票
$输出=了shell_exec('ffmpeg的'escapeshellarg($ in_file中)''escapeshellarg($ out_file)。);//文件已被处​​理
//存放输出名称到DB
//马克在数据库中记录为处理

真的比你首先想到的更容易,不是吗? ;-)
结果只是不要担心背景的东西了。唯一重要的是,处理脚本定期推出,从crontab中

结果
希望这有助于: - )

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);
}

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);
}

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.

Thanks!

解决方案

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 :

  • the PHP webpage adds a record in database to indicate "this file has to be processed"
    • and displays a message to the user ; something like "your file will be processed soon"
  • In CLI, have a batch process the new inserted files
    • first, mark a record as "processing"
    • do the ffmpeg thing
    • mark the file as "processed"
  • And, on the webpage, you can show to the user in which state his file is :
    • if it has not been processed yet
    • if it's being processed
    • or if it's been processed -- you can then give him the link to the new video file.

Here's a couple of other thoughts :

  • The day your application becomes bigger, you can have :
    • one "web server"
    • many "processing servers" ; in your application, it's the ffmpeg thing that will require lots of CPU, not serving web pages ; so, being able to scale that part is nice (that's another to "lock" files, indicating them as "processing" in DB : that way, you will not have several processing servers trying to process the same file)
  • You only use PHP from the web server to generate web pages, which is je job of a web server
    • Heavy / long processing is not the job of a web server !
    • The day you'll want to switch to something else than PHP for the "processing" part, it'll be easier.

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.


Edit : a bit more informations, after seeing the comment

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 :

  • one webpage (nothing "background")
  • one CLI script, with no background stuff either.

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"

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天全站免登陆