問題描述
我想在 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?
這可能是我的方式:
- PHP 網頁在數據庫中添加了一條記錄,表示必須處理此文件"
- 并向用戶顯示一條消息;類似于您的文件將很快得到處理"
- 首先,將記錄標記為正在處理"
- 做 ffmpeg 的事情
- 將文件標記為已處理"
- 如果它還沒有被處理
- 如果正在處理
- 或者如果它已經被處理過——然后你可以給他新視頻文件的鏈接.
這里有一些其他的想法:
Here's a couple of other thoughts :
- 當您的應用程序變大時,您可以:
- 一個網絡服務器"
- 許多處理服務器";在您的應用程序中,ffmpeg 需要大量 CPU,而不是為網頁提供服務;因此,能夠擴展該部分是很好的(這是另一個鎖定"文件,在數據庫中將它們指示為正在處理":這樣,您將不會有多個處理服務器嘗試處理同一個文件)
- 繁重/長時間的處理不是網絡服務器的工作!
- 如果您想在處理"部分切換到 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 進程的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!