Run a ffmpeg process in the background

后端 未结 2 1497
太阳男子
太阳男子 2020-11-30 06:55

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

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 07:42

    You don't need to write a separate php script to do this (Though you may want to later if you implement some sort of queuing system).

    You're almost there. The only problem is, the shell_exec() call blocks to wait for the return of the shell. You can avoid this if you redirect all output from the command in the shell to wither a file or /dev/null and background the task (with the & operator). So your code would become:

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

    I don't think there is any way to retrieve the PID, unfortunately.

提交回复
热议问题