<tfoot id='SkSDN'></tfoot>

    <small id='SkSDN'></small><noframes id='SkSDN'>

    <legend id='SkSDN'><style id='SkSDN'><dir id='SkSDN'><q id='SkSDN'></q></dir></style></legend>

    • <bdo id='SkSDN'></bdo><ul id='SkSDN'></ul>

  1. <i id='SkSDN'><tr id='SkSDN'><dt id='SkSDN'><q id='SkSDN'><span id='SkSDN'><b id='SkSDN'><form id='SkSDN'><ins id='SkSDN'></ins><ul id='SkSDN'></ul><sub id='SkSDN'></sub></form><legend id='SkSDN'></legend><bdo id='SkSDN'><pre id='SkSDN'><center id='SkSDN'></center></pre></bdo></b><th id='SkSDN'></th></span></q></dt></tr></i><div id='SkSDN'><tfoot id='SkSDN'></tfoot><dl id='SkSDN'><fieldset id='SkSDN'></fieldset></dl></div>

      在后台运行一个ffmpeg进程

      Run a ffmpeg process in the background(在后台运行一个ffmpeg进程)
      • <tfoot id='Xf2B9'></tfoot>
          <tbody id='Xf2B9'></tbody>

            <bdo id='Xf2B9'></bdo><ul id='Xf2B9'></ul>
            <legend id='Xf2B9'><style id='Xf2B9'><dir id='Xf2B9'><q id='Xf2B9'></q></dir></style></legend>

                <i id='Xf2B9'><tr id='Xf2B9'><dt id='Xf2B9'><q id='Xf2B9'><span id='Xf2B9'><b id='Xf2B9'><form id='Xf2B9'><ins id='Xf2B9'></ins><ul id='Xf2B9'></ul><sub id='Xf2B9'></sub></form><legend id='Xf2B9'></legend><bdo id='Xf2B9'><pre id='Xf2B9'><center id='Xf2B9'></center></pre></bdo></b><th id='Xf2B9'></th></span></q></dt></tr></i><div id='Xf2B9'><tfoot id='Xf2B9'></tfoot><dl id='Xf2B9'><fieldset id='Xf2B9'></fieldset></dl></div>

                <small id='Xf2B9'></small><noframes id='Xf2B9'>

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

                问题描述

                我想在 php 中使用 ffmpeg 将视频转换为 .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 的事情
                  • 将文件标记为已处理"
                  • 如果尚未处理
                  • 如果它正在处理中
                  • 或者,如果它已被处理,那么您可以将指向新视频文件的链接提供给他.

                  还有一些其他的想法:

                  • 您的应用程序变大的那一天,您可以拥有:
                    • 一个网络服务器"
                    • 许多处理服务器";在您的应用程序中,ffmpeg 需要大量 CPU,而不是提供网页;因此,能够扩展该部分是很好的(这是另一个锁定"文件,将它们指示为 DB 中的处理":这样,您将不会有多个处理服务器尝试处理同一个文件)
                    • 繁重/长时间的处理不是网络服务器的工作!
                    • 如果您想在处理"部分改用 PHP 以外的其他东西,那会更容易.

                    您的处理脚本"必须每隔几分钟启动一次;如果你在类似 Linux 的机器上,你可以使用 cron.

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

                    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                • <tfoot id='jXJ1b'></tfoot>

                • <small id='jXJ1b'></small><noframes id='jXJ1b'>

                  <i id='jXJ1b'><tr id='jXJ1b'><dt id='jXJ1b'><q id='jXJ1b'><span id='jXJ1b'><b id='jXJ1b'><form id='jXJ1b'><ins id='jXJ1b'></ins><ul id='jXJ1b'></ul><sub id='jXJ1b'></sub></form><legend id='jXJ1b'></legend><bdo id='jXJ1b'><pre id='jXJ1b'><center id='jXJ1b'></center></pre></bdo></b><th id='jXJ1b'></th></span></q></dt></tr></i><div id='jXJ1b'><tfoot id='jXJ1b'></tfoot><dl id='jXJ1b'><fieldset id='jXJ1b'></fieldset></dl></div>
                  <legend id='jXJ1b'><style id='jXJ1b'><dir id='jXJ1b'><q id='jXJ1b'></q></dir></style></legend>
                    <tbody id='jXJ1b'></tbody>

                      • <bdo id='jXJ1b'></bdo><ul id='jXJ1b'></ul>