A list of useful commands for the FFmpeg command line tool.
Download FFmpeg: https://www.ffmpeg.org/download.html
Full documentation: https://www.ffmpeg.org/ffmpeg.html
ffmpeg -i in.mp4 out.avi
ffmpeg -i in.mkv -c:v copy -c:a copy out.mp4
Use the crf
(Constant Rate Factor) parameter to control the output quality. The lower crf, the higher the quality (range: 0-51). The default value is 23, and visually lossless compression corresponds to -crf 18
. Use the preset
parameter to control the speed of the compression process. Additional info: https://trac.ffmpeg.org/wiki/Encode/H.264
ffmpeg -i in.mp4 -preset slower -crf 18 out.mp4
Without re-encoding:
ffmpeg -ss [start] -i in.mp4 -t [duration] -c copy out.mp4
-ss
specifies the start time, e.g.00:01:23.000
or83
(in seconds)-t
specifies the duration of the clip (same format).- Recent
ffmpeg
also has a flag to supply the end time with-to
. -c
copy copies the first video, audio, and subtitle bitstream from the input to the output file without re-encoding them. This won't harm the quality and make the command run within seconds.
With re-encoding:
If you leave out the -c copy
option, ffmpeg
will automatically re-encode the output video and audio according to the format you chose. For high quality video and audio, read the x264 Encoding Guide and the AAC Encoding Guide, respectively.
For example:
ffmpeg -ss [start] -i in.mp4 -t [duration] -c:v libx264 -c:a aac -strict experimental -b:a 128k out.mp4
To copy the video from in0.mp4 and audio from in1.mp4:
ffmpeg -i in0.mp4 -i in1.mp4 -c copy -map 0:0 -map 1:1 -shortest out.mp4
- With -c copy the streams will be
stream copied
, not re-encoded, so there will be no quality loss. If you want to re-encode, see FFmpeg Wiki: H.264 Encoding Guide. - The
-shortest
option will cause the output duration to match the duration of the shortest input stream. - See the
-map
option documentation for more info.
First, make a text file.
file 'in1.mp4'
file 'in2.mp4'
file 'in3.mp4'
file 'in4.mp4'
Then, run ffmpeg
:
ffmpeg -f concat -i list.txt -c copy out.mp4
Delay video by 3.84 seconds:
ffmpeg -i in.mp4 -itsoffset 3.84 -i in.mp4 -map 1:v -map 0:a -vcodec copy -acodec copy out.mp4
Delay audio by 3.84 seconds:
ffmpeg -i in.mp4 -itsoffset 3.84 -i in.mp4 -map 0:v -map 1:a -vcodec copy -acodec copy out.mp4
Use the libass library (make sure your ffmpeg install has the library in the configuration --enable-libass
).
First convert the subtitles to .ass format:
ffmpeg -i sub.srt sub.ass
Then add them using a video filter:
ffmpeg -i in.mp4 -vf ass=sub.ass out.mp4
To extract all frames from between 1 and 5 seconds, and also between 11 and 15 seconds:
ffmpeg -i in.mp4 -vf select='between(t,1,5)+between(t,11,15)' -vsync 0 out%d.png
To extract one frame per second only:
ffmpeg -i in.mp4 -fps=1 -vsync 0 out%d.png
Rotate 90 clockwise:
ffmpeg -i in.mov -vf "transpose=1" out.mov
For the transpose parameter you can pass:
0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip
Use -vf "transpose=2,transpose=2"
for 180 degrees.
- Locate the playlist file, e.g. using Chrome > F12 > Network > Filter: m3u8
- Download and concatenate the video fragments:
ffmpeg -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4
If you get a "Protocol 'https not on whitelist 'file,crypto'!" error, add the protocol_whitelist
option:
ffmpeg -protocol_whitelist "file,http,https,tcp,tls" -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4
To replace the first 90 seconds of audio with silence:
ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='lte(t,90)':volume=0" out.mp4
To replace all audio between 1'20" and 1'30" with silence:
ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='between(t,80,90)':volume=0" out.mp4
Deinterlacing using "yet another deinterlacing filter".
ffmpeg -i in.mp4 -vf yadif out.mp4
Parameters: -r
marks the image framerate (inverse time of each image); -vf fps=25
marks the true framerate of the output.
ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4
- Extract all frames:
ffmpeg -i input.mp4 thumb%04d.jpg -hide_banner
- Extract a frame each second:
ffmpeg -i input.mp4 -vf fps=1 thumb%04d.jpg -hide_banner
- Extract only one frame:
ffmpeg -i input.mp4 -ss 00:00:10.000 -vframes 1 thumb.jpg
ffmpeg -i in.mov -vf "drawtext=fontfile=arial.ttf: text=%{n}: x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000099: fontsize=72" -y out.mov
ffmpeg -i in.mp4 -map_metadata -1 -metadata title="My Title" -c:v copy -c:a copy out.mp4
https://ffmpeg.lav.io/ is an interactive resource to compose FFmpeg actions.
The D.mkv file is not downloaded. I created it in the VideoPad program. I combined 24 parts. This is due to incorrect playback on the smartTV. According to the playlist, all parts will be played at once, but the Subtitle must be set for each.
After linking to a large file, I needed to sync the subtitles in the SubtitleEdit program. But the Waveform soundtrack will only be created by 12:25:29. In consultation with the author of the SE, I found out that it will terminate FFmpeg earlier.
Here is the output of ffprobe.
D:\FF>ffprobe d.mkv
ffprobe version N-90707-g01170e9db0 Copyright (c) 2007-2018 the FFmpeg developers
built with gcc 7.1.0 (GCC)
configuration: --arch=x86_64 --target-os=mingw32 --cross-prefix=/home/jacek/ffenv/04/jpff/cross_compilers/mingw-w64-x86_64/bin/x86_64-w64-mingw32- --pkg-config=pkg-config --pkg-config-flags=--static --enable-gray --enable-version3 --disable-debug --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --disable-w32threads --extra-cflags=-DPTW32_STATIC_LIB --extra-libs=-lstdc++ --enable-gpl --enable-version3 --enable-libx264 --enable-libx265 --enable-libxvid --enable-libmp3lame --enable-libfdk-aac --enable-libopus --enable-zlib --enable-fontconfig --enable-libass --enable-iconv --enable-libfreetype --extra-cflags=-DLIBTWOLAME_STATIC --extra-cflags=-DMODPLUG_STATIC --extra-cflags=-DCACA_STATIC --extra-cflags='-mtune=generic' --extra-cflags=-O3 --enable-shared --disable-static --prefix=/home/jacek/ffenv/04/jpff/win64/ffmpeg_git_with_fdk_aac_xp_compat_shared
libavutil 56. 13.100 / 56. 13.100
libavcodec 58. 17.100 / 58. 17.100
libavformat 58. 11.101 / 58. 11.101
libavdevice 58. 2.100 / 58. 2.100
libavfilter 7. 15.100 / 7. 15.100
libswscale 5. 0.102 / 5. 0.102
libswresample 3. 0.101 / 3. 0.101
libpostproc 55. 0.100 / 55. 0.100
Input #0, matroska,webm, from 'd.mkv':
Metadata:
encoder : libebml v1.4.2 + libmatroska v1.6.4
creation_time : 2022-01-31T22:57:04.000000Z
Duration: 17:08:02.92, start: 0.000000, bitrate: 874 kb/s
Stream #0:0: Video: h264 (Main), yuv420p(progressive), 800x600 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
Metadata:
BPS : 712784
DURATION : 17:08:02.920000000
NUMBER_OF_FRAMES: 1542073
NUMBER_OF_BYTES : 5495827574
_STATISTICS_WRITING_APP: mkvmerge v64.0.0 ('Willows') 64-bit
_STATISTICS_WRITING_DATE_UTC: 2022-01-31 22:57:04
_STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
Stream #0:1(cze): Audio: ac3, 44100 Hz, stereo, fltp, 160 kb/s (default)
Metadata:
BPS : 160000
DURATION : 17:08:02.625000000
NUMBER_OF_FRAMES: 1770966
NUMBER_OF_BYTES : 1233652506
_STATISTICS_WRITING_APP: mkvmerge v64.0.0 ('Willows') 64-bit
_STATISTICS_WRITING_DATE_UTC: 2022-01-31 22:57:04
_STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
D:\FF>