ffmpeg cheat sheet

Julian Rakuschek - 2023-04-11



cheat sheet

# Basic Conversion
ffmpeg -i input.mp4 output.avi

# Trim Video
ffmpeg -i input.mp4 -ss 00:19:39 -to 00:20:31 -c copy output.mp4
# -c copy makes it fast, but it is not that precise
# Therefore omit for small files

# Convert single video to standard h264 video Codec
ffmpeg -i input.mp4 -vcodec libx264 -acodec aac output.mp4

# Compress Video
ffmpeg -i input.mp4 -vcodec libx264 -crf 22 output.mp4
# Vary the CRF (Constant Rate Factor) between around 18 and 24 — the lower, the higher the bitrate.
# Scale the resolution down and compress bitrate:
ffmpeg -i input.mp4 -vf scale=-1:480 -vcodec libx264 -crf 22 output.mp4

# First make Text File mylist.txt with this content:
# file './path/to/in1.mp4'
# file './path/to/in2.mp4'
# file './path/to/in3.mp4'
# Then run:
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4

# Convert to gif
ffmpeg -i in.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -f gif out.gif

# Delay video by 3.84 seconds:
ffmpeg -i input.mp4 -itsoffset 3.84 -i input.mp4 -map 1:v -map 0:a -vcodec copy -acodec copy output.mp4

# Delay audio by 3.84 seconds:
ffmpeg -i input.mp4 -itsoffset 3.84 -i input.mp4 -map 0:v -map 1:a -vcodec copy -acodec copy output.mp4

# Extracting frames of video

# To extract all frames from between 1 and 5 seconds, and also between 11 and 15 seconds:
ffmpeg -i input.mp4 -vf select='between(t,1,5)+between(t,11,15)' -vsync 0 output%d.png
# To extract one frame per second only:
ffmpeg -i input.mp4 -fps=1 -vsync 0 output%d.png
# 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

# Make a timelapse from a video (0.1 means 10% of Video length, -an to remove audio)
ffmpeg -i input.mp4 -framerate 60 -video_size 1280x720 -f x11grab -i :0 -vf "setpts=0.1*PTS" -an output.mp4 

# Merge audio and video file
ffmpeg -i input.mp4 -i input.mp3 -map 0:v -map 1:a -c copy -y output.mp4

# Remove Audio
ffmpeg -i input.mp4 -c copy -an output.mp4

# Extract Audio
ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac

# Crop Video
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4

# Sources:
# https://gist.github.com/steven2358/ba153c642fe2bb1e47485962df07c730
# https://catswhocode.com/ffmpeg-commands/