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 "pal-", "ntsc-" or "film-")
-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 釋出!