2012-07-13 29 views
0

我的問題是,FFMpeg和Mencoder是非常足智多謀的,即使一個進程運行它使HTTPd減速,但多進程(FFMPEG/Mencoder)完全掛上它(HTTPd)。例如,我希望我的轉換能夠用Beanstalk進行處理。FFMpeg + Beanstalk:如何將進程傳遞給它或不使用Beanstalk達到相同的結果

我的具體問題是:如何將我目前的工作轉移到Beanstalk?

我有一個簡單PHP代碼觸發轉換:

RunInBackground('convert.php', array($upload, $video_id), $log_path); 

現在會有什麼魔豆正確的代碼看起來像這樣這些過程,如果多個視頻上傳不會啓動所有在同一時間?

如果您認爲我的需求最好是使用Beanstalk以外的其他產品,並且您知道如何實施它,我仍然很樂意看到它!

由於提前,
伊利亞

回答

3

兩種可能性:
- 爭取你的轉換xuggler - 內聯運行,而不必產卵工作
- 做一個「文件處理隊列」使用數據庫或文件..你的轉換過程只是查詢這個'未完成的文件來處理',但一次只能運行其中的一個。它將獨立運行你的主要任務,但可以在你的主要工作可以閱讀的地方發佈它的狀態。 EG「繁忙」或「隊列中的3個文件」或「可用」

+0

你認爲這是更好地使用DB隊列,而不是魔豆或Resque? – 2012-07-13 17:26:26

+0

因爲它聽起來像你的問題的一部分是控制多少個Beanstalk/etc的實例正在運行..這就是爲什麼我建議一個單獨的機制進行轉換,將維護一個實例。我不知道BeanStalk或Resque,但它聽起來像你是資源有限。 – ethrbunny 2012-07-13 20:12:47

1

基於ethrbunny提供的好點子,我決定用Beanstalk或Resque實現我的目標!畢竟我非常高興!我敢打賭,試圖將FFMpeg/MPlayer/MEncoder進程限制爲服務器上唯一一個活動進程以便能夠使其他進程保持良好運行狀態的所有人都會很有用,例如httpd。

一步一步!

  1. 創建一個數據庫,我把它叫做 「encoding_queue」:

    CREATE TABLE IF NOT EXISTS `encoding_queue` (
    `queue_timestamp` int(11) NOT NULL DEFAULT '0', 
    `uploaded_video_id` int(11) NOT NULL DEFAULT '0', 
    `uploaded_file_name` varchar(255) NOT NULL DEFAULT '', 
    `log_file_path` varchar(255) NOT NULL DEFAULT '', 
    PRIMARY KEY (`queue_timestamp`) 
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
    
  2. 在你的主上傳的文件,就在你觸發轉換過程:

    // Let's check if ffmpeg, mplayer, mencoder or mediainfo processes are running // 
    exec("ps ax | grep -v grep | grep ffmpeg", $ffmpeg); 
    exec("ps ax | grep -v grep | grep mplayer", $mplayer); 
    exec("ps ax | grep -v grep | grep mencoder", $mencoder); 
    exec("ps ax | grep -v grep | grep mediainfo", $mediainfo); 
    
    if(empty($ffmpeg) && empty($mplayer) && empty($mencoder) && empty($mediainfo)) { 
    // As non of them are running we start conversion process // 
    
        RunInBackground('convert.php', array($uploaded_file_name , $uploaded_video_id), $log_file_path); 
    
    } else { 
    
    // As the ffmpeg or mplayer or mencoder or mediainfo is running we add the process to the queue into the database// 
    
        //Connecting to database using ADOdb //    
    
        $sql = "INSERT INTO encoding_queue SET queue_timestamp = '" .time(). "', 
        uploaded_video_id = '" .mysql_real_escape_string($uploaded_video_id). "', 
        uploaded_file_name = '" .mysql_real_escape_string($uploaded_file_name). "', 
        log_file_path = '" .mysql_real_escape_string($log_file_path). "'"; 
        $conn->execute($sql); 
    } 
    
  3. 我們加入後值的數據庫,我們需要檢查前一次轉換是否完成,我們準備開始新的轉換!創建convert_queue.php文件並將其添加到您的CRON作業,比方說每隔5分鐘!

    <?php 
    require('path/to/your/config.php'); 
    require('path/to/your/function.php'); 
    
    exec("ps ax | grep -v grep | grep ffmpeg", $ffmpeg); 
    exec("ps ax | grep -v grep | grep mplayer", $mplayer); 
    exec("ps ax | grep -v grep | grep mencoder", $mencoder); 
    exec("ps ax | grep -v grep | grep mediainfo", $mediainfo); 
    
    $sql = "SELECT * FROM encoding_queue ORDER BY queue_timestamp ASC LIMIT 1"; 
    $rs = $conn->execute($sql); 
    $queue = $rs->getrows(); 
    if ($conn->Affected_Rows() == 0) { 
    die ("Error! There is no records in the database. Nothing to convert. Stopping..."); 
    } 
    if (file_exists($config['VDO_DIR'] . '/' .$queue[0]['uploaded_file_name'])) { 
        if(empty($ffmpeg) && empty($mplayer) && empty($mencoder) && empty($mediainfo)) { 
        RunInBackground('convert.php', array($queue[0]['uploaded_file_name'], $queue[0]['uploaded_video_id']), $queue[0]['log_file_path']); 
    } else { 
        die ("Another conversion process is still in progress. Stopping..."); 
    } 
        } else { 
        die ("Error! There is no video file ".$queue[0]['uploaded_file_name']." found. Nothing to convert. Stopping..."); } 
    ?> 
    
  4. 最後,我們處理隊列中的第一項,我們需要從數據庫中刪除它,因此convert_queue.php可以開始另一種工作。某處運行在你清理或將其添加到convert_queue.php,但如果進程失敗,你將無法再重新啓動它:

    $sql = "DELETE FROM encoding_queue WHERE uploaded_video_id = '" .mysql_real_escape_string($uploaded_video_id). "' LIMIT 1"; 
    $conn->execute($sql); 
    

那麼,我們得到什麼?如果上傳的文件是目前唯一的文件,並且目前服務器上沒有更多的轉化發生,那麼我們將開始轉換過程! 如果有一個視頻文件已經被轉換,那麼我們會將有關上傳文件的信息放入數據庫,然後等待當其他文件轉換結束後立即開始轉換。

隊列是真正的隊列 - 文件正在處理基於上傳時間!

這是一個FFMpeg/MPlayer/MEncoder隊列解決方案!

一切順利, 伊利亞

相關問題