FFMPEG Commands

Rescale a video to 480p

ffmpeg -i input.mp4 -vf "scale=-2:480" output.mp4

Alternative syntax:

ffmpeg -i input.mp4 "-filter:v" "scale=-2:480" output.mp4

The "-2" means to scale with the aspect ratio but to keep to numbers that MP4 can still handle fine.

Rescale a video to half its vertical resolution

ffmpeg -i input.mp4 -vf "scale=-2:ih/2" output.mp4

The "-filter:v" syntax should work too.

This article has more information.

Avoid upscaling when resizing

ffmpeg has a "min()" function to prevent upscaling arbitrary videos when resizing:

ffmpeg -i input.mp4 -vf "scale=-2:'min(480,ih)'" output.mp4

(The "-filter:v" syntax should work too.)

See the ffmpeg wiki.

Converting to MP4

The video codec we want is libx264. For the audio codec, on older ffmpeg builds, use either -c:a aac -strict experimental (or -c:a aac -strict -2) or -c:a libvo_aacenc. For newer ffmpeg builds, just use -c:a aac.

ffmpeg -i input.mpg -c:v libx264 -c:a libvo_aacenc output.mp4

OR

ffmpeg -i input.mpg -c:v libx264 -c:a aac -strict -2 output.mp4

OR, on new builds,

ffmpeg -i input.mpg -c:v libx264 -c:a aac output.mp4

h264 MP4s can be tuned to a variety of values for different uses: see

h264 Tune

Converting to h265 MP4

The video codec we want is libx265. For the audio codec, the same rules apply as for straight MP4.

ffmpeg -i input.mp4 -c:v libx265 -c:a aac -strict -2 output.mp4

h265 is useful for dropping the file size, but is still not that widely supported, with AV1 surprisingly having wider web-browser support. See ffmpeg-small-files for more.

GIFs

GIFs are larger than MP4s, so it may be beneficial to convert them for some platforms:

ffmpeg -i input.gif -pix_fmt yuv420p -c:v libx264 output.mp4

The -pix_fmt yuv420p is necessary as GIFs use a different colour format(?).

Note that some GIFs have weird frame-rates and frame-delays, which can be fixed with:

 ffmpeg -i input.gif -filter_complex "[0:v] fps=15" -vsync 0 -pix_fmt yuv420p  -c:v libx264 output.mp4

(You may need to adjust the fps to a different number. If you know the frame-delay, the fps is calculated as (1/delay).)

See this article for more details.

Fixing annoying formats

Some systems won't play h264 "High 4:4:4", so this must be converted to h264 main like this:

ffmpeg -i input.mp4 -profile:v main -pix_fmt yuv420p output.mp4

It may sometimes help to throw in:

-vf "scale=2*trunc(iw/2):-2"

with some annoying formats to ensure width and height are multiples of two as sometimes this can be off for some formats, yet is necessary for h264.

See this StackOverflow question

The filter

"setsar=1:1"

may occasionally be needed if the aspect ratio comes out wonky despite the inputs seeming to be correct.

Cropping Letterboxing

ffmpeg -i "input.avi" -filter:v "crop=iw-400:ih-40,scale=-2:720" -pix_fmt yuv420p output.mp4

Crops 200px from both the left and right of the video, plus 20px from both the top and bottom, then rescales to 720p.

From this StackOverflow answer.

Letterbox to 320x240

ffmpeg -i input.mp4 -vf "scale=320:240:force_original_aspect_ratio=decrease,pad=320:240:(ow-iw)/2:(oh-ih)/2" -c:v copy -c:a copy output.mp4

As explained in the ffmpeg wiki
With more explained in this SuperUser answer