ffmpeg是一款开源、跨平台的视频处理程序,可用在Windows、mac、linux等平台,可以方便的运用多种语言脚本来调用其执行视频的操作。
虚拟机当前系统环境:
php:PHP 7.2.1
nginx:nginx/1.10.
linux:CentOS Linux release 7.4.1708 (Core)
mysql:Ver 15.1 Distrib 10.2.11-MariaDB
1、Linux编译安装ffmpeg:
载安装源,配置,编译,安装,设置环境变量。
官网:http://www.ffmpeg.org/download.html
[root@localhost src]# cd /usr/local/src/ [root@localhost src]# wget -O ffmpeg-4.0.tar.bz2 https://ffmpeg.org/releases/ffmpeg-4.0.tar.bz2 [root@localhost src]# tar -jxvf ffmpeg-4.0.tar.bz2 [root@localhost src]# cd ffmpeg-4.0/ [root@localhost ffmpeg-4.0]# ./configure --prefix=/usr/local/ffmpeg [root@localhost ffmpeg-4.0]# make && make install
等待安装完成...
[root@localhost ffmpeg-4.0]#ffmpeg -version 查看版本 [root@localhost ~]# ffmpeg -version ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-16) configuration: --prefix=/usr/local/ffmpeg libavutil 56. 14.100 / 56. 14.100 libavcodec 58. 18.100 / 58. 18.100 libavformat 58. 12.100 / 58. 12.100 libavdevice 58. 3.100 / 58. 3.100 libavfilter 7. 16.100 / 7. 16.100 libswscale 5. 1.100 / 5. 1.100 libswresample 3. 1.100 / 3. 1.100
当然,你也可以把 /usr/local/ffmpeg/bin 加入到环境变量,这里就暂不作说明。
注意,安装过程中出现以下错误:
yasm/nasm not found or too old. Use –disable-yasm for a crippled build.
If you think configure made a mistake, make sure you are using the latest version from Git. If the latest version fails, report the problem to the ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net. Include the log file “config.log” produced by configure as this will help solve the problem.
需要安装yasm:
# wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz # tar -zxvf yasm-1.3.0.tar.gz # cd yasm-1.3.0 # ./configure # make && make install
2、下面介绍使用ffmpeg获取视频首帧的方法。
<?php header('content-type:text/html;charset=utf-8'); date_default_timezone_set('PRC'); //设置中国时区 //待处理视频 $in_file = './1111.mkv'; //缩略图保存路径 $out_file = './1111.jpg'; //shell脚本 $shell = "/usr/local/ffmpeg/bin/ffmpeg -i $in_file -y -f image2 -ss 00:01:53 -vframes 1 $out_file 2>&1"; //调用php的exec方法去执行脚本 exec($shell, $output, $return_val); //获取输出信息 print_r($output);
注意:当前是在虚拟机 www.ck.com 根目录,所以以上的 视频是放在当前项目的根目录。该视频资源为 变形金刚4,取的是 一分五十三秒整的视频截图。
上述脚本使用linux的管道命令将运行结果保存在$output变量中。
上述脚本运行结果:
Array( [0] => ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers [1] => built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-16) [2] => configuration: --prefix=/usr/local/ffmpeg [3] => libavutil 56. 14.100 / 56. 14.100 [4] => libavcodec 58. 18.100 / 58. 18.100 [5] => libavformat 58. 12.100 / 58. 12.100 [6] => libavdevice 58. 3.100 / 58. 3.100 [7] => libavfilter 7. 16.100 / 7. 16.100 [8] => libswscale 5. 1.100 / 5. 1.100 [9] => libswresample 3. 1.100 / 3. 1.100 [10] => Input #0, matroska,webm, from './1111.mkv': [11] => Metadata: [12] => encoder : libebml v1.3.0 + libmatroska v1.4.0 [13] => creation_time : 2015-12-05T00:18:37.000000Z [14] => Duration: 02:45:07.39, start: 0.000000, bitrate: 2335 kb/s [15] => Stream #0:0: Video: h264 (High), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default) [16] => Stream #0:1(eng): Audio: ac3, 48000 Hz, stereo, fltp, 384 kb/s (default) [17] => Metadata: [18] => title : 英语 [19] => Stream #0:2(chi): Audio: ac3, 48000 Hz, stereo, fltp, 384 kb/s [20] => Metadata: [21] => title : 国语 [22] => Stream mapping: [23] => Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native)) [24] => Press [q] to stop, [?] for help [25] => [swscaler @ 0x2531e40] deprecated pixel format used, make sure you did set range correctly [26] => Output #0, image2, to './1111.jpg': [27] => Metadata: [28] => encoder : Lavf58.12.100 [29] => Stream #0:0: Video: mjpeg, yuvj420p(pc), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 23.98 fps, 23.98 tbn, 23.98 tbc (default) [30] => Metadata: [31] => encoder : Lavc58.18.100 mjpeg [32] => Side data: [33] => cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1 [34] => frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x frame= 1 fps=0.1 q=6.9 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.00464x [35] => video:91kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown)
然后通过php的is_file方法判断是否生成成功,执行响应的逻辑。
接下来获取视频分辨率,方法有2种:
第一种:
直接获取首帧图片的width、height,
第二种:
使用正则去获取“Stream #0:0: Video: mjpeg, yuvj420p(pc), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 23.98 fps, 23.98 tbn, 23.98 tbc (default) ”里边的宽高。
附录:
http://trac.ffmpeg.org/wiki/PHP
本文为崔凯原创文章,转载无需和我联系,但请注明来自冷暖自知一抹茶ckhttp://www.cksite.cn