Another way to obtain the total length of video time in php

Source: Internet
Author: User
At that time, the video length was obtained using a common method on the Internet to obtain the flv video file length. However, this method only supports flv videos, the value obtained from videos in other formats is very poor. at that time, the video length was obtained using the common method on the Internet to obtain the flv video file length. However, this method only supports flv videos, the value obtained from videos in other formats is very poor,
The following introduces one method: the method of returning the value of Duration using ffmpeg, which was previously used but not solved. now, ffmpeg supports videos in many formats, therefore, this is specific and universal.
Linux command for obtaining the video Duration by ffmpeg:
Ffmpeg-I test. flv 2> & 1 | grep 'duration' | cut-D'-f 4 | sed s /,//
You can get the Duration.

Command analysis:

Grep command: Match the matching strings in the search File. here, we will look for the Duration field.

Cut:Use space as the delimiter to query the fourth element. cut is a good cutting command.

The following are examples of cut:

# Ffmpeg-I test. flv

Enter the following information:

① Obtain creationdate: file creation time

Ffmpeg-I test. flv 2> & 1 | grep 'creationdate' | cut-D'-f 5-

Note: Cut is a text truncation command. it uses spaces as separators to intercept fields after 5th bits,

To intercept: 5th elements and 8th elements, write as follows:

Ffmpeg-I test. flv 2> & 1 | grep 'creationdate' | cut-D'-f 5, 8

② Obtain the video size

Use cut to intercept the tenth element with space as the separator, which 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 be replaced/new string/g'

For example: sed s/, //: replace ',' with a blank space
The following code obtains the video thumbnails and total video length:
Copy codeThe code is as follows:
/*
* Obtain the thumbnails and video length of a video file.
* Ffmpeg support is required
* @ Author PHP Huaibei
* @ Date 2011-09-14
* @ Copyright PHP Huaibei
*/
// Obtain 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/, //"); // The 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
);
}
// Obtain the thumbnail of the video file
Function getVideoCover ($ file, $ time ){
If (empty ($ time) $ time = '1'; // The first frame of the first second is intercepted by default.
$ Strlen = strlen ($ file );
$ VideoCover = substr ($ file, 0, $ strlen-4 );
$ VideoCoverName = $videoCover.'.jpg '; // name the thumbnail.
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'].'
'; // The total length.
Echo $ duration ['ctime'].'
'; // Creation time
$ VideoCoverName = getVideoCover ('/usr/local/apache/htdocs/test. flv', 6 );
Echo $ videoCoverName; // Obtain the thumbnail name
?>

Test results:

Video Length: 55 seconds 43

Video Creation time; 2011-9-13

Video thumbnail: test.jpg

----------------------------- The test is completely OK.

Supplement: to obtain the video file size, use:

Filesize ()

The filesize () function is used to obtain the file size. the default unit is bytes. the number of bytes of the file size is returned successfully. otherwise, FALSE is returned.

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.