2012-09-27 57 views
1

可能重複:
Run a ffmpeg process in the backgroundPHP運行FFmpeg的背景,並找出當它完成

我在想,如果有在後臺的Runn任何簡單的方法和ffmpeg的過程,從而人們可以離開這個頁面。如果你這樣做,那麼就告訴ffmpeg進程是否正在運行,以及何時完成,將數據庫更新爲「已完成」。如果需要,我會發布我的代碼。謝謝! 這裏的處理網頁上的代碼:

所有的
<?php 
$name = $_FILES['upload_file']['name']; 
$type = $_FILES['upload_file']['type']; 
$size = $_FILES['upload_file']['size']; 
$tmpname = $_FILES['upload_file']['tmp_name']; 

if (!$title) { 
    $title = $name; 
} 

if (!$description) { 
    $description = "No description available."; 
} 

$string = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',10)),0,10); 
$videoname = "$string.mp4"; 
$date = date("F d, Y"); // October 09, 2010 
$srcFile = $tmpname; 
mkdir("users/$usercode/uploads/$string"); 
mkdir("users/$usercode/uploads/$string/HD"); 
mkdir("users/$usercode/uploads/$string/regular"); 
mkdir("users/$usercode/uploads/$string/mobile_upload"); 
mkdir("users/$usercode/uploads/$string/thumbnails"); 
$destFile1 = "https://stackoverflow.com/users/$usercode/uploads/$string/HD/$string.mp4"; 
$destFile2 = "https://stackoverflow.com/users/$usercode/uploads/$string/regular/$string.mp4"; 
$destFile3 = "https://stackoverflow.com/users/$usercode/uploads/$string/mobile_upload/$string.mp4"; 
$ffmpegPath = "/usr/local/bin/ffmpeg"; 
$flvtool2Path = "/usr/bin/flvtool2"; 
$yamdiPath = "/usr/bin/yamdi"; 
$mp4boxPath = "/usr/local/bin/MP4Box"; 
// Create our FFMPEG-PHP class 
$ffmpegObj = new ffmpeg_movie($srcFile); 
// Save our needed variables 
$srcWidth = $ffmpegObj->getFrameWidth(); 
$srcHeight = $ffmpegObj->getFrameHeight(); 
$srcFPS = $ffmpegObj->getFrameRate(); 
$srcAB = $ffmpegObj->getAudioBitRate(); 
$srcAR = $ffmpegObj->getAudioSampleRate(); 
$res = $srcWidth . "x" . $srcHeight; 
// Call our convert using exec() 
$iphone1 = getcwd().$destFile1; 
$iphone2 = getcwd().$destFile2; 
$iphone3 = getcwd().$destFile3; 
$low = "854x480"; 
$local_img = "https://stackoverflow.com/users/$usercode/uploads/$string/thumbnails/$string.jpg"; 
$img = getcwd().$local_img; 
$img = preg_replace('/ /','\ ',$img); 
$thumb = "$string.jpg"; 
$cmd = "$ffmpegPath -i $tmpname 2>&1"; 
if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) { 
    $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4]; 
    $interval = rand(0, $total); 
} 

$img = shell_exec("$ffmpegPath -ss $interval -i $tmpname 2>&1 -s 120x90 -f mjpeg -vframes 1 $img"); 


if ($srcWidth >= 1280 && $srcHeight >= 720) { 
    $out1 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre normal -ab $srcAB -ar $srcAR -b 5000k -r $srcFPS -s $res -acodec libfaac $iphone1"); 
    $out2 = shell_exec("$mp4boxPath -inter 0.5 $iphone1"); 
    $out3 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre normal -ab $srcAB -ar $srcAR -b 2000k -r $srcFPS -s $low -acodec libfaac $iphone2"); 
    $out4 = shell_exec("$mp4boxPath -inter 0.5 $iphone2"); 
    $out5 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre slow -ab 64k -ar 44100 -b 500k -r 30 -s $low -acodec libfaac $iphone3"); 
    $out6 = shell_exec("$mp4boxPath -inter 0.5 $iphone3"); 
    echo 1; 
} 
else { 
    $out3 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre normal -ab 64k -ar 44100 -b 500k -r 30 -s $low -acodec libfaac $iphone2"); 
    $out4 = shell_exec("$mp4boxPath -inter 0.5 $iphone2"); 
    $out5 = shell_exec("$ffmpegPath -i $tmpname -f mp4 -vcodec libx264 -vpre slow -ab 64k -ar 44100 -b 500k -r 30 -s $low -acodec libfaac $iphone3"); 
    $out6 = shell_exec("$mp4boxPath -inter 0.5 $iphone3");   
    echo 1; 
} 
function generate_random($number_of_characters) 
{ 
     $characters = array(); 
     $randomchar = ""; 
     $x = 0; 
     for($i = 48; $i < 123; $i++) 
     { 
       if(ctype_alnum(chr($i))) 
       { 
         $characters[$x] = chr($i); 
         $x++; 
       } 
     } 
     for($i = 0; $i < $number_of_characters; $i++) 
     { 
       $randomchar .= $characters[rand(0, count($characters) - 1)]; 
     } 
     return $randomchar; 
} 
?> 
+0

它通常總是發佈的代碼是一個好主意。 :) – jimp

+0

哈哈好的,我會的。 :) – toms900

+0

我想你的答案是在這裏:http://stackoverflow.com/questions/1198052/run-a-ffmpeg-process-in-the-background?rq=1 – jimp

回答

0

首先,你的代碼是SQL注入攻擊和shell參數的攻擊非常的主題。您需要將所有輸入轉換爲SQL和shell命令!

我認爲這將是一個標準的方法來解決這個問題:

  1. 寫您的ffmpeg的「工作」到數據庫記錄。請注意作業編號,它將是元組的主鍵。此時「狀態」需要是「完整」以外的內容。
  2. 將您的所有ffmpeg處理代碼移至助手PHP腳本。通過幫手,我只是指另一個腳本。稱之爲「ffmpeg_job_processor.php」。
  3. 在後臺運行該腳本並調用exec('php ffmpeg_job_processor.php '.escapeshellarg($jobNumber).' > /dev/null &');該腳本將通過ID從數據庫加載作業,執行所有處理,完成後它會將數據庫更新爲「完成」。
  4. 基於Web的作業監視器頁面可以通過按ID查找相同的元組來輕鬆顯示作業狀態。

訣竅是在後臺執行第二個PHP腳本,因此它會釋放您的調用PHP腳本以繼續運行而不會阻塞。您必須將您的stdout管道連接到/dev/null(Linux機箱)並使用後臺操作員&

一些更多的好信息是在這裏:Run a ffmpeg process in the background

+0

關於如何修補SQL的任何建議?這是我發現的一些舊代碼,希望能夠再次使用它。 – toms900

+0

不推薦使用mysql擴展,但如果要繼續使用它,則必須用'mysql_real_escape_string'引用所有SQL查詢參數。 – jimp

+0

看起來我已經做了一些補丁!謝謝你們的反饋! – toms900