Code for obtaining flv video thumbnails and video time using Ffmpeg

Source: Internet
Author: User
Tags call shell flv file

Problem description; obtain the thumbnail and video duration of the flv video.

  • Google found that Ffmpeg can be used to obtain video information for half a day. First, let's introduce FFMEPG.

Here, I will briefly describe: FFmpeg is a complete solution for recording, conversion, and streaming audio and video. It is a leading Audio/Video Codec class library. Official official ffmpeg does not support rmvb and rm formats, but there are many solutions

The official website of FFmpeg is http://ffmpeg.mplayerhq.hu /.

Chinese Wiki is http://www.ffmpeg.com.cn/, a lot of information.

(I) install FFMEPG

Operating System: centos6

I found so many articles on FFMEPG installation, basically without comments. I need to install so many software packages, which does not explain what to do and how to use them .. In addition, there is always a problem with the above steps. At last, I had to look for the official website. take a serious look at the official information. In the future, we must first check the official website information.

Because FFMEPG itself supports the flv format, that is to say, you do not need to install any plug-ins, you only need to install FFMEPG. There are two ways to install FFMEPG: ① install the source code package, I don't know what's going on. I always reported an error. ② the yum command is installed, and centos yum is the best command.

The installation steps are as follows:

(I) install the compiling environment

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

(Ii) install the RPM package of the required library to centos

Rpm-Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm

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

************************************ The installation under centos has done!

Install php support plug-in: FFMPEG-PHP

Install FFMPEG-PHP
Cd/usr/local/src
Wget http://garr.dl.sourceforge.net/sourceforge/ffmpeg-php/ffmpeg-php-0.6.0.tbz2
Tar jxvf ffmpeg-php-0.6.0.tbz2
Cd ffmpeg-php-0.6.0
/Usr/local/php/bin/phpize
./Configure -- with-php-config =/usr/local/php/bin/php-confi
Make
Make install

Then modify the php. ini file.
Vi php. ini

Add this sentence to the php. ini file.
Extension = ffmpeg. so

Then restart apache
/Etc/init. d/httpd restart

* ****** Note that the wget link may be invalid. It is estimated that it is blocked. You can find it on the Internet.

Bytes ----------------------------------------------------------------------------------------------------------

But I didn't see FFMPEG when I opened phpinfo. I don't know what's going on. The installation method provided on the official website is to re-compile php to support ffmpeg. I am too troublesome, since the services are all running on centos,

So I use php exec function call liunx shell command can not be, that is, do not need to install FFMPEG-PHP

For details about php exec functions, refer to using exec, system, and other functions in php to call system commands.

The following are common commands for obtaining thumbnails:

Example 1:
Take an image of the 352x240 size in jpg format:
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 an animation Gif:
Ffmpeg-I test. asf-vframes 30-y-f gif a.gif

Example 3: This is what I need!
Crop a 8.01*320 thumbnail at the second of the video

Ffmpeg-I test. flv-y-f mjpeg-ss 3-t 0.001-s 320x240 test.jpg

Example 4:

Convert a video to a flv file (this is the most widely used file. Now Flv has basically become the standard for online videos)

Ffmpeg-I source-s 320x240-B 700 k-aspect-y-f flv dest. flv.

Where:

  • Source: the name of the original file. It can be mov, mpeg, avi, or wmv. ffmpeg is basically supported.
  • -S wxh: Specifies the video width and height.
  • -B: sets the video bit rate.
  • -Aspect: Specifies the video retention rate. For example, or
  • -Y: if the target file exists, the original target file will be overwritten.
  • -F: Specifies the format of the converted file. The format is flv. (In fact, if the file format is not specified, ffmpeg will also convert according to the file suffix ).
  • Dest: name of the target file to be converted, not necessarily flv. It can be mov, mpeg, or other common formats.

Parameter description:

-L license

-H help

-Fromats: display available formats, codec, and Protocols

-F fmt forced the fmt Format

-I filename input file

-Y overwrites the output file

-T duration: the recording time in hh: mm: ss [. xxx] format is also supported.

-Ss position: the specified time [-] hh: mm: ss [. xxx] format is also supported.

S wxh: Specifies the video width and height.

**************************************** ************************************

Example 3: a thumbnail of a specified position is obtained for a flv video. Remember that the format of the -fforce conversion is the thumbnail of the message queue because I want to upload .jpg. Many articles written on the Internet are written as ffmpeg-I test. flv-y-f image2-ss 08.010-t 0.001-s 352x240 B .jpg is incorrect and cannot be output.

Through the above: we can see the input flv information and the output jpg image information. Duration is the video length required in this article, but I don't know how to obtain this variable.

The following is the code for PHP to call the shell command to obtain the thumbnail.

<? Php

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 indicates success. 1 indicates failure.

**************************************** *********

If there are no possible causes for image generation:

① Write permission is required for the folder storing the generated image # chomd 777/usr/local/apache/htdocs

② In php. ini, disable_functions disables php to call shell command functions,

Disable_functions = proc_open, popen, exec, system, shell_exec, passthru

Solution: Comment out disable_functions.

# Disable_functions = proc_open, popen, exec, system, shell_exec, passthru

Or disable_functions = (remove banned functions)

Save, close, and enable it.

③ The exec function can be called only when the security mode in php. ini is disabled.

Safe_mode = off

④ The screenshot time is also very important, and it is likely that the image is invalid or black screen.

We recommend that you add a key frame. Generally, the first frame is a key frame. You can use: vframes: frame parameter,Discard the microsecond parameter and retain 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

**************************************** ************************************

The above is a solution for obtaining thumbnails. I have seen someone using ffmpeg in Android development to obtain thumbnails of videos in mobile phones. Considering that the android underlying layer is liunx, it should be generic! The following shows how to get the video length. Although Duration is the required video length, I don't know how to get it. If someone can, please teach me and beg for it!

The following figure shows the video length obtained using pure PHP:

Search on the Internet: php gets the flv video Length

I can find many results, but after turning over more than a dozen pages, I found that tmd was copied and reproduced, and all of them were unavailable. I don't know why? This code is strange. You can run the code on the Internet. You will find that this is not a php code, because the editor does not display syntax highlighting, and there is no way for me to write the code on the Internet, the error is still reported... It is still strange to report an error. If you are interested, you can try it. I have no choice but to search for the English documents. I finally saw the code on a foreign website and tried it! Haha, it's a good thing for foreigners.

Error code:

Keyword not highlighted


The following is the correct code:

Copy codeThe Code is as follows: <? Php
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}) x 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;
}
// Obtain the video's digital time
Function getTime ($ name ){
If (! File_exists ($ name )){
Return;
}
$ 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;
}
// Convert to the 0:03:56 time format
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 the numeric time, such as 236722
Echo fn ($ t); // display time format 0:03:56
?>

Preview effect:

My video is 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: 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.