FFmpeg - Cheatsheet

By xngo on February 25, 2019


FFmpeg is a very powerful command line tool to manipulate and transcode videos. It has so many features and options that it is overwhelming. Here are the most often used commands.

Often used commands

# Extract a section of a video
    # Note: This only copy the 1st stream of video, audio, subtitle. If your video contains
    #       multiple audios or subtitles, the your have to use -map.
    ffmpeg -y -i INPUT.mp4 -ss 00:11:53 -to 00:23:11 -c copy OUTPUT.mp4
# Convert video from 1 format to another
    ffmpeg -i INPUT.rmvb -c:v libx264 -b:a 32k OUTPUT.mp4
# Preserve audio and video quality but use a different container(i.e from MOV to MKV)
    ffmpeg -i INPUT.mov -vcodec copy -acodec copy OUTPUT.mkv
# List supported video and audio codecs
    ffmpeg -codecs
# Get the info of video
    ffmpeg -i INPUT.avi
#Convert wav file to mp3 file.
    ffmpeg -i filename.wav filename.mp3
# Strange behaviour when running ffmpeg in a loop. It reads standard INPUT. To turn it off.
    ffmpeg ... < /dev/null
    ffmpeg -nostdin

Handling different streams

# Probe video file.
    ffmpeg -i INPUT.mkv
        #INPUT #0, matroska,webm, from 'INPUT.mkv':
        #  Metadata:
        #  Duration: 00:00:05.00, start: 0.000000, bitrate: 106 kb/s
        #    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p,
        #    Stream #0:1: Audio: vorbis, 44100 Hz, mono, fltp (default)
        #    Stream #0:2: Audio: vorbis, 44100 Hz, mono, fltp (default)
        #    Stream #0:3: Audio: vorbis, 44100 Hz, mono, fltp (default)
        #    Stream #0:4: Subtitle: ass (default)
# Extract streams.            
    ffmpeg -i INPUT.mkv \
        -map 0:v -c copy video.mkv \
        -map 0:a:0 -c copy audio0.oga \
        -map 0:a:1 -c copy audio1.oga \
        -map 0:a:2 -c copy audio2.oga \
        -map 0:s -c copy subtitles.ass
    # In this case, the example above is the same as:
    ffmpeg -i INPUT.mkv \
        -map 0:0 -c copy video.mkv \
        -map 0:1 -c copy audio0.oga \
        -map 0:2 -c copy audio1.oga \
        -map 0:3 -c copy audio2.oga \
        -map 0:4 -c copy subtitles.ass          

Convert VOB

 ffmpeg \
      -analyzeduration 500M -probesize 500M \
      -i INPUT.VOB \
      -map 0:1 -map 0:2 -map 0:3 -map 0:4 -map 0:5 -map 0:6 \
          -metadata:s:a:0 language=ita -metadata:s:a:0 title="Italian stereo" \
          -metadata:s:a:1 language=eng -metadata:s:a:1 title="English stereo" \
          -metadata:s:s:0 language=ita -metadata:s:s:0 title="Italian" \
          -metadata:s:s:1 language=eng -metadata:s:s:1 title="English" \   
      -codec:v libx264 -crf 21 \
      -codec:a libmp3lame -qscale:a 2 \
      -codec:s copy \
      OUTPUT.mkv < /dev/null

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.