本文采用知识共享署名 4.0 国际许可协议进行许可,转载时请注明原文链接,图片在使用时请保留全部内容,可适当缩放并在引用处附上图片所在的文章链接。
安装
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install ffmpeg
查看版本
ffmpeg -version
输出
ffmpeg version 3.4.8-0ubuntu0.2 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
configuration: --prefix=/usr --extra-version=0ubuntu0.2 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
ffmpeg -i GstTest.mp4 output.avi
输出:
ffmpeg version 4.2.4-1ubuntu0.1 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 9 (Ubuntu 9.3.0-10ubuntu2)
configuration: --prefix=/usr --extra-version=1ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
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
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 5.100 / 5. 5.100
libswresample 3. 5.100 / 3. 5.100
libpostproc 55. 5.100 / 55. 5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'GstTest.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42isom
creation_time : 2020-07-26T12:09:43.000000Z
Duration: 00:00:09.17, start: 0.000000, bitrate: 900 kb/s
Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 48 kb/s (default)
Metadata:
creation_time : 2020-07-26T12:09:43.000000Z
Stream #0:1(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 960x544, 852 kb/s, 28.91 fps, 28.92 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2020-07-26T12:09:43.000000Z
encoder : JVT/AVC Coding
Stream mapping:
Stream #0:1 -> #0:0 (h264 (native) -> mpeg4 (native))
Stream #0:0 -> #0:1 (aac (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, avi, to 'output.avi':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: mp42isom
ISFT : Lavf58.29.100
Stream #0:0(und): Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 960x544, q=2-31, 200 kb/s, 28.92 fps, 28.92 tbn, 28.92 tbc (default)
Metadata:
creation_time : 2020-07-26T12:09:43.000000Z
encoder : Lavc58.54.100 mpeg4
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
Stream #0:1(und): Audio: mp3 (libmp3lame) (U[0][0][0] / 0x0055), 44100 Hz, stereo, fltp (default)
Metadata:
creation_time : 2020-07-26T12:09:43.000000Z
encoder : Lavc58.54.100 libmp3lame
frame= 264 fps=145 q=31.0 Lsize= 732kB time=00:00:09.19 bitrate= 651.8kbits/s speed=5.04x
video:563kB audio:144kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.440192%
ffmpeg {1} {2} -i {3} {4} {5}
ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...
$ ffmpeg \
[全局参数] \
[输入文件参数] \
-i [输入文件] \
[输出文件参数] \
[输出文件]
ffmpeg -i ../20200910162907037_dir.h264 -r 10 -f image2 h264_%03d.png
ffmpeg -i ../20200910162907037_dir.h264 -r 10 -f image2 h264_%03d.jpg
ffmpeg -i ../20200909145247237_dir.h264 -r 25 -f image2 h264_%03d.jpg
ffmpeg -i ../*.h264 -r 25 -f image2 h264_%03d.png
#!/bin/bash
# Convert a series of png images to into an mp4 movie.
#
# On OSX you can use "brew install ffmpeg" to install ffmpeg
# -r 10 means 10 fps
# -crf 20 determines the quality 0 = lossless, 23 = default
# -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" is to deal with widths and heights not divisible by 2
# -pix_fmt yuv420p is for compatibility with QuickTime and other old players. Drop it if you only use VLC or something modern.
# -i "movie%04d.png" is a placeholder for movie0000.png, movie0001.png, ...
ffmpeg -r 10 -i "movie%04d.png" -c:v libx264 -crf 20 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -pix_fmt yuv420p imovie2.mp4
ffmpeg -i ./20200909080831116_dir.h264 -vf "select=between(n\,1150\,1450)" -y -acodec copy ./output.h264
# "select=between(n\,1150\,1450)" 1150 起始帧 1450 结束帧
# 下面的命令,可以从时间为00:00:15开始,截取5秒钟的视频。
ffmpeg -ss 00:00:15 -t 00:00:05 -i input.mp4 -vcodec copy -acodec copy output.mp4
# -ss表示开始切割的时间,-t表示要切多少。上面就是从15秒开始,切5秒钟出来。
ffmpeg -i output.h264 -vcodec copy -f mp4 test.mp4
ffmpeg -i input.avi output.mp4
ffmpeg -i input.mp4 output.ts
ffmpeg -i input.mp4 -vcodec copy -an output.mp4
ffmpeg -i output.h264 -i T1-OUT.oni.h264 -filter_complex "[0:v]pad=iw*2:ih*1[a];[a][1:v]overlay=w" out.mp4
ffmpeg -i input.mp4 -vcodec copy -an output.mp4
ffmpeg -i test.mp4 -acodec copy -vn output.aac
# 上面的命令,默认mp4的audio codec是aac,如果不是,可以都转为最常见的aac。
ffmpeg -i test.mp4 -acodec aac -vn output.aac
码率控制对于在线视频比较重要。因为在线视频需要考虑其能提供的带宽。
那么,什么是码率?很简单:
bitrate = file size / duration
比如一个文件20.8M,时长1分钟,那么,码率就是:
biterate = 20.8M bit/60s = 20.8*1024*1024*8 bit/60s= 2831Kbps
一般音频的码率只有固定几种,比如是128Kbps,
那么,video的就是
video biterate = 2831Kbps -128Kbps = 2703Kbps。
那么ffmpeg如何控制码率。
ffmpg控制码率有3种选择,-minrate -b:v -maxrate
-b:v主要是控制平均码率。
比如一个视频源的码率太高了,有10Mbps,文件太大,想把文件弄小一点,但是又不破坏分辨率。
ffmpeg -i input.mp4 -b:v 2000k output.mp4
上面把码率从原码率转成2Mbps码率,这样其实也间接让文件变小了。目测接近一半。
不过,ffmpeg官方wiki比较建议,设置b:v时,同时加上 -bufsize
-bufsize 用于设置码率控制缓冲器的大小,设置的好处是,让整体的码率更趋近于希望的值,减少波动。(简单来说,比如1 2的平均值是1.5, 1.49 1.51 也是1.5, 当然是第二种比较好)
ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k output.mp4
-minrate -maxrate就简单了,在线视频有时候,希望码率波动,不要超过一个阈值,可以设置maxrate。
ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k -maxrate 2500k output.mp4