What's This?¶
This for my own use. Use it if you want to. Just documenting the use of FFmpeg on Linux.
Installation¶
First we will need to install FFmpeg
. I will be doing it on ArchLinux since that's what I use. Look for packages on your distro if you don't use it.
Now that we have it, it's time for the cheat sheet.
.// The Cheat Sheet \\.
Basic conversion¶
Remux an MKV file into MP4¶
High-quality encoding¶
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.
Trimming¶
Without re-encoding:
--ss
specifies the start time, e.g. 00:01:23.000
or 83
(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
Mux video and audio from another video¶
To copy the video from in0.mp4 and audio from in1.mp4:
- With -c copy the streams will bestream 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.
Concat demuxer¶
First, make a text file.
Then, runffmpeg
:
Delay audio/video¶
Delay video by 3.84 seconds:
Delay audio by 3.84 seconds:Burn subtitles¶
Use the libass library (make sure your ffmpeg install has the library in the configuration --enable-libass
).
First convert the subtitles to .ass format:
Then add them using a video filter:Extract the frames from a video¶
To extract all frames from between 1 and 5 seconds, and also between 11 and 15 seconds:
To extract one frame per second only:
Rotate a video¶
Rotate 90 clockwise:
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.
Download "Transport Stream" video streams¶
- Locate the playlist file, e.g. using Chrome > F12 > Network > Filter: m3u8
- Download and concatenate the video fragments:
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
Mute some of the audio¶
To replace the first 90 seconds of audio with silence:
To replace all audio between 1'20" and 1'30" with silence:
Deinterlace¶
Deinterlacing using "yet another deinterlacing filter".
Create a video slideshow from images¶
Parameters: -r
marks the image framerate (inverse time of each image); -vf fps=25
marks the true framerate of the output.
Extract images from a video¶
- 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
Display the frame number on each frame¶
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
Metadata: Change the title¶
Wrap up¶
That's it. If you have any additional tips send them my way so I can add to this thing. The more the merrier. Useful for all.