PHP uses FFmpeg to obtain video playback duration, bit rate, and other information.
Note: passthru is used in this article. Some virtual hosts may disable this command.
The Code is as follows:
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]; // playback time $ arr_duration = explode (':', $ match [1]); $ data ['seconds'] = $ arr_duration [0] * 3600 + $ arr_duration [1] * 60 + $ arr_duration [2]; // convert the playback time to seconds $ data ['start'] = $ match [2]; // start time $ data ['bitrate'] = $ match [3]; // bit rate (kb)} if (preg_match ("/Video :(. *?), (.*?), (.*?) [, \ S]/", $ info, $ match) {$ data ['vcodec'] = $ match [1]; // video encoding format $ data ['vformat'] = $ match [2]; // video format $ data ['resolution'] = $ match [3]; // video resolution $ 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]; // Audio Encoding $ data ['asamplerate'] = $ match [2]; // audio sampling frequency Rate} if (isset ($ data ['seconds']) & isset ($ data ['start']) {$ data ['play _ time'] = $ data ['seconds'] + $ data ['start']; // actual playback time} $ data ['SIZE'] = filesize ($ file); // file size return $ data;} // usage $ video_info = getVideoInfo('video.mp4 '); print_r ($ video_info);?>
Summary
The above is all the content of this article, hoping to help you learn or use PHP. If you have any questions, you can leave a message.