ffmpeg 是一个处理媒体文件的命令行工具 (command line based) 。它是一个拥有非常多功能的框架,并且因为他是开源的,很多知名的工具如 VLC,YouTube, iTunes 等等,都是再其之上开发出来的。我们项目需要针对视频文件进行缩略图截帧操作,所以记述一下安装过程。
8.1 前提安装yasm[root@ecs-s6 J2EETool]# wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
[root@ecs-s6 J2EETool]# tar -zxvf yasm-1.3.0.tar.gz
[root@ecs-s6 J2EETool]# cd yasm-1.3.0
[root@ecs-s6 yasm-1.3.0]# ./configure
[root@ecs-s6 yasm-1.3.0]# make
[root@ecs-s6 yasm-1.3.0]# make install
8.2 安装ffmpeg4.2
[root@ecs-s6 J2EETool]# wget http://ffmpeg.org/releases/ffmpeg-4.2.tar.gz
[root@ecs-s6 J2EETool]# tar -zxvf ffmpeg-4.2.tar.gz
[root@ecs-s6 J2EETool]# cd ffmpeg-4.2
[root@ecs-s6 ffmpeg-4.2]# ./configure
[root@ecs-s6 ffmpeg-4.2]# make
[root@ecs-s6 ffmpeg-4.2]# make install
ffmpeg的编译较长,中间请耐心等待。
8.3 安装结果确认:ffmpeg -version[root@ecs-s6 ffmpeg-4.2]# ./ffmpeg -version
ffmpeg version 4.2 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-39)
configuration:
libavutil 56. 31.100 / 56. 31.100
libavcodec 58. 54.100 / 58. 54.100
libavformat 58. 29.100 / 58. 29.100
libavdevice 58. 8.100 / 58. 8.100
libavfilter 7. 57.100 / 7. 57.100
libswscale 5. 5.100 / 5. 5.100
libswresample 3. 5.100 / 3. 5.100
[root@ecs-s6 ffmpeg-4.2]#
8.4 ffmpeg命令使用示例
# 视频截图:
ffmpeg -i /home/test/CZ002.mp4 -y -f image2 -ss 8 -frames 1 ./CZ002.jpg
# 从视频前10s中提取图像,1s提取一帧
ffmpeg -i /home/test/CZ002.mp4 -t 10 -r 1 -f image2 pic-%03d.jpeg
# 音频转换16位
ffmpeg -i /home/test/REC002.WAV -ar 16000 -ab 24k -ac 1 /home/test/RECORD00224.wav
ffmpeg -i /home/test/REC002.WAV -ar 16000 -ab 64k -ac 1 /home/test/RECORD00264.wav
执行命令结果:
[root@ecs-s6 ffmpeg-4.2]# ./ffmpeg -i /home/test/CZ002.mp4 -y -f image2 -ss 8 -frames 1 /home/test/CZ002.jpg
ffmpeg version 4.2 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-39)
configuration:
libavutil 56. 31.100 / 56. 31.100
libavcodec 58. 54.100 / 58. 54.100
libavformat 58. 29.100 / 58. 29.100
libavdevice 58. 8.100 / 58. 8.100
libavfilter 7. 57.100 / 7. 57.100
libswscale 5. 5.100 / 5. 5.100
libswresample 3. 5.100 / 3. 5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/test/CZ002.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : www.aliyun.com - Media Transcoding
Duration: 00:00:58.35, start: 0.000000, bitrate: 1615 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 1485 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 0x35497c0] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to '/home/test/CZ002.jpg':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.29.100
Stream #0:0(und): Video: mjpeg, yuvj420p(pc), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc58.54.100 mjpeg
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
frame= 1 fps=0.0 q=5.4 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.0811x
video:47kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[root@ecs-s6 ffmpeg-4.2]#
[root@ecs-s6 test]# pwd
/home/test
[root@ecs-s6 test]# ll
total 11556
-rw-r--r-- 1 root root 48158 Jan 28 17:59 CZ002.jpg
-rw-r--r-- 1 root root 11780658 Nov 7 11:36 CZ002.mp4