FFmpeg recipes

FFmpeg is an open source command line tool to convert multimedia files between formats and optionally apply a wide range of filters and effects.

Basic video conversion

ffmpeg -i input.mov output.mp4

FFmpeg can be very simple to use. It has useful defaults for quality and video/audio codecs to use, so you tend to get what you expect for well-defined file formats.

Convert a collection of 4k videos to a smaller size

for f in *.MP4 ; do ffmpeg -i "$f" -vf scale=1920:-1 -pix_fmt yuv420p -crf 22 -metadata artist="My Name" "${f%.*}sml.mp4" ; done

Find all the *.AVI files in a sub-directory structure and convert to MPEG-4

find . -type f -name "*.AVI" -print0 | while IFS= read -r -d $'\0' line ; do

ffmpeg -i "$line" -preset slow -crf 23 -pix_fmt yuv420p -movflags +faststart "${line%.*}.mp4"

done

Generate Apple-compatible HEVC H265 encoding

ffmpeg -i input.mov -c:v libx265 -crf 28 -c:a aac -b:a 128k -tag:v hvc1 output.mp4

The -c:v libx265 sets the video codec to H265.

The constant rate factor (crf) defaults to 28 for H265 encoding, so the -crf 28 option in this command is redundant, but you can make the number lower (for higher quality) or higher (for lower quality). The range is between 0 and 51, but the extreme values aren’t much use. Changing the crf setting by 6 in either direction will roughly double/halve the video bitrate.

In this example we’re setting the audio codec (-c:a aac) and bitrate (-b:a 128k). AAC is chosen by default for mp4 files, so it may not be necessary to specify the audio settings. If the source audio is already in a reasonable format and you just want to re-encode the video, then -c:a copy can be used to exactly copy the audio without re-encoding.

The -tag:v hvc1 option is the key to making the resulting video compatible with the Apple QuickTime player.

Useful 16:9 video sizes

The following frame sizes (in pixels) are in the 16:9 ratio and each dimension is divisible by 16.

Size FFmpeg crop FFmpeg scale
5376×3024 crop=5376:3024 scale=5376:3024
5120×2880 crop=5120:2880 scale=5120:2880
4864×2736 crop=4864:2736 scale=4864:2736
4608×2592 crop=4608:2592 scale=4608:2592
4352×2448 crop=4352:2448 scale=4352:2448
4096×2304 crop=4096:2304 scale=4096:2304
3840×2160 crop=3840:2160 scale=3840:2160
3584×2016 crop=3584:2016 scale=3584:2016
3328×1872 crop=3328:1872 scale=3328:1872
3072×1728 crop=3072:1728 scale=3072:1728
2816×1584 crop=2816:1584 scale=2816:1584
2560×1440 crop=2560:1440 scale=2560:1440
2304×1296 crop=2304:1296 scale=2304:1296
2048×1152 crop=2048:1152 scale=2048:1152
1920×1080 crop=1920:1080 scale=1920:1080
1280×720 crop=1280:720 scale=1280:720