FFmpeg

Table of Contents

How-to

Encode HTML5 compatible videos

# webm
ffmpeg -i IN -f webm -vcodec libvpx -acodec libvorbis -ab 128000 -crf 22 -s 640x360 OUT.webm

# mp4
ffmpeg -i IN -acodec aac -strict experimental -ac 2 -ab 128k -vcodec libx264 -vpre slow -f mp4 -crf 22 -s 640x360 OUT.mp4

# ogg (if you want to support older Firefox)
ffmpeg2theora IN -o OUT.ogv -x 640 -y 360 --videoquality 5 --audioquality 0  --frontend

Use Cases

Extracting audio from video

ffmpeg -i "video.mp4" -vn -codec:a libmp3lame -qscale:a 3 "audio.mp3"

Convert gif to mp4 howto

ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4
movflags
Optimizes the structure of the MP4 file so the browser can load it as quickly as possible.
pix_fmt
MP4 videos store pixels in different formats. We include this option to specify a specific format which has maximum compatibility across all browsers.
vf
MP4 videos using H.264 need to have a dimensions that are divisible by 2. This option ensures that’s the case.

Burn subtitles into video howto

First, you need ffmpeg compiled with --enable-libass option. On macOS, simply run brew install ffmpeg --with-libass

ffmpeg -i video.avi -vf subtitles=subtitle.srt out.avi