Home Backend Development PHP Problem How to use Ffmpeg to get flv video thumbnail and video length time

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

Jul 14, 2021 pm 03:35 PM
ffmpeg

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

<?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是失败
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:

<?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, &#39;rb&#39;);
	$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.&#39;:&#39;.$m.&#39;:&#39;.$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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Golang and FFmpeg: How to achieve audio mixing and separation Golang and FFmpeg: How to achieve audio mixing and separation Sep 27, 2023 pm 02:24 PM

Golang vs. FFmpeg: How to implement audio mixing and separation, specific code examples required Summary: Audio processing is an essential part of many multimedia applications. In Golang, we can use the FFmpeg library to achieve audio mixing and separation. This article will introduce how to use Golang to call the FFmpeg library to achieve audio mixing and separation, and provide specific code examples. By studying this article, readers will learn how to use Golang and FFmpeg to implement audio processing

Practice of video splicing using Golang and FFmpeg Practice of video splicing using Golang and FFmpeg Sep 28, 2023 am 08:37 AM

Practical introduction to video splicing using Golang and FFmpeg: In daily life, we often encounter situations where we need to merge multiple video files into one, such as splicing multiple recorded videos into a complete video file. To achieve this goal, this article will introduce how to use Golang and FFmpeg libraries to implement the video splicing process, and provide specific code examples. 1. What are Golang and FFmpeg? Golang (Go language) is an open source programming language developed by

Golang and FFmpeg: How to implement audio format conversion and compression Golang and FFmpeg: How to implement audio format conversion and compression Sep 28, 2023 pm 07:13 PM

Golang and FFmpeg: How to implement audio format conversion and compression, specific code examples are needed. Introduction: In audio file processing, sometimes you encounter the need to convert audio formats or compress audio file sizes. As a powerful programming language, Golang, combined with FFmpeg, a popular audio and video processing tool, can achieve fast and efficient audio format conversion and compression. This article will introduce how to use Golang and FFmpeg to achieve audio format conversion and compression, and give specific code examples.

Golang and FFmpeg: How to implement audio synthesis and segmentation Golang and FFmpeg: How to implement audio synthesis and segmentation Sep 27, 2023 pm 10:52 PM

Golang and FFmpeg: How to implement audio synthesis and segmentation, specific code examples are required Summary: This article will introduce how to use Golang and FFmpeg libraries to implement audio synthesis and segmentation. We will use some specific code examples to help readers understand better. Introduction: With the continuous development of audio processing technology, audio synthesis and segmentation have become common functional requirements in daily life and work. As a fast, efficient and easy to write and maintain programming language, Golang, coupled with FFmpeg

Golang and FFmpeg: Technology for real-time video stream analysis and recognition Golang and FFmpeg: Technology for real-time video stream analysis and recognition Sep 27, 2023 pm 02:31 PM

Golang and FFmpeg: Technology to implement real-time video stream analysis and recognition, requiring specific code examples Introduction: In today's digital and intelligent era, video technology is increasingly used. Among them, the analysis and recognition of real-time video streams play an important role in security monitoring, intelligent transportation, face recognition and other fields. This article will introduce how to use the technology combining Golang and FFmpeg to realize the analysis and identification of real-time video streams, and provide specific code examples. 1. Introduction to Golang Golang is a

How to install PHP FFmpeg extension on server? How to install PHP FFmpeg extension on server? Mar 28, 2024 pm 02:39 PM

How to install PHPFFmpeg extension on server? Installing the PHPFFmpeg extension on the server can help us process audio and video files in PHP projects and implement functions such as encoding, decoding, editing, and processing of audio and video files. This article will introduce how to install the PHPFFmpeg extension on the server, as well as specific code examples. First, we need to ensure that PHP and FFmpeg are installed on the server. If FFmpeg is not installed, you can follow the steps below to install FFmpe

PHP FFmpeg extension installation guide: easy-to-follow tutorial PHP FFmpeg extension installation guide: easy-to-follow tutorial Mar 28, 2024 pm 02:17 PM

PHPFFmpeg Extension Installation Guide: Simple and easy-to-understand tutorial In the process of website development, sometimes we need to process various multimedia files, such as audio, video, etc. FFmpeg is a powerful multimedia processing tool that can process audio, video and other formats, and supports various transcoding, cutting and other operations. The PHPFFmpeg extension is an extension library that calls FFmpeg functions in PHP. It can be used to process multimedia files easily. Below we will introduce PHPF in detail

The practice of using Golang and FFmpeg to achieve video de-flickering The practice of using Golang and FFmpeg to achieve video de-flickering Sep 27, 2023 pm 04:46 PM

A practical overview of using Golang and FFmpeg to achieve video de-flickering: Video flickering is a challenge often encountered in video processing. When the frame rate of the recorded video does not match the lighting frequency, it may cause flickering in the video. This article will introduce how to use Golang and FFmpeg libraries to achieve video de-flickering, and provide specific code examples. Steps: Install the FFmpeg library: First, we need to install the FFmpeg library in the Golang development environment. able to pass

See all articles