FFmpeg Cheatsheet
A cheatsheet for common video processing operations in FFmpeg
Operations
Use the -y
flag to override the output file if it exists.
Audio-video sync
# Delay audio by 3 seconds
$ ffmpeg -i input.mov -itsoffset 3 -i input.mov -map 0:v -map 1:a -codec:a copy -codec:v copy output.mov
# Delay video by 3 seconds (ie. advance audio by 3 seconds)
$ ffmpeg -i input.mov -itsoffset 3 -i input.mov -map 1:v -map 0:a -codec:a copy -codec:v copy output.mov
- The second
-i
flag must come immediately after the-itsoffset
flag.
Crop
# Crop to width 360, height 640
$ ffmpeg -i input.mov -filter:v 'crop=360:640:0:0' -codec:a copy output.mov
# Crop to width 360, height 640, starting from coordinates (10, 20)
$ ffmpeg -i input.mov -filter:v 'crop=360:640:10:20' -codec:a copy output.mov
Format
# Convert to GIF
$ ffmpeg -i input.mov output.gif
# Convert from GIF
$ ffmpeg -i input.gif output.mov
# Convert between non-GIF formats
$ ffmpeg -i input.mov -codec:v copy -codec:a copy output.mp4
Frame rate
# Change the frame rate to 12
$ ffmpeg -i input.mov -filter:v 'fps=fps=12' -codec:a copy output.mov
Strip audio
# Remove audio
$ ffmpeg -i input.mov -codec:v copy -an output.mov
Resize
# Resize to width 360, height 640
$ ffmpeg -i input.mov -filter:v 'scale=360:640' -codec:a copy output.mov
# Resize to width 360, maintaining the aspect ratio
$ ffmpeg -i input.mov -filter:v 'scale=360:-1' -codec:a copy output.mov
# Resize to height 640, maintaining the aspect ratio
$ ffmpeg -i input.mov -filter:v 'scale=-1:640' -codec:a copy output.mov
- Set either
width
orheight
to-1
to maintain the aspect ratio.
Reverse
# Reverse
$ ffmpeg -i input.mov -filter:v 'reverse' -filter:a 'areverse' output.mov
Rotate
# Rotate 90 degrees clockwise
$ ffmpeg -i input.mov -filter:v 'transpose=1' -codec:a copy output.mov
# Rotate 90 degrees counter-clockwise
$ ffmpeg -i input.mov -filter:v 'transpose=2' -codec:a copy output.mov
# Rotate 180 degrees
$ ffmpeg -i input.mov -filter:v 'transpose=1,transpose=1' -codec:a copy output.mov
Speed
# Quarter the speed
$ ffmpeg -i input.mov -filter:v 'setpts=4*PTS' -filter:a 'atempo=0.5,atempo=0.5' output.mov
# Halve the speed
$ ffmpeg -i input.mov -filter:v 'setpts=2*PTS' -filter:a 'atempo=0.5' output.mov
# Double the speed
$ ffmpeg -i input.mov -filter:v 'setpts=0.5*PTS' -filter:a 'atempo=2' output.mov
# Quadruple the speed
$ ffmpeg -i input.mov -filter:v 'setpts=0.25*PTS' -filter:a 'atempo=2,atempo=2' output.mov
-
Use the formula
1 ÷ speed
to compute the value ofsetpts
.- Half the speed:
setpts=2*PTS
since1 ÷ 0.5 = 2
. - Double the speed:
setpts=0.5*PTS
since1 ÷ 2 = 0.5
.
- Half the speed:
-
The value of each
atempo
filter must be between 0.5 and 2.- Quarter the speed:
atempo=0.5,atempo=0.5
since0.5 × 0.5 = 0.25
. - Quadruple the speed:
atempo=2,atempo=2
since2 × 2 = 4
.
- Quarter the speed:
Subtitles
# Write subtitles into video
$ ffmpeg -i input.mov -filter:v 'subtitles=subtitles.srt' -codec:a copy output.mov
# Write subtitles into video, with custom subtitle styles
$ ffmpeg -i input.mov -filter:v "subtitles=subtitles.srt:force_style='FontName=Menlo Bold,Fontsize=18'" -codec:a copy output.mov
Trim
# Trim from 0:05 to 0:10
$ ffmpeg -ss 0:05 -to 0:10 -i input.mov -codec:v copy -codec:a copy output.mov
# Trim from 0:05 to the end of the video
$ ffmpeg -ss 0:05 -i input.mov -codec:v copy -codec:a copy output.mov
- The
-ss
and-to
flags must come before the-i
flag.
Volume
# Halve the volume
$ ffmpeg -i input.mov -codec:v copy -filter:a 'volume=0.5' output.mov
# Double the volume
$ ffmpeg -i input.mov -codec:v copy -filter:a 'volume=2' output.mov
Relevant CLI flags
ffmpeg
-an
-ss <timestamp>
-to <timestamp>
-itsoffset <offset>
-i <input>
-map <stream>
-codec:a <codec>
-codec:v <codec>
-filter:a <filtergraph>
-filter:v <filtergraph>
-y
<output>