Code _php tips for using ffmpeg to get FLV video thumbnails and video time

Source: Internet
Author: User
Tags call shell fread safe mode flv file

Description of the problem; get the thumbnail and video time length of the FLV video

    • Google for half a day found that you can use FFmpeg to get some information about the video, first introduce FFMEPG

Here's a simple: FFmpeg is a complete solution for recording, converting, and streaming audio and video, a leading audio/video codec class library. Official official version FFmpeg does not support RMVB and RM formats. But there are a lot of solutions.

FFmpeg's official web site is

The Chinese wiki is, with a lot of information.

㈠ installation FFMEPG

Operating system: CENTOS6

Find so many articles installed FFMEPG, basically there is no annotation, need to install so many software packages, also do not explain what is used, tangled. And the installation of the above steps are always problems, and finally have to look for official website, seriously look, the official information is very good, the future must be priority reader network data.

Because FFMEPG itself supports the FLV format, which means that there is no need to install any plug-ins only need to install FFMEPG, installation FFMEPG There are two ways: ① source package Installation, this does not know how the problem is always error ②yum command installation, CentOS This yum is the best command, hehe

Here is the installation step:

㈠ Installation Compilation Environment

#yum install-y automake autoconf libtool gcc gcc-c++

㈡ install the required libraries of RPM packages to CentOS


Install Install FFmpeg and other modules
Yum-y Install FFmpeg ffmpeg-devel

CentOS The following installation has been completed!

Install the PHP support plugin: ffmpeg-php

Install ffmpeg-php
Tar jxvf ffmpeg-php-0.6.0.tbz2
CD ffmpeg-php-0.6.0
Make install

Then modify the php.ini file
VI php.ini

Add this sentence to the php.ini file

And then restart Apache

note wget link that may be ineffective, it is estimated to be the wall, you can find their own online


But I opened Phpinfo did not see FFmpeg, do not know how, the official online installation method is required to recompile PHP to support ffmpeg, I am too troublesome, considering the service is running on the CentOS, since the CentOS can,

So I'm using the EXEC function of PHP to invoke Liunx's shell command, which means no need to install ffmpeg-php

About the EXEC function of PHP can refer to: PHP in the use of Exec,system functions such as calling the system command

Here are the common commands for getting thumbnails:

Example 1:
To intercept a picture of a 352x240 size, formatted as JPG:
Ffmpeg-i test.asf-y-F image2-t 0.001-s 352x240 a.jpg

Example 2:
Convert the first 30 frames of the video into a animated Gif:
Ffmpeg-i test.asf-vframes 30-y-F gif a.gif

Example 3: This is what I need!
Intercept the 320*240 thumbnail at the first 8.01 seconds of the video

Ffmpeg-i test.flv-y-F mjpeg-ss 3-t 0.001-s 320x240 test.jpg

Example 4:

Convert video to FLV file (this is the most used, now FLV is basically the standard for network video)

Ffmpeg-i source-s 320x240-b 700k-aspect 4:3-y-f flv dest.flv.


    • Source: Is the original file name, can be mov,mpeg,avi,wmv all kinds of formats, FFmpeg basic support.
    • -S WxH: Specifies the width and height of the video
    • -B: Set the video bit rate
    • -aspect: Keep the video rate. such as 4:3 or 16:9
    • -y: If the target file exists, overwrite the original target file directly.
    • -F: Specifies the converted file format, which is the FLV format. (In fact, if you do not specify a file format, FFmpeg will also be converted by the file suffix name).
    • Dest: The name of the target file that is converted does not necessarily need to be FLV, can be mov,mpeg, and other common formats.

Parameter description:

-L License

-H Help

-fromats display available formats, codecs, protocols

-F FMT forced to adopt format FMT

-I filename input file

-Y Overwrite output file

-t duration setting record time in HH:MM:SS[.XXX] format is also supported

-ss position search to the specified time [-]HH:MM:SS[.XXX] format also supports

S WxH: Specifies the width and height of the video


Example 3: A thumbnail for the FLV format, remember that the format of the-F cast is MJPEG because I want to get a thumbnail of. jpg, there are many written articles written in Ffmpeg-i test.flv-y-F image2-ss 08.010-t 0.001-s 352x240 b.jpg This is wrong and impossible to output.

Through the screenshot above: we can see the input of FLV information and output of JPG image information, duration is the video length required in this article, but I do not know how to get this variable

Here is the code that PHP calls the shell command to get the thumbnail


EXEC ("/usr/bin/ffmpeg-i/usr/local/apache/htdocs/test.flv-y-F mjpeg-ss 3-t 0.001-s 320x240/usr/local/apache/htdocs/ Test.jpg ", $out, $status);

Print_r ($status);//0 is a success 1 is a failure


If there is no possible reason for a picture to be generated:

① requires write permission for the folder where the picture is to be stored #chomd 777/usr/local/apache/htdocs

② has disable_functions disabled the PHP call shell command function in php.ini.

Disable_functions = Proc_open, Popen,exec, System, Shell_exec, PassThru

Workaround: Comment out the disable_functions

#disable_functions = Proc_open, Popen,exec, System, Shell_exec, PassThru

or disable_functions = (remove the Forbidden function)

Save it, turn it off, you can.

Safe Mode in ③php.ini must be closed to call the EXEC function

Safe_mode = Off

④ picture time interception is also very important, it is likely to be invalid picture or black screen

It is recommended to add keyframes, usually the first frame is the key frame, you can use: Vframes: Frame parameters, discard microsecond parameters, only the time parameter

/usr/bin/ffmpeg-i/usr/local/apache/htdocs/test.flv-y-F mjpeg-ss 3 -vframes 1-s 320x240/usr/local/apache/htdocs/ Test.jpg


It's all about getting thumbnails, and I see someone using ffmpeg to get a thumbnail of the video in Android, given that the bottom of Android is Liunx, it should be generic! Here is how to get the length of the video, although duration is the need for video length, but do not know how to fetch, if someone will, you can teach me, kneeling!

Here is the length of time to get video using pure PHP:

You search the Internet: PHP gets the FLV video length

Can find a lot of results, but I turned over more than 10 pages to find the TMD are reproduced, and all can not use, do not know why? This code and weird, you can run the code online, you will find that this is not PHP, because the editor does not show syntax highlighting, no way I follow the online handwriting on one side of the code, found that the error is strange ... The error is still very strange, interested can try, there is no way I decided to search English information, and finally saw the code in foreign websites, take a try can! Ha ha, or the foreigner's things to do AH

Wrong code:

Keyword not highlighted

The following is the correct code:

Copy Code code as follows:

function Bigendian2int ($byte _word, $signed = False) {
$int _value = 0;
$byte _wordlen = strlen ($byte _word);
for ($i = 0; $i < $byte _wordlen; $i + +) {
$int _value + + ord ($byte _word{$i}) * POW (256, ($byte _wordlen-1-$i));
if ($signed) {
$sign _mask_bit = 0x80 << (8 * ($byte _wordlen-1));
if ($int _value & $sign _mask_bit) {
$int _value = 0-($int _value & ($sign _mask_bit-1));
return $int _value;
Digital time to get video
function GetTime ($name) {
if (!file_exists ($name)) {
$flv _data_length=filesize ($name);
$fp = @fopen ($name, ' RB ');
$flv _header = Fread ($fp, 5);
Fseek ($FP, 5, Seek_set);
$frame _size_data_length =bigendian2int (Fread ($FP, 4));
$flv _header_frame_length = 9;
if ($frame _size_data_length > $flv _header_frame_length) {
Fseek ($fp, $frame _size_data_length-$flv _header_frame_length, seek_cur);
$duration = 0;
while (Ftell ($FP) + 1) < $flv _data_length) {
$this _tag_header = fread ($fp, 16);
$data _length = Bigendian2int (substr ($this _tag_header, 5, 3));
$timestamp = Bigendian2int (substr ($this _tag_header, 8, 3));
$next _offset = Ftell ($fp)-1 + $data _length;
if ($timestamp > $duration) {
$duration = $timestamp;
Fseek ($fp, $next _offset, Seek_set);
Fclose ($FP);
return $duration;
Time format converted to 0:03:56
function fn ($time) {
$num = $time;
$sec = Intval ($num/1000);
$h = intval ($sec/3600);
$m = Intval (($sec%3600)/60);
$s = intval (($sec%60));
$tm = $h. ': $m. ': $s;
return $TM;
$t = GetTime ("22.flv");//display digital time like 236722
ECHO fn ($t);//Display time format 0:03:56

Preview Effect:

My video is just 55 seconds! Ok

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