FFmpeg is an open-source computer program that can be used to record, convert digital audio, video, and turn it into a stream. Use the LGPL or GPL license. It provides a complete solution for recording, converting, and streaming audio and video. It contains a very advanced audio/video codec library Libavcodec, in order to ensure high portability and codec quality, many of the code in LIBAVCODEC is developed from scratch.
This article will introduce the use of PHP call FFmpeg to obtain video information, call FFmpeg first need to install the ffmpeg on the server, the installation method is simple, you can search by yourself.
The code is as follows:
<?php//define FFMPEG path and command constant define (' ffmpeg_cmd ', '/usr/local/bin/ffmpeg-i '%s ' 2>&1 ');/** * Use FFMPEG to get 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 output buffer to get all output of ffmpeg $ret = Array (); duration:00:33:42.64, start:0.000000, bitrate:152 kb/s if (Preg_match ("/duration: (. *?), 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]; Converted to seconds $ret [' start '] = $matches [2]; Start time $ret [' bitrate '] = $matches [3]; Bitrate Bitrate Unit KB}//Stream #0:1: Video:rv20 (rv20/0x30325652), yuv420p, 352x288, 117 kb/s, FPS, 1k, TBN, 1k TBC if (Preg_match ("/video:"), (. *?), (. *?), (. *?) [, \s]/", $video _info, $matches)) {$ret [' vcodec '] = $matches [1]; The 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), 22050 Hz, stereo, FLTP, 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 [' Star T ']; 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 ');p Rint_r ($video _info[0]);? >
Output:
Array ([duration] = 00:33:42.64 [seconds] = 2022.64 [start] = 0.000000 [bitrate] [vcodec] = RV20 (rv20/0x30325652) [Vformat] = yuv420p [Resolution] = 352x288 [Width] = 352 [Height] = 288 [Acodec] = Cook (cook/0x6b6f 6F63) [Asamplerate] = 22050 [Play_time] = 2022.64 [size] + 38630744)