Common FFmpeg Commands

📢 This article was translated by gemini-3-flash-preview

When it comes to audio and video processing, FFmpeg is the ultimate open-source tool. I’ve been using it for a while, but my notes were scattered. Since I’m using it more frequently now, I decided to compile my most-used commands here (and add a few missing ones).

Convert Video Format

1
ffmpeg -i video.mp4 video.avi

Extract Audio from Video

1
ffmpeg -i input.mp4 -vn output.mp3
  • -vn: Disables video recording in the output file.

Re-encode Video

Sometimes a video file has a corrupted encoding or lags on certain devices. Re-encoding usually fixes this.

1
ffmpeg -i "video.mp4" -c:v libx264 -c:a aac -preset fast "video_re.mp4"

This re-encodes the video using x264 and the audio using AAC.

Convert to Silent WebM

1
ffmpeg -i input.mp4 -an -c:v libvpx-vp9 -crf 30 -b:v 0 output.webm

Parameters:

  • -an: Removes audio.
  • -c:v libvpx-vp9: Specifies the video codec (VP9 is widely supported; use libvpx for faster conversion).
  • -crf 30: Controls quality (0-63). Lower numbers mean better quality and larger files.
  • -b:v 0: When using VP9, it’s common to set bitrate to 0 and let the -crf parameter determine quality.

Convert to GIF

Use the -loop 0 parameter for infinite looping.

1
ffmpeg -i input.mp4 -loop 0 output.gif

If you need a transparent background, there are two scenarios:

1. The source video already has an alpha channel (e.g., ProRes 4444 .mov). Direct conversion often results in noise. Use a palette filter to ensure transparency is mapped correctly:

1
ffmpeg -i input.mov -vf "fps=15,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif

Parameters:

  • fps=15: Sets the frame rate.
  • split[s0][s1]: Splits the video into two streams. s0 generates the palette, s1 is the output image.
  • [s0]palettegen[p]: Generates a specialized 256-color palette named p. It preserves transparency by default.
  • [s1][p]paletteuse: Applies palette p to the video stream s1.

2. The source video has a solid background color. Use the colorkey filter to remove it:

1
ffmpeg -i input.mp4 -vf "fps=15,colorkey=0x00FF00:0.1:0.1,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif

For colorkey=0x00FF00:0.1:0.1:

  • 0x00FF00: The color to remove (Pure Green here; Black is 0x000000).
  • First 0.1: Similarity. Ranges 0.01-1.0. Usually set between 0.1 and 0.3.
  • Second 0.1: Blend. Softens the edges.

Merge Audio and Video

First, check if the audio codec is compatible (like AAC).

1
ffprobe audio.mp3

If compatible, use stream copy:

1
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a copy output.mp4

If the audio is incompatible, re-encode it during the merge:

1
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac output.mp4

Image Formats

FFmpeg can even convert image formats. For a basic conversion:

1
ffmpeg -i input.png output.jpg

Very handy for quick format swaps.

This post is licensed under CC BY-NC-SA 4.0 by the author.