亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 php、ffmpeg 和 proc_open 逐幀讀取視頻

使用 php、ffmpeg 和 proc_open 逐幀讀取視頻

PHP
忽然笑 2022-12-30 16:05:32
我想使用 proc_open 使用 ffmpeg 從視頻中讀取每一幀,然后使用 PHP 對幀進行處理。下面是相反的示例 - 它從 PHP 逐幀發送到 ffmpeg:$descriptors = array(    0 => array("pipe", "r"),    1 => array("pipe", "w"),     2 => array("file", "C:/error-output.txt", "a")  );$frames = glob('Q:/images/*.jpg');$command = "ffmpeg -f image2pipe -pix_fmt rgb24 -framerate 10 -c:v mjpeg -i - ".                "-r 10 -vcodec libx264 -pix_fmt yuv420p  -preset faster -crf 17 ".                "-y test.mp4";$ffmpeg = proc_open($command, $descriptors, $pipes);if (!is_resource($ffmpeg)){    die('Could not run ffmpeg');}foreach ($frames AS $frame){    fwrite($pipes[0], file_get_contents($frame) );}fclose($pipes[0]);這就是我正在嘗試但無法正確完成的方式:$descriptors = array(    0 => array("pipe", "r"),    1 => array("pipe", "w"),  write to    2 => array("file", "C:/error-output.txt", "a") );$command = "ffmpeg -i 1.gif -ss 00:00:3 -s 650x390 -vframes 100 -c:v png -f image2pipe -";$ffmpeg = proc_open($command, $descriptors, $pipes);if (!is_resource($ffmpeg)){    die('Could not run ffmpeg');}while (!feof($pipes[1])){    $frame = fread($pipes[1], 5000);}fclose($pipes[1]);最大的問題是我不知道要從 ffmpeg 讀取多少數據才能獲得整個幀。
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

$descriptors = array(

    0 => array("pipe", "r"),    

    1 => array("pipe", "w"), 

    2 => array("file", "C:/path/error-output.txt", "a") 

);


$command = 'ffmpeg -i INPUT.mp4 -ss 00:00:10 -t 120 -c:v png -f image2pipe  -compression_level 0 -';


$ffmpeg = proc_open($command, $descriptors, $pipes);


if (!is_resource($ffmpeg))

{

    die('Could not run ffmpeg');


}


$k = 0;


$buff = '';


$blend = 0; 


while (!feof($pipes[1]))

{

    $buff .= fread($pipes[1], 50000);


    $pos = strpos($buff, chr(137).'PNG', 1);


    if ($pos !== false)

    {

        $frame = substr($buff, 0, $pos);

        $buff = substr($buff, $pos);


        handleFrame($frame, $k);    


        $k++;       

    }

}


handleFrame($buff, $k);


fclose($pipes[0]);

fclose($pipes[1]);




function handleFrame(&$str, $num)

{

    $im = imagecreatefromstring($str);

    ...

}


查看完整回答
反對 回復 2022-12-30
  • 1 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號