PHP使用FFmpeg擷取視頻播放總時間長度與碼率等資訊_php執行個體

來源:互聯網
上載者:User

請注意:這篇文章中會用到passthru,可能部分虛擬機器主機會將此命令禁用。

代碼如下:

PHP

<?phpdefine('FFMPEG_PATH', '/usr/local/ffmpeg2/bin/ffmpeg -i "%s" 2>&1'); function getVideoInfo($file) {    $command = sprintf(FFMPEG_PATH, $file);    ob_start();  passthru($command);  $info = ob_get_contents();  ob_end_clean();    $data = array();  if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {    $data['duration'] = $match[1]; //播放時間    $arr_duration = explode(':', $match[1]);    $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //轉換播放時間為秒數    $data['start'] = $match[2]; //開始時間    $data['bitrate'] = $match[3]; //碼率(kb)  }  if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $info, $match)) {    $data['vcodec'] = $match[1]; //視頻編碼格式    $data['vformat'] = $match[2]; //視頻格式    $data['resolution'] = $match[3]; //視頻解析度    $arr_resolution = explode('x', $match[3]);    $data['width'] = $arr_resolution[0];    $data['height'] = $arr_resolution[1];  }  if (preg_match("/Audio: (\w*), (\d*) Hz/", $info, $match)) {    $data['acodec'] = $match[1]; //音頻編碼    $data['asamplerate'] = $match[2]; //音頻採樣頻率  }  if (isset($data['seconds']) && isset($data['start'])) {    $data['play_time'] = $data['seconds'] + $data['start']; //實際播放時間  }  $data['size'] = filesize($file); //檔案大小  return $data;} //用法$video_info = getVideoInfo('video.mp4');print_r($video_info);?>

總結

以上就是這篇文章的全部內容,希望對大家學習或使用PHP的時候能有所協助。如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.