Php calls ffmpeg to obtain video information. phpffmpeg

Source: Internet
Author: User

Php calls ffmpeg to obtain video information. phpffmpeg

Ffmpeg is an open-source computer program that can be used to record, convert, and convert digital audio and video into streams. It contains libavcodec to ensure high value portability and CODEC quality.

This article describes how to use php to call ffmpeg to obtain video information. To call ffmpeg, you must first install ffmpeg on the server. The installation method is simple and you can search for it by yourself.

The Code is as follows:

<? Php // defines the ffmpeg path and command constant define ('ffmpeg _ cmd', '/usr/local/bin/FFMPEG-I "% s" 2> & 1 '); /*** use ffmpeg to obtain video information * @ param String $ file video file * @ return Array */function getVideoInfo ($ file) {ob_start (); passthru (sprintf (FFMPEG_CMD, $ file); $ video_info = ob_get_contents (); ob_end_clean (); // use the output buffer to get all output content of ffmpeg $ ret = array (); // Duration: 00:33:42. 64, start: 0.000000, bitrate: 152 kb/s if (preg_match ("/Duratio N :(.*?), Start :(.*?), Bitrate: (\ d *) kb \/s/", $ video_info, $ matches) {$ ret ['duration'] = $ matches [1]; // video length $ duration = explode (':', $ matches [1]); $ ret ['seconds'] = $ duration [0] * 3600 + $ duration [1] * 60 + $ duration [2]; // convert to seconds $ ret ['start'] = $ matches [2]; // start time $ ret ['bitrate'] = $ matches [3]; // bitrate unit: kb} // Stream #0: 1: Video: rv20 (RV20/0x30325652), yuv420p, 352x288,117 kb/s, 15 fps, 15 tbr, 1 k tbn, 1 k t Bc if (preg_match ("/Video :(.*?), (.*?), (.*?) [, \ S]/", $ video_info, $ matches) {$ ret ['vcodec'] = $ matches [1]; // encoding format $ ret ['vformat'] = $ matches [2]; // video format $ ret ['resolution'] = $ matches [3]; // resolution list ($ width, $ height) = explode ('x', $ matches [3]); $ ret ['width'] = $ width; $ ret ['height'] = $ height;} // Stream #0: 0: Audio: cook (cook/0x6B6F6F63), 22050Hz, stereo, fltp, 32 kb/s if (preg_match ("/Audio :(. *), (\ d *) Hz/", $ video_info, $ matches )){ $ Ret ['acodec'] = $ matches [1]; // Audio Encoding $ ret ['asamplerate'] = $ matches [2]; // audio sampling frequency} if (isset ($ ret ['seconds']) & isset ($ ret ['start']) {$ ret ['play _ time'] = $ ret ['seconds'] + $ ret ['start']; // actual playback time} $ ret ['SIZE'] = filesize ($ file); // video file size $ video_info = iconv ('gbk', 'utf8 ', $ video_info); return array ($ ret, $ video_info);} // output video information $ video_info = getVideoInfo ('myvideo. avi'); print_r ($ video_info [0]) ;?>

Output:

Array( [duration] => 00:33:42.64 [seconds] => 2022.64 [start] => 0.000000 [bitrate] => 152 [vcodec] => rv20 (RV20 / 0x30325652) [vformat] => yuv420p [resolution] => 352x288 [width] => 352 [height] => 288 [acodec] => cook (cook / 0x6B6F6F63) [asamplerate] => 22050 [play_time] => 2022.64 [size] => 38630744)

The simple implementation of the above php call ffmpeg to obtain video information is all the content shared by xiaobian. I hope to give you a reference, and I hope you can provide more support for the customer's house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.