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

Source: Internet
Author: User
At that time to obtain video length is the use of Web-based universal access to FLV video file length method, but this method only for FLV video support, other formats of video to get the value is very poor,
Here is an introduction: the use of FFmpeg return value duration method, originally also wanted to use, but did not solve, now can, because ffmpeg support a lot of formats of video, so this specific certain versatility.
FFmpeg get 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;

Command analysis:

grep command : Matches the string found in the search file, looking for the Duration field

Cut: Take the space as a separator, query the fourth element, cut is a good cut 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 delimiter, intercept the 5th bit after the field,

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

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

② Get Video size

Using cut to intercept the tenth element with a space delimiter is also the video size

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

sed command : sed ' s/string to replace/new string/g '

For example: sed s/,//: means: replace ', ' with a blank character
Here's the code that takes the total length of the video thumbnail and video:
Copy CodeThe code is as follows:
/*
* Get thumbnail and video length for video files
* Requires FFMPEG support
* @author PHP Huaibei
* @date 2011-09-14
* @copyright PHP Huaibei
*/
Get the total length and creation time of a 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 seconds
Return Array (' vtime ' = $vtime,
' CTime ' = $ctime
);
}
Get thumbnail images of video files
function Getvideocover ($file, $time) {
if (empty ($time)) $time = ' 1 ';//default intercept first Second first 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". $videoCoverName. ", $out, $status);
if ($status = = 0) return $videoCoverName;
ElseIf ($status = = 1) return FALSE;
}
Calling methods
$duration = GetTime ('/usr/local/apache/htdocs/test.flv ');
echo $duration [' vtime ']. '
';//Total length
echo $duration [' CTime ']. '
';//Creation time
$videoCoverName = Getvideocover ('/usr/local/apache/htdocs/test.flv ', 6);
echo $videoCoverName;//Get thumbnail name
?>

Test Results :

Video length is: 55 seconds 43

Video creation time; 2011-9-13

Video thumbnail: test.jpg

-----------------------------test is fully OK

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

FileSize ()

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

http://www.bkjia.com/PHPjc/324235.html www.bkjia.com true http://www.bkjia.com/PHPjc/324235.html techarticle at that time to obtain video length is the use of the general web to obtain FLV video file length method, but this method only for FLV video support, the other format of the video to get the value is very poor, ...

  • 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.