Comprehensive Guide to FFmpeg Command-Line Operations
Command Structure and Syntax
The fundamental syntax for FFmpeg follows a pattern where global options precede input files, input-specific options precede the -i flag, and output-specific options precede the output filename.
ffmpeg [global_options] [input_options] -i input_file [output_options] output_file
By default, FFmpeg attempts to perform lossless conversion, maintaining the same audio and video parameters as the source material unless specified otherwise.
Video Transcoding and Format Conversion
To convert video containers without re-encoding the streams (fast process), use the copy codec. This is useful for changing containers like MP4 to MKV.
ffmpeg -i source_video.mp4 -c:v copy -c:a copy output_video.mkv
To re-encode video using specific codecs, such as converting an older file to H.264:
ffmpeg -i input.avi -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4
Converting to HLS segments for adaptive streaming requires creating a playlist and segment files:
ffmpeg -i movie.mp4 -c:v libx264 -c:a aac -f hls -hls_time 4 -hls_list_size 0 -hls_segment_filename segment_%03d.ts stream.m3u8
Stream Extraction and Processing
Separating audio from video allows for dedicated audio processing. The -vn flag disables video recording.
ffmpeg -i multimedia_file.mp4 -vn -acodec copy audio_only.aac
Conversely, to extract video without audio (disabling audio with -an):
ffmpeg -i multimedia_file.mp4 -an -vcodec copy video_only.h264
To extract specific frames from a video as images, use the image2 muxer:
ffmpeg -i clip.mp4 -r 1 -f image2 snapshot_%03d.png
Video Filtering and Manipulation
FFmpeg provides powerful filtering capabilities via the -vf (video filter) option. To resize a video while maintaining aspect ratio, use the scale filter with a value of -1 for the height to calculate it automatically.
ffmpeg -i original.mp4 -vf scale=1280:-1 -c:a copy resized.mp4
To overlay a watermark image onto a video, use the overlay filter. This example places the logo 10 pixels from the top-left corner.
ffmpeg -i main_video.mp4 -i logo.png -filter_complex "overlay=10:10" watermarked_video.mp4
Trimming a video segment without re-encoding is efficient. The following command cuts from 10 seconds to 20 seconds.
ffmpeg -ss 00:00:10 -i input.mp4 -t 00:00:10 -c copy segment.mp4
Network Streaming Protocols
FFmpeg can push streams to various network protocols. To stream a file via UDP:
ffmpeg -re -i local_file.ts -c copy -f mpegts udp://192.168.1.50:1234
For publishing to an RTMP server (common for live streaming ingestion), the syntax typically involves the FLV format.
ffmpeg -re -i source_video.mp4 -c copy -f flv rtmp://live-server/app/stream_key
Audio Processing Options
Audio files can be converted and processed. To change the sampling rate of a WAV file to MP2:
ffmpeg -i input_audio.wav -ar 44100 output_audio.mp2
Adjusting volume can be done using the volume filter. The example below increases volume by 2.0 (double).
ffmpeg -i quiet_track.mp3 -af "volume=2.0" loud_track.mp3