FFmpeg is a complete solution for recording, converting and streaming audio and video, a leading audio/video codec library. The official version of ffmpeg does not support rmvb and rm formats. Today we will introduce it.
After searching Google for a long time, I found that I can use Ffmpeg to obtain some information about the video. Let me first introduce FFMEPG
Here is a brief explanation: FFmpeg is used for A complete solution for recording, converting and streaming audio and video, a leading audio/video codec library. The official version of ffmpeg does not support rmvb and rm formats. However, there are many solutions
The official website of FFmpeg is http://ffmpeg.mplayerhq.hu/.
The Chinese Wiki is http://www.ffmpeg.com.cn/, which has a lot of information.
㈠Installing FFMEPG
Operating system: centos6
I have found so many articles about installing FFMEPG, but basically there are no comments, so many software packages need to be installed, and there are no Explain what it is used for, I'm confused. . Moreover, there are always problems with the above steps of installation. In the end, I have to look for the official website and take a careful look. It is true that the official information is very useful. I must give priority to the official website information in the future.
Since FFMEPG itself supports the flv format, that is to say, there is currently no need to install any plug-ins and only FFMEPG needs to be installed. There are two ways to install FFMEPG: ① Source code package installation, I don’t know why this always reports an error ②yum Command installation, centos yum is the best command, haha
The following are the installation steps:
㈠Install the compilation environment
#yum install -y automake autoconf libtool gcc gcc -c
㈡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 ffmpeg-devel
***** ****************************centos The installation below has been completed!
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
and then restart apache
/etc/init.d/ httpd restart
*******Note: The wget link may be invalid. It is probably blocked. You can find it online
----------- -------------------------------------------------- ----------------------------------------
But I didn’t see FFMPEG when I opened phpinfo. I don’t know what happened. The installation method provided on the official website requires recompiling PHP to support ffmpeg. I find it troublesome. Considering that the services are all running on centos, since centos can already do it. ,
Then I can use the exec function of php to call the shell command of liunx, which means that there is no need to install FFMPEG-PHP
For information about the exec function of php, please refer to: php Use exec, system and other functions to call system commands
The following are common commands to obtain thumbnails:
Example 1:
Capture a 352x240 size picture 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 Animated Gif:
ffmpeg -i test.asf -vframes 30 -y -f gif a.gif
Example 3: This is what I need!
Take a 320*240 thumbnail at the 8.01 second of the video
ffmpeg -i test.flv -y -f mjpeg -ss 3 -t 0.001 -s 320x240 test.jpg
Example 4:
Convert the video to a flv file (this is the most used, and now Flv has basically become the standard for online videos)
ffmpeg -i source -s 320× 240 -b 700k -aspect 4:3 -y -f flv dest.flv.
Among them:
source: is the name of the original file, which can be mov, mpeg, avi, wmv formats, ffmpeg basically supports them.
-s wxh: Specify the width and height of the video
-b: Set the bitrate of the video
-aspect: Maintain the aspect ratio of the video. For example, 4:3 or 16:9
-y: If the target file exists, directly overwrite the original target file.
-f: Specify the converted file format, here is the flv format. (In fact, if the file format is not specified, ffmpeg will also convert according to the file suffix name).
dest: The converted target file name does not necessarily need to be flv, it can be mov, mpeg and other common formats.
Parameter description:
-L license
-h Help
-fromats Display available formats, codecs, Protocol
-f fmt forces the format fmt
-I filename input file
-y overwrites the output file
-t duration sets the recording time hh The recording time in the :mm:ss[.xxx] format also supports
-ss position. The format of searching for the specified time [-]hh:mm:ss[.xxx] also supports
s wxh: Specify the width and height of the video
****************************************** *******************************************
Example 3: Get the thumbnail at the specified location for the video in flv format. Remember that the format of -f forced conversion is mjpeg because I want to get the thumbnail of .jpg. There are many articles on the Internet written as ffmpeg -i test.flv -y -f image2 -ss 08.010 -t 0.001 -s 352x240 b.jpg This is an error and it is impossible to output.
Through the screenshot above: we can Seeing the input flv information and the output jpg image information, Duration is the video length required for this article, but I don’t know how to obtain this variable
The following is the code for calling the shell command in PHP 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是成功 1是失败
**************************************************** **
If no pictures are generated, possible reasons:
①The folder where the generated pictures are stored needs to have write permission#chomd 777 /usr/local/apache/htdocs
②There is disable_functions in php.ini that disables php from calling the shell command function,
disable_functions = proc_open, popen,exec, system, shell_exec, passthru
Solution: Comment out disable_functions This item
#disable_functions = proc_open, popen,exec, system, shell_exec, passthru
or disable_functions = (remove the banned function)
Save, close and enable That’s it
③The safe mode in php.ini must be turned off before calling the exec function
safe_mode = off
④Picture time interception is also very important, it is likely to be invalid The picture or black screen
It is recommended to add key frames. Usually the first frame is the key frame. You can use: vframes: frame parameter, discard the microsecond parameter and only keep 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 are all solutions to obtain thumbnails. I saw someone using ffmpeg in Android development. Get the thumbnail of the video in the mobile phone. Considering that the bottom layer of Android is liunx, it should be universal! Here's how to get the length of the video. Although Duration is the required video length, I don't know how to get it. If anyone knows how to get it, can you teach me, please!
The following is the length of video obtained using pure PHP:
You can search on the Internet: php to obtain the length of flv video
You can find a lot of results, but I turned it over On more than ten pages, I found that tmd was copied and reproduced, and all of them cannot be used. I don’t know why? This code is weird. You can run the code on the Internet. You will find that this is not PHP, because the editor does not display syntax highlighting. There is no way. I copied the code online and found that the error is still weird. . . The error reported is very strange. If you are interested, you can try it. There is no way I decided to search for English information. Finally, I saw the code on a foreign website. I can give it a try! Hahaha It’s still foreigner’s stuff that works so well
Incorrect code:
Keywords are not highlighted
The following is correct Code:
<?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}) * 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; } //获得视频的数字时间 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; } //转化为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");//显示数字时间如236722 echo fn($t);//显示时间格式0:03:56 ?>
Preview effect:
My video is 55 seconds exactly! ok
Recommended learning: php video tutorial
The above is the detailed content of How to use Ffmpeg to get flv video thumbnail and video length time. For more information, please follow other related articles on the PHP Chinese website!