FFmpeg Command-line toolkit for recording, converting, and streaming audio and video

Common Operations

The everyday operations you'll reach for most: changing formats, cutting clips, pulling out audio, and resizing video.

Converting formats (transcoding)

ffmpeg -i in.mov out.mp4                          # container + default codecs
ffmpeg -i in.mp4 -c:v libx264 -c:a aac out.mp4     # explicit codecs
ffmpeg -i in.mkv -c copy out.mp4                   # remux only, no re-encode (fast)

-c copy is much faster and lossless since it just repackages the existing streams — use it whenever you only need to change the container, not the codec.

Trimming / cutting a clip

# fast, keyframe-aligned (may be slightly off from the exact timestamp)
ffmpeg -ss 00:00:10 -i in.mp4 -t 5 -c copy clip.mp4

# frame-accurate, re-encodes (slower, exact)
ffmpeg -i in.mp4 -ss 00:00:10 -t 5 -c:v libx264 -c:a aac clip.mp4

-ss is the start time, -t is the duration to keep; use -to instead of -t to specify an absolute end timestamp rather than a duration.

Extracting audio

ffmpeg -i in.mp4 -vn -c:a copy audio.m4a     # copy the existing audio stream as-is
ffmpeg -i in.mp4 -vn -c:a libmp3lame -q:a 2 audio.mp3  # re-encode to MP3

-vn means "no video stream" in the output.

Scaling / resizing video

ffmpeg -i in.mp4 -vf scale=1280:720 out.mp4       # exact size
ffmpeg -i in.mp4 -vf scale=1280:-1 out.mp4        # fixed width, auto height (even)
ffmpeg -i in.mp4 -vf scale=-1:720 out.mp4         # fixed height, auto width

Use -2 instead of -1 for the auto dimension if the target codec requires even numbers (most H.264 encoders do).

Changing quality / bitrate

# constant quality (recommended for most cases); lower = better, 18-28 is typical
ffmpeg -i in.mp4 -c:v libx264 -crf 23 out.mp4

# explicit target bitrate instead
ffmpeg -i in.mp4 -c:v libx264 -b:v 2M out.mp4

# audio bitrate
ffmpeg -i in.mp4 -c:a aac -b:a 192k out.mp4

Creating a GIF

# quick and simple
ffmpeg -i in.mp4 -vf "fps=15,scale=480:-1" out.gif

# higher quality, two-pass palette approach
ffmpeg -i in.mp4 -vf "fps=15,scale=480:-1,palettegen" palette.png
ffmpeg -i in.mp4 -i palette.png \
  -filter_complex "fps=15,scale=480:-1[x];[x][1:v]paletteuse" out.gif

Extracting frames as images

ffmpeg -i in.mp4 -vf fps=1 frame_%04d.png       # one frame per second
ffmpeg -ss 00:00:05 -i in.mp4 -frames:v 1 shot.png  # a single frame at 5s

Concatenating clips

# files.txt contains lines like: file 'clip1.mp4'
ffmpeg -f concat -safe 0 -i files.txt -c copy joined.mp4

The concat demuxer only works cleanly with -c copy when every input shares the same codec/resolution; otherwise re-encode with the concat filter instead.

Compressing a large video

ffmpeg -i in.mp4 -c:v libx264 -crf 28 -preset slow \
  -c:a aac -b:a 128k out.mp4

-preset trades encoding speed for compression efficiency (ultrafastslower) — it does not directly affect quality, -crf does.