How to use Ffmpeg to get flv video thumbnail and video length time

醉折花枝作酒筹
Release: 2023-03-11 14:28:02
forward
1897 people have browsed it

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.

How to use Ffmpeg to get flv video thumbnail and video length time

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.

How to use Ffmpeg to get flv video thumbnail and video length time

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


        
Copy after login

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

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:

How to use Ffmpeg to get flv video thumbnail and video length time

Keywords are not highlighted

The following is correct Code:

 $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 ?>
Copy after login

Preview effect:

How to use Ffmpeg to get flv video thumbnail and video length time

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!