FFmpeg is a set of open-source computer programs that can be used to record, convert, and transform digital audio, video, and streams, including Libavcodec, which guarantees high-mobility and codec quality.
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 (': ', $mat CHES[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 [' 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 ');p Rint_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)
FFmpeg is a set of open-source computer programs that can be used to record, convert, and transform digital audio, video, and streams, including Libavcodec, which guarantees high-mobility and codec quality.
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 (': ', $mat CHES[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 [' 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 ');p Rint_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)
This article explains the use of PHP call FFmpeg to get video information, more about the inner glory please pay attention to the PHP Chinese network.