FFmpeg cheatsheet
My personal list of frequently used FFmpeg commands. See the FFmpeg documentation for more examples and detailed explanations.
I find FFmpeg convenient for simple tasks, but for more complicated projects, I prefer Kdenlive, OpenShot, or the Samsung Video Editor.
Trim
Trim input.mp4
so that output.mp4
starts at 13.5 seconds and ends at 1 minute:
ffmpeg -ss 00:00:13.5 -i input.mp4 -t 00:00:46.5 -c copy output.mp4
This runs quickly because -c copy
says not to re-encode anything.
After checking that output.mp4
is correctly trimmed, run with re-encoding:
ffmpeg -ss 00:00:13.5 -i input.mp4 -t 00:00:46.5 output-small.mp4
Compare the size of the two outputs and choose the smaller one. Re-encoding often results in smaller file size with the same video quality.
Concatenate
Name the files to be concatenated in order like partial-1.mp4
, partial-2.mp4
, and partial-3.mp4
.
A nice one liner for concatenating:
ffmpeg -f concat -safe 0 -i <(for f in partial-*; do echo "file '$PWD/$f'"; done) -c copy output.mp4
As with trimming, removing -c copy
may reduce the video size without losing quality.
Export Individual Frames
Export one frame at 16 minutes:
ffmpeg -ss 00:16:00 -i input.mp4 -frames:v 1 image.jpg
Check the quality of the image. If it’s not good enough, export a non-compressed image:
ffmpeg -ss 00:16:00 -i input.mp4 -frames:v 1 image.png
Check that the image is the right frame. If not, export several at once to find the correct one:
ffmpeg -ss 00:16:00 -i input.mp4 -frames:v 100 image%03d.jpg
Reduce Resolution
Reduce the resolution from 1920x1080 to 960x540:
ffmpeg -i input.mp4 -s 960x540 output.mp4
Don’t try -c copy
on this one, because it won’t reduce the
resolution or the size of the file.
Extract Audio, Edit, Put Back Together
Extract audio:
ffmpeg -i input.mp4 -map a -c copy output.aac
More details on Stack Overflow. After editing the audio, put the audio and video back together:
ffmpeg -i output-edited.aac -i input.mp4 -map 0:a -map 1:v -c copy output.mp4
More -map
examples in the FFmpeg documentation.
If the audio and video come from different sources, one of them might need to start earlier than the other. For example, skip the first 2.7 seconds of the video input to line the two streams up.
ffmpeg -i audio.aac -ss 00:02.7 -i video.mp4 -map 0:a -map 1:v -c copy output.mp4
Crop
Play the video with a crop filter to test the dimensions:
ffplay -i input.mp4 -vf "crop=out_w:out_h:x:y" -c:a copy
The dimensions are as follows:
out_w
the width of the output videoout_h
the height of the output videox
andy
using the input video coordinates that start at (0,0) in the top left corner, (x,y) is the top left corner of the output video.
With the correct coordinates chosen, produce an output file:
ffmpeg -i input.mp4 -vf "crop=out_w:out_h:x:y" -c:a copy output.mp4
The crop filter re-encodes the video, so only the audio can be copied.
More details and examples: