PHP uses FFmpeg to read video playback time and code rate information

Source: Internet
Author: User
Tags explode file size strlen

example, FFmpeg read video playback time length and bit rate

The code is as follows:

The code is as follows Copy Code

<?php
Define (' 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]; Play time
$arr _duration = Explode (': ', $match [1]);
$data [' seconds '] = $arr _duration[0] * 3600 + $arr _duration[1] * + $arr _duration[2]; Convert 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 coding
$data [' asamplerate '] = $match [2]; Audio sampling frequency
}
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);
?>

In this case, PassThru is used, and some virtual masters may be able to disable this command

Example 2

The code is as follows Copy Code

Get thumbnails of video files
function Getvideocover ($file, $time, $name) {
if (empty ($time)) $time = ' 1 ';//default intercept first second frame
$strlenstrlen = strlen ($file);
$videoCover = substr ($file, 0, $strlen-4);
$videoCoverName = $videoCover. JPG ';//thumbnail name
EXEC ("Ffmpeg-i". $file. "-y-f mjpeg-ss". $time. "-T 0.001-s 320x240". $name. "", $out, $status);
$str = "Ffmpeg-i". $file. "-y-f mjpeg-ss 3-t". $time. "-S 320x240". $name;
echo $str. "
";
$result = System ($STR);
}

Gets the total length and creation time of the video file
function GetTime ($file) {
$vtime = EXEC ("Ffmpeg-i". $file. " 2>&1 | grep ' Duration ' | Cut-d '-F 4 | sed s/,//"); Total length of//cuplayer.com
$ctime = Date ("y-m-d h:i:s", Filectime ($file));//Creation time
$duration = Explode (":", $time);
$duration _in_seconds = $duration [0]*3600 + $duration [1]*60+ round ($duration [2]);//cuplayer.com converted to seconds
Return Array (' Vtime ' => $vtime,
' CTime ' => $ctime
);
}

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.