LinuxCommandLibrary

ffmpeg

Convert, record, and stream multimedia

TLDR

Extract the sound from a video and save it as MP3

$ ffmpeg -i [path/to/video.mp4] -vn [path/to/sound.mp3]
copy

Transcode a FLAC file to Red Book CD format (44100kHz, 16bit)
$ ffmpeg -i [path/to/input_audio.flac] -ar 44100 -sample_fmt s16 [path/to/output_audio.wav]
copy

Save a video as GIF, scaling the height to 1000px and setting framerate to 15
$ ffmpeg -i [path/to/video.mp4] [[-vf|-filter:v]] 'scale=-1:1000' -r 15 [path/to/output.gif]
copy

Combine numbered images (frame_1.jpg, frame_2.jpg, etc) into a video or GIF
$ ffmpeg -i [path/to/frame_%d.jpg] -f image2 [video.mpg|video.gif]
copy

Trim a video from a given start time mm:ss to an end time mm2:ss2 (omit the -to flag to trim till the end)
$ ffmpeg -i [path/to/input_video.mp4] -ss [mm:ss] -to [mm2:ss2] [[-c|-codec]] copy [path/to/output_video.mp4]
copy

Convert AVI video to MP4. AAC Audio @ 128kbit, h264 Video @ CRF 23
$ ffmpeg -i [path/to/input_video].avi [[-c|-codec]]:a aac -b:a 128k [[-c|-codec]]:v libx264 -crf 23 [path/to/output_video].mp4
copy

Remux MKV video to MP4 without re-encoding audio or video streams
$ ffmpeg -i [path/to/input_video].mkv [[-c|-codec]] copy [path/to/output_video].mp4
copy

Convert MP4 video to VP9 codec. For the best quality, use a CRF value (recommended range 15-35) and -b:v MUST be 0
$ ffmpeg -i [path/to/input_video].mp4 [[-c|-codec]]:v libvpx-vp9 -crf [30] -b:v 0 [[-c|-codec]]:a libopus -vbr on -threads [number_of_threads] [path/to/output_video].webm
copy

SYNOPSIS

ffmpeg [global_options] [{[input_options] -i input}]... [{[output_options] output}]...

PARAMETERS

-i input
    Specify input file or stream URL

-f fmt
    Force input/output format (e.g., mp4, flv)

-c[:s] codec
    Codec selection for stream s (e.g., libx264, copy)

-b[:s] bitrate
    Set bitrate for stream s (e.g., 2M)

-r rate
    Set frame rate (e.g., 30)

-s size
    Set frame size (e.g., 1920x1080)

-aspect ratio
    Set video aspect ratio (e.g., 16:9)

-ar rate
    Set audio sampling rate (e.g., 44100)

-ac channels
    Set audio channels (e.g., 2)

-vn
    Disable video

-an
    Disable audio

-ss time
    Seek to timestamp (e.g., 00:01:30)

-t duration
    Limit duration (e.g., 60)

-vf filter
    Video filter graph (e.g., scale=640:480)

-af filter
    Audio filter graph

-map spec
    Select streams (e.g., -map 0:v:0)

-y
    Overwrite output files

-threads count
    Set threading count

-loglevel level
    Set logging verbosity (e.g., info)

-hide_banner
    Hide banner

DESCRIPTION

FFmpeg is a comprehensive, open-source command-line tool for handling multimedia data. It supports decoding, encoding, transcoding, muxing, demuxing, streaming, filtering, and playing virtually any audio/video format. Developed as part of the FFmpeg project, it powers many applications and services worldwide.

Key capabilities include format conversion (e.g., MP4 to AVI), bitrate adjustment, resolution scaling, audio extraction, subtitle burning, live streaming to platforms like YouTube, and complex filtering chains for effects like cropping, rotating, or overlaying. Its libraries (libavcodec, libavformat, etc.) are used in VLC, YouTube, and more.

FFmpeg excels in batch processing, server-side automation, and real-time manipulation, but its syntax can be intricate due to extensive options for fine control over streams, codecs, and protocols. It's cross-platform, lightweight, and actively maintained, making it indispensable for developers, sysadmins, and media professionals.

CAVEATS

FFmpeg syntax is complex with stream specifiers; test commands incrementally. Some codecs require patents/licenses. Compilation needed for latest features or hardware acceleration. High CPU usage for transcoding.

STREAM SPECIFIERS

Use :v for video, :a for audio, 0:v:0 for first video stream to target options precisely.
Example: -c:v libx264 -b:v 2M

FILTERS

Powerful chainable filters via -vf/-af. Examples: scale=iw/2:ih/2 (half size), drawtext=text='Hello' (overlay text). See ffmpeg-filters(1).

EXAMPLES

Convert: ffmpeg -i input.mp4 output.avi
Extract audio: ffmpeg -i video.mp4 -vn audio.aac
Stream: ffmpeg -re -i input.mp4 -f flv rtmp://server/live/stream

HISTORY

Initiated in 2000 by Fabrice Bellard as part of the FFmpeg project (forked from libav in 2004). Evolved from multimedia libraries; avconv spun off but deprecated. Actively developed by a global community, with major releases incorporating new codecs, hardware support (e.g., NVENC), and AI filters.

SEE ALSO

ffprobe(1), ffplay(1), avconv(1)

Copied to clipboard