Another way to get the total length of video time in PHP _php tips

Source: Internet
Author: User
Getting video length was a common way to get the length of an FLV video file on the Web, but this method is only for FLV video support, and other formats of video are not getting the same value,
Here is a description: the use of FFmpeg return value duration method, originally also wanted to use, but did not solve, now can, because the ffmpeg support a lot of formats of video, so this specific certain versatility.
FFmpeg gets the video time length of the Duration Linux command:
Ffmpeg-i test.flv 2>&1 | grep ' Duration ' | Cut-d '-F 4 | Sed s/,//
can get duration; look at the picture below

Command analysis:

grep command : matches a matching string in a lookup file, where you find the Duration field

Cut : in the space as a separator, query the fourth element, cutting is a good command

A few examples of cut are attached below:

#ffmpeg-I. test.flv

Enter the following information:

① Get CreationDate: File creation time

Ffmpeg-i test.flv 2>&1 | grep ' CreationDate ' | Cut-d '-F 5-

Description: Cut is a text intercept command: Take a space as a separator, intercept the 5th-bit field,

If you want to intercept: elements 5th and 8th, you should write this:

Ffmpeg-i test.flv 2>&1 | grep ' CreationDate ' | Cut-d '-F 5,8

② Get video size size

Use cut to intercept a space-delimited tenth element is also a video dimension

Ffmpeg-i test.flv 2>&1 | grep ' Video ' | Cut-d '-F 10 | Sed s/,//

sed command : sed ' s/the string/new string to be replaced/g '

For example: sed s/,//: "Replace with whitespace", "No."
Here's the code to get the total length of video thumbnails and video:

Copy Code code as follows:

<?php
/*
* Get the thumbnail and video length of the video file
* Need ffmpeg support
* @author PHP Huaibei
* @date 2011-09-14
* @copyright PHP Huaibei
*/
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
$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]);//Convert to second
Return Array (' Vtime ' => $vtime,
' CTime ' => $ctime
);
}
Get thumbnails of video files
function Getvideocover ($file, $time) {
if (empty ($time)) $time = ' 1 ';//default intercept first second frame
$strlen = 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". $videoCoverName. "", $out, $status);
if ($status = = 0) return $videoCoverName;
ElseIf ($status = = 1) return FALSE;
}
Call method
$duration = GetTime ('/usr/local/apache/htdocs/test.flv ');
echo $duration [' Vtime ']. <br/>/Total length
echo $duration [' CTime ']. <br/>//Creation time
$videoCoverName = Getvideocover ('/usr/local/apache/htdocs/test.flv ', 6);
echo $videoCoverName//Get thumbnail name
?>

Test effect:

Video length is: 55 seconds 43

Video creation time; 2011-9-13

Video thumbnails: test.jpg

-----------------------------Test completely OK

Add: If you want to get the size of the video file you can use:

FileSize ()

The FileSize () function is used to obtain the default unit of file size:bytes, which returns the number of bytes of file size successfully, otherwise returns FALSE.

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.