If you intend to distribute your video content via the web then you will want to minimize the size of your video file in order to incur the least cost in terms of bandwidth. There are three strategies that will lead to lower video file sizes: using more efficient or aggresive compression, reducing the resolution of the file, and reducing the frame rate of the video.
For web distribution we use the MP4 file format with MPEG-4 part 2 encoded video and MP3 encoded audio. The MP4 file format is specified as part of the MPEG-4 suite of standards and is playable by a broad range of media players, including iTunes and the iPod in case you’re podcasting your video. In order to transcode your video into the MP4 format you simply specify the .mp4 file extension for the output file and ffmpeg will understand what you want.
$ ffmpeg -i input.avi output.mp4
If for some reason you want to use a non-standard filename extension, then you must make the file format explicit:
$ ffmpeg -i input.avi -f mp4 output.ext
While the H.264 codec would be ideal, ffmpeg does not yet support encoding in H.264, so instead we use the MPEG-4 part 2 Advanced Simple Profile. The best compression ratio for audio in the MP4 format is still achieved via MP3 compression. The audio codec and video codec can be specified via the -acodec and -vcodec options.
$ ffmpeg -i input.avi -acodec mp3 -vcodec mpeg4 output.mp4
The -b option sets the video bitrate, and the -ab option sets the audio bitrate. By default ffmpeg uses 200 kb/s video bitrate for the mpeg4 codec and 64 kb/s audio bitrate for the mp3 codec.
The most common resolution for web distribution is 320×240; a 4:3 aspect ratio with square pixels. Almost all the video on archive.org is in that resolution, as are MP4 (iPod) video downloads from google video. To force this resolution, use the -s option:
$ ffmpeg -i input.avi -acodec mp3 -vcodec mpeg4 -s 320×240 output.mp4
Note that the same video encoded at standard resolution and at 320×240 will have roughly similar file sizes. This is because the codec’s bitrate is constant: bitrate times time equals file size. Reducing the video resolution allows you to use a lower bitrate with fewer compression artifacts.
If your source material is interlaced, add the -deinterlace option. I don’t know how ffmpeg deinterlaces video, and it’s likely not a great method, e.g. choosing one of the fields and scaling it up to a 4:3 aspect ratio; however, if you’re going from standard definition to 320×240, even the most brutal deinterlacing methods will be hidden by the scaling.
Finally, to squeeze every last spare bit out of the video file, you can reduce the frame rate. Most video can be dropped to 24 FPS with no perceptible change. For relatively static material, such as slide presentations, very low frame rates in the 2-4 FPS range may be appropriate. Again, you also have to reduce your bitrate or 24 FPS will produce an image with fewer compression artifacts rather than a smaller file size. Set the frame rate with the -r option:
$ ffmpeg -i input.avi -acodec mp3 -vcodec mpeg4 -s 320×240 -r 24 output.mp4
A great way to experiment with acceptable bitrates is to use the -ss and -t options. The -ss option seeks to a position in the input file, and the -t option sets the duration of the output file. Thus:
$ ffmpeg -i input.avi -ss 30 -t 5 output.mp4
Will encode a five second clip of the input file thirty seconds from its beginning. This lets you see what the resulting compression level will look like without encoding the whole video.