linux 入门(七)-ffmpeg使用
linux 入门(七)
1: 安装ffmpeg
ubuntu下安装ffmpeg:
sudo apt-get install ffmpeg
2: ffmpeg --help
ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
configuration: --prefix=/usr --extra-version=0ubuntu0.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
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
Getting help:
-h -- print basic options
-h long -- print more options
-h full -- print all options (including all format and codec specific options, very long)
-h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf
See man ffmpeg for detailed description of the options.
Print help / information / capabilities:
-L show license
-h topic show help
-? topic show help
-help topic show help
--help topic show help
-version show version
-buildconf show build configuration
-formats show available formats
-muxers show available muxers
-demuxers show available demuxers
-devices show available devices
-codecs show available codecs
-decoders show available decoders
-encoders show available encoders
-bsfs show available bit stream filters
-protocols show available protocols
-filters show available filters
-pix_fmts show available pixel formats
-layouts show standard channel layouts
-sample_fmts show available audio sample formats
-colors show available color names
-sources device list sources of the input device
-sinks device list sinks of the output device
-hwaccels show available HW acceleration methods
Global options (affect whole program instead of just one file:
-loglevel loglevel set logging level
-v loglevel set logging level
-report generate a report
-max_alloc bytes set maximum size of a single allocated block
-y overwrite output files
-n never overwrite output files
-ignore_unknown Ignore unknown stream types
-filter_threads number of non-complex filter threads
-filter_complex_threads number of threads for -filter_complex
-stats print progress report during encoding
-max_error_rate maximum error rate ratio of errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.
-bits_per_raw_sample number set the number of bits per raw sample
-vol volume change audio volume (256=normal)
Per-file main options:
-f fmt force format
-c codec codec name
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile
-t duration record or transcode "duration" seconds of audio/video
-to time_stop record or transcode stop time
-fs limit_size set the limit file size in bytes
-ss time_off set the start time offset
-sseof time_off set the start time offset relative to EOF
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string add metadata
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "", "" or "")
-apad audio pad
-frames number set the number of frames to output
-filter filter_graph set stream filtergraph
-filter_script filename read stream filtergraph description from a file
-reinit_filter reinit filtergraph on input parameter changes
-discard discard
-disposition disposition
Video options:
-vframes number set the number of video frames to output
-r rate set frame rate (Hz value, fraction or abbreviation)
-s size set frame size (WxH or abbreviation)
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number set the number of bits per raw sample
-vn disable video
-vcodec codec force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff set initial TimeCode value.
-pass n select the pass number (1 to 3)
-vf filter_graph set video filters
-ab bitrate audio bitrate (please use -b:a)
-b bitrate video bitrate (please use -b:v)
-dn disable data
Audio options:
-aframes number set the number of audio frames to output
-aq quality set audio quality (codec-specific)
-ar rate set audio sampling rate (in Hz)
-ac channels set number of audio channels
-an disable audio
-acodec codec force audio codec ('copy' to copy stream)
-vol volume change audio volume (256=normal)
-af filter_graph set audio filters
Subtitle options:
-s size set frame size (WxH or abbreviation)
-sn disable subtitle
-scodec codec force subtitle codec ('copy' to copy stream)
-stag fourcc/tag force subtitle tag/fourcc
-fix_sub_duration fix subtitles duration
-canvas_size size set canvas size (WxH or abbreviation)
-spre preset set the subtitle options to the indicated preset
3: 转码
转码视频格式只需要执行下面的命令即可:
ffmpeg -i share_5eb7ba669e87b5ab48d50107a249a1c1.mp4 test1.avi
输出如下:
ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
configuration: --prefix=/usr --extra-version=0ubuntu0.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 'share_5eb7ba669e87b5ab48d50107a249a1c1.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
comment : vid:v0300fg10000cl7lp5bc77ubt3j4km1g
encoder : Lavf58.76.100
Duration: 00:00:15.30, start: 0.000000, bitrate: 1676 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 720x1280 [SAR 1:1 DAR 9:16], 1543 kb/s, 54.28 fps, 60 tbr, 15360 tbn, 120 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> mpeg4 (native))
Stream #0:1 -> #0:1 (aac (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, avi, to 'test1.avi':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
ICMT : vid:v0300fg10000cl7lp5bc77ubt3j4km1g
ISFT : Lavf58.29.100
Stream #0:0(und): Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 720x1280 [SAR 1:1 DAR 9:16], q=2-31, 200 kb/s, 60 fps, 60 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
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:
handler_name : SoundHandler
encoder : Lavc58.54.100 libmp3lame
frame= 826 fps=514 q=31.0 Lsize= 3354kB time=00:00:15.30 bitrate=1795.0kbits/s speed=9.53x
video:3069kB audio:240kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.374900%
3: 视频压缩
指定尺寸
ffmpeg -i share_5eb7ba669e87b5ab48d50107a249a1c1.mp4 -s 720x360 test1.mp4
原视频1280x720, 大小3.3m, 修改后720x360 ,大小1.5M.
修改帧率
ffmpeg -i share_5eb7ba669e87b5ab48d50107a249a1c1.mp4 -r 20 test5.mp4
原视频帧率60, 大小3.3M. 修改帧率为20后, 大小为2.9M
限定视频大小
ffmpeg -i share_5eb7ba669e87b5ab48d50107a249a1c1.mp4 -fs 1MB test6.mp4
该命令会限制输出文件的大小为1MB.
但是-fs参数并不会保证输出的文件在1MB. 它只是告诉ffmpeg在输出文件到达1MB后停止输出.
并且, 如果该命令会导致输出视频的长度发生变化,比如之前是16s的视频, 输出只有3s.
4: 视频裁剪
ffmpeg -i share_5eb7ba669e87b5ab48d50107a249a1c1.mp4 -ss 00:00:10 -to 00:00:13 -c copy test7.mp4
- "-ss" 参数表示开始裁剪的时间,
- "-to" 参数表示裁剪到的时间
- "-c copy" 表示复制原始视频的编码格式。
5: 合并视频
将两个视频直接拼接:
创建mp4.txt文件 ,写入需要拼接的视频的路径.如:
file 'share_5eb7ba669e87b5ab48d50107a249a1c1.mp4'file 'test1.mp4'
执行命令:
ffmpeg -f concat -i mp4.txt -c copy test8.mp4
如果某一个视频有问题,如:无法播放或者文件不存在. 则输出如下:
ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1) configuration: --prefix=/usr --extra-version=0ubuntu0.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[mov,mp4,m4a,3gp,3g2,mj2 @ 0x55964a0ff340] Auto-inserting h264_mp4toannexb bitstream filterInput #0, concat, from 'mp4.txt': Duration: N/A, start: -0.046440, bitrate: 1671 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 720x1280 [SAR 1:1 DAR 9:16], 1543 kb/s, 54.28 fps, 60 tbr, 15360 tbn, 120 tbc Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s Metadata: handler_name : SoundHandlerOutput #0, mp4, to 'test8.mp4': Metadata: encoder : Lavf58.29.100 Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 720x1280 [SAR 1:1 DAR 9:16], q=2-31, 1543 kb/s, 54.28 fps, 60 tbr, 15360 tbn, 15360 tbc Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s Metadata: handler_name : SoundHandlerStream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (copy)Press [q] to stop, [?] for help[mov,mp4,m4a,3gp,3g2,mj2 @ 0x55964a118200] moov atom not found[concat @ 0x55964a0f56c0] Impossible to open 'test1.mp4'mp4.txt: Invalid data found when processing inputframe= 826 fps=0.0 q=-1.0 Lsize= 3131kB time=00:00:15.32 bitrate=1673.6kbits/s speed=1.23e+03x video:2867kB audio:240kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.780092%
此时仍然可以生成视频,只是只有第一个视频, 第二个未拼接成功.
第二种情况:
两个视频的帧率,分辨率, 编解码器不同.
虽然可以成功拼接,但是效果却差强人意:
ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1) configuration: --prefix=/usr --extra-version=0ubuntu0.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[mov,mp4,m4a,3gp,3g2,mj2 @ 0x55acf6064340] Auto-inserting h264_mp4toannexb bitstream filterInput #0, concat, from 'mp4.txt': Duration: N/A, start: -0.046440, bitrate: 1671 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 720x1280 [SAR 1:1 DAR 9:16], 1543 kb/s, 54.28 fps, 60 tbr, 15360 tbn, 120 tbc Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s Metadata: handler_name : SoundHandlerOutput #0, mp4, to 'test8.mp4': Metadata: encoder : Lavf58.29.100 Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 720x1280 [SAR 1:1 DAR 9:16], q=2-31, 1543 kb/s, 54.28 fps, 60 tbr, 15360 tbn, 15360 tbc Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s Metadata: handler_name : SoundHandlerStream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (copy)Press [q] to stop, [?] for help[mov,mp4,m4a,3gp,3g2,mj2 @ 0x55acf607d200] Auto-inserting h264_mp4toannexb bitstream filter[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675840, current: 124565; changing to 675841. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675841, current: 125589; changing to 675842. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675842, current: 126613; changing to 675843. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675843, current: 127637; changing to 675844. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675844, current: 128661; changing to 675845. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675845, current: 129685; changing to 675846. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675846, current: 130709; changing to 675847. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675847, current: 131733; changing to 675848. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675848, current: 132757; changing to 675849. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675849, current: 133781; changing to 675850. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675850, current: 134805; changing to 675851. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675851, current: 135829; changing to 675852. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675852, current: 136853; changing to 675853. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675853, current: 137877; changing to 675854. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675854, current: 138901; changing to 675855. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675855, current: 139925; changing to 675856. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675856, current: 140949; changing to 675857. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675857, current: 141973; changing to 675858. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675858, current: 142997; changing to 675859. This may result in incorrect timestamps in the output file.[mp4 @ 0x55acf609df40] Non-monotonous DTS in output stream 0:1; previous: 675859, current: 144021; changing to 675860. This may result in incorrect timestamps in the output file.frame= 903 fps=0.0 q=-1.0 Lsize= 3219kB time=00:01:44.78 bitrate= 251.7kbits/s speed=4.89e+03x video:2951kB audio:244kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.774971%
生成的视频时长与 两个视频拼接的长度不一致.
我们直接将相同的两个视频拼接,此时:
file 'share_5eb7ba669e87b5ab48d50107a249a1c1.mp4'file 'share_5eb7ba669e87b5ab48d50107a249a1c1.mp4'
最终可以拼接成功.
如果需要解决这个问题,则需要我们使用ffmpeg的其他选项进行转码和调整,保证两个视频的格式统一后,进行拼接.
本文由博客一文多发平台 OpenWrite 发布!