


Code to use Ffmpeg to obtain flv video thumbnail and video time_PHP tutorial
问题描述;获得flv视频的缩略图和视频时间长度
- 谷歌了半天发现可以使用Ffmpeg获得视频的一些信息,先介绍一下FFMEPG
这里简单说一下:FFmpeg是用于录制、转换和流化音频和视频的完整解决方案,一套领先的音/视频编解码类库。官方正式版ffmpeg不支持rmvb和rm格式. 不过有很多解决方法
FFmpeg的官方网址是 http://ffmpeg.mplayerhq.hu/ 。
中文Wiki是 http://www.ffmpeg.com.cn/ ,资料很多。
㈠安装FFMEPG
操作系统:centos6
找了那么多安装FFMEPG的文章,基本上都是没有注释,需要安装那么多软件包,也不说明一下是干什么用的,纠结。。而且安装上面步骤总是出问题,最后只得寻找官网,认真的看一下,确实官方的资料很好用,以后一定要优先看官网资料。
由于FFMEPG本身就支持flv格式,也就是说目前不需要安装什么插件只需要安装FFMEPG,安装FFMEPG有两种方式:①源码包安装,这个不知道怎么回事老是报错②yum命令安装,centos这个yum是最好的命令,呵呵
下面是安装步骤:
㈠安装编译环境
#yum install -y automake autoconf libtool gcc gcc-c++
㈡安装所需程序库的RPM包到 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 等模块
yum -y install ffmpeg ffmpeg-devel
***********************************centos下面的安装已经完成!
安装php支持插件:FFMPEG-PHP
安装 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
然后修改php.ini文件
vi php.ini
在php.ini文件加上这句
extension=ffmpeg.so
然后重新启动apache
/etc/init.d/httpd restart
*******备注wget链接那个可能失效,估计是被墙了,可以在网上自己找
----------------------------------------------------------------------------------------------------------
但是我打开phpinfo并没有看到FFMPEG,不知道怎么回事,官网上面提供的安装方法是需要重新编译php来支持ffmpeg,我嫌麻烦,考虑到服务都是在centos上运行,既然centos已经可以,
那我使用php的exec函数调用liunx的shell命令不就可以了,也就是说不需要安装FFMPEG-PHP
关于php的exec函数的可以参考:php中使用exec,system等函数调用系统命令
下面是获得缩略图的常用命令:
示例1:
截取一张352x240尺寸大小的,格式为jpg的图片:
ffmpeg -i test.asf -y -f image2 -t 0.001 -s 352x240 a.jpg
示例2:
把视频的前30帧转换成一个Animated Gif :
ffmpeg -i test.asf -vframes 30 -y -f gif a.gif
示例3:这个是我需要的!
在视频的第8.01秒处截取 320*240 的缩略图
ffmpeg -i test.flv -y -f mjpeg -ss 3 -t 0.001 -s 320x240 test.jpg
示例4:
把视频转换成flv文件(这个用得最多,现在Flv基本上已经成了网络视频的标准了)
ffmpeg -i source -s 320×240 -b 700k -aspect 4:3 -y -f flv dest.flv 。
其中:
- source:是原始文件的名字,可以是mov,mpeg,avi,wmv各类格式,ffmpeg基本都支持。
- -s wxh: 指定视频的宽和高
- -b : 设定视频的比特率
- -aspect: 保持视频的比率。如4:3或者16:9
- -y : 如果目标文件存在时,直接覆盖原有的目标文件。
- -f : 指定转换的文件格式,这里是flv格式。(其实如果不指定文件格式,ffmpeg也会按文件的后缀名来进行转换)。
- dest: 转换的目标文件名字,并不一定需要是flv,可以是mov,mpeg以及其他的常用格式。
参数说明:
-L license
-h 帮助
-fromats 显示可用的格式,编解码的,协议的
-f fmt 强迫采用格式fmt
-I filename 输入文件
-y 覆盖输出文件
-t duration 设置纪录时间 hh:mm:ss[.xxx]格式的记录时间也支持
-ss position 搜索到指定的时间 [-]hh:mm:ss[.xxx]的格式也支持
s wxh: 指定视频的宽和高
****************************************************************************
示例3:是针对flv格式的视频获得指定处的缩略图,记住 -f强制转换的格式是mjpeg因为我要获得.jpg的缩略图,网上有很多写成文章都是写成ffmpeg -i test.flv -y -f image2 -ss 08.010 -t 0.001 -s 352x240 b.jpg 这个是错误,不可能输出.
通过上面的截图:我们可以看到输入的flv信息和输出的jpg图片信息,Duration就是本文需要的视频长度,但是我不知道如何取得这个变量
下面是PHP调用shell命令获得缩略图的代码
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是失败
*************************************************
如果没有什么图片生成的可能原因:
①对于存储生成图片的文件夹需要有写入权限 #chomd 777 /usr/local/apache/htdocs
②在php.ini中有disable_functions禁用了php调用shell命令函数,
disable_functions = proc_open, popen,exec, system, shell_exec, passthru
解决办法:注释掉disable_functions这一项
#disable_functions = proc_open, popen,exec, system, shell_exec, passthru
或者disable_functions = (把禁言的函数除去)
保存关闭开启就可以了
③php.ini中的安全模式必须关闭 才可以调用exec函数
safe_mode = off
④图片时间截取也很重要,很有可能是无效图片或者是黑屏
建议 增加关键帧,通常第一帧为关键帧,可以使用:vframes:帧参数,舍弃微秒参数,只保留时间参数
/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
****************************************************************************
上面都是解决获得缩略图的方法,我看到有人在安卓开发中利用ffmpeg获得手机里面视频的缩略图,考虑到安卓的底层是liunx,应该是通用的!下面是如何获得视频的长度,虽然Duration就是需要的视频长度,但是不知道如何去取,如果有人会,可以教一下我,跪求!
下面是使用纯PHP获得视频的时间长度:
你在网上搜一下:php获得flv视频长度
You can find a lot of results, but after flipping through more than a dozen pages, I found that all the tmd ones were copied and reprinted, 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, things from foreigners are still useful
Error code:
Keywords are not highlighted
The following is the correct code:
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;
}
//Get the digital time of the video
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 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 digital time such as 236722
echo fn($t);/ /Display time format 0:03:56
?>
Preview effect:
My video is exactly 55 seconds! kk

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the rise of short video platforms, Douyin has become an indispensable part of everyone's daily life. On TikTok, we can see interesting videos from all over the world. Some people like to post other people’s videos, which raises a question: Is Douyin infringing upon posting other people’s videos? This article will discuss this issue and tell you how to edit videos without infringement and how to avoid infringement issues. 1. Is it infringing upon Douyin’s posting of other people’s videos? According to the provisions of my country's Copyright Law, unauthorized use of the copyright owner's works without the permission of the copyright owner is an infringement. Therefore, posting other people’s videos on Douyin without the permission of the original author or copyright owner is an infringement. 2. How to edit a video without infringement? 1. Use of public domain or licensed content: Public

