PHP uses FFmpeg to obtain video playback duration, bit rate, and other information.

Source: Internet
Author: User

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.

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.