Useful ffmpeg Tricks

Extract a piece of audio from a video (at 3m5s for 45s into high-quality mp3):

ffmpeg -i sample.avi -ss 00:03:05 -t 00:00:45.0 -q:a 0 -map a sample.mp3

Convert a video to an animated GIF (see here for more information on GIF palettes):

# Generate palette.
ffmpeg -i -vf "fps=15,scale=320:-1:flags=lanczos,palettegen" -y palette.png
# Convert video to GIF using palette.
ffmpeg -i -pix_fmt rgb24 -i palette.png -lavfi "fps=8,scale=640:-1:flags=lanczos [x]; [x][1:v] paletteuse" -f gif -y video.gif

Crop an MP3 at 20 seconds in for a duration of 30 seconds:

ffmpeg -ss 20 -t 30 -i input.mp3 -acodec copy output.mp3

Normalize an audio file (simple version):

# First pass (find out peak):
ffmpeg -i input.wav -filter:a volumedetect -f null /dev/null

# Second pass (increase volume by e.g. 10dB):
ffmpeg -i input.wav -filter:a "volume=10dB" output.wav

Crop a video at 5m20s for 10m12s without re-encoding it:

ffmpeg -ss 00:05:20.000 -t 00:10:12.000 -i input.mp4 -vcodec copy -acodec copy output.mp4

Crop a video to the dimensions out_w and out_h at the original position x and y (copy the audio stream):

ffmpeg -i in.mp4 -filter:v "crop=out_w:out_h:x:y" -c:a copy out.mp4