本文介绍了在后台运行一个程序的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运行来执行这些功能呢?我只是需要一点微调在得到这个工作,然后我可以把它从那里。

谢谢!


解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:48