Douyin, the national short video platform, not only allows us to enjoy a variety of interesting and novel short videos in our free time, but also gives us a stage to show ourselves and realize our values. So, how to make money by posting videos on Douyin? This article will answer this question in detail and help you make more money on TikTok. 1. How to make money from posting videos on Douyin? After posting a video and gaining a certain amount of views on Douyin, you will have the opportunity to participate in the advertising sharing plan. This income method is one of the most familiar to Douyin users and is also the main source of income for many creators. Douyin decides whether to provide advertising sharing opportunities based on various factors such as account weight, video content, and audience feedback. The TikTok platform allows viewers to support their favorite creators by sending gifts,

1. First open Weibo on your mobile phone and click [Me] in the lower right corner (as shown in the picture). 2. Then click [Gear] in the upper right corner to open settings (as shown in the picture). 3. Then find and open [General Settings] (as shown in the picture). 4. Then enter the [Video Follow] option (as shown in the picture). 5. Then open the [Video Upload Resolution] setting (as shown in the picture). 6. Finally, select [Original Image Quality] to avoid compression (as shown in the picture).

With the rise of short video platforms, Xiaohongshu has become a platform for many people to share their lives, express themselves, and gain traffic. On this platform, publishing video works is a very popular way of interaction. So, how to publish Xiaohongshu video works? 1. How to publish Xiaohongshu video works? First, make sure you have a video content ready to share. You can use your mobile phone or other camera equipment to shoot, but you need to pay attention to the image quality and sound clarity. 2. Edit the video: In order to make the work more attractive, you can edit the video. You can use professional video editing software, such as Douyin, Kuaishou, etc., to add filters, music, subtitles and other elements. 3. Choose a cover: The cover is the key to attracting users to click. Choose a clear and interesting picture as the cover to attract users to click on it.

Many users like to watch videos on the browser. If there is no sound when watching web videos on the edge browser, how to solve the problem? This problem is not difficult. Next, let me tell you how to fix the problem of no sound in edge browser web videos. There is no sound in edge browser web videos? Method 1: 1. First, check the top tab of the edge browser. 2. There is a "Sound Button" on the left side of the tab, make sure it is not muted. Method 2: 1. If it is confirmed that the sound is not muted, it may be a sound setting problem. 2. You can right-click the sound device in the lower right corner and select "Open Volume Synthesizer" 3. Open

Players can obtain Lucia's Crimson Abyss when playing in Battle Double Pamish. Many players don't know how to obtain Lucia's Crimson Abyss. Players can obtain it through research and development, or redeem it at the Phantom Pain Cage store. How to obtain R&D for Battle Double Pamish Lucia Crimson Abyss 1. Players can obtain it by drawing from the R&D system, which includes the base card pool, the theme limited card pool and the destiny limited card pool. 2. Revealed in these card pools The basic drop rate of Sia Crimson Abyss is 1.50%, but if the player draws Lucia Crimson Abyss from the card pool, the drop rate will increase to 1.90%. Redemption in the Phantom Pain Cage Store 1. Players can redeem fragments of Lucia Crimson Abyss by using Phantom Pain Scars in the Phantom Pain Cage Store. 2. You can redeem up to 30 fragments every week.

Microsoft PowerPoint presentation is one of our commonly used office software. In order to present the content to be explained in a comprehensive and detailed manner, video is one of the important presentation methods. You know, when we insert videos into PPT, whether through hyperlinks or WindowsMediaPlayer controls, we need to consider path issues. So, if we want to embed PPT video and combine it into a file, how should we solve it? Next, let’s take a look at the solution! The steps are as follows: 1. First, we double-click to open the [Video Embed.pptx] Microsoft PowerPoint presentation. Because ShockwaveFlashO is used

Nowadays, more and more friends are particularly dependent on Baidu Netdisk software, and they can find that the functions here are particularly rich. These functions allow everyone to choose and use them for free. It is really good at solving some problems for you. It is very powerful and easy to use. The super large storage space can really help you store various resources. You will never be able to find these files. Resources, whether you are at work or studying, if you have any document type content that you want to store, we can satisfy everyone. For these file resources here, you can view or view them at any time here. Sharing is very convenient. Anyway, it is permanently saved in your account. Then you
