FFmpeg Command Examples
FFmpeg is a powerful, open-source command-line tool for handling multimedia files. It can decode, encode, transcode, mux, demux, stream, filter, and play virtually anything that has been created by humans and machines. Below are some common FFmpeg commands for various media processing tasks.
File Metadata and Basic Conversion
Understanding file information and performing simple format conversions are fundamental tasks.
# To print file metadata:
ffmpeg -i <file>
Batch Conversion and Format Transcoding
Automate the conversion of multiple files or change between popular formats like M4A to MP3, WebM to MP4, and MP4 to MOV.
# To convert all m4a files to mp3
for f in *.m4a; do ffmpeg -i "$f" -acodec libmp3lame -vn -b:a 320k "${f%.m4a}.mp3"; done
# To convert video from .foo to .bar
# -g : GOP, for searchability
ffmpeg -i input.foo -vcodec bar -acodec baz -b:v 21000k -b:a 320k -g 150 -threads 4 output.bar
# To convert webm to mp4
ffmpeg -i input_video.webm output_video.mp4
# To convert mp4 to mov
ffmpeg -i input_video.mp4 -acodec copy -vcodec copy -f mov output_video.mov
# To convert mov to mp4
ffmpeg -i input_video.mov -vcodec copy -acodec copy output_video.mp4
Image Sequence to Video
Create videos from a sequence of image files, useful for animations or time-lapses.
# To convert image sequence to video:
ffmpeg -r 18 -pattern_type glob -i '*.png' -b:v 21000k -s hd1080 -vcodec vp9 -an -pix_fmt yuv420p -deinterlace output.ext
Combining Audio and Video Files
Merge separate audio and video streams into a single media file.
# To combine video and audio into one file
ffmpeg -i video.ext -i audio.ext -c:v copy -c:a copy output.ext
Subtitle Integration
Add subtitle tracks to your video files.
# To add ass subtitle to the video
ffmpeg -i input_video.mp4 -vf ass=sub.ass output_video_subtitles.mp4
Audio Extraction and Playback
Extract audio from video files or listen to specific segments.
# Listen to 10 seconds of audio from a video file
#
# -ss : start time
# -t : seconds to cut
# -autoexit : closes ffplay as soon as the audio finishes
ffmpeg -ss 00:34:24.85 -t 10 -i path/to/file.mp4 -f mp3 pipe:play | ffplay -i pipe:play -autoexit
Concatenating Multiple Files
Join multiple video or audio files together sequentially.
# To combine audio and video from N files:
# See also https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg
cat mylist.txt
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
Further Resources
For more advanced usage and detailed explanations, refer to the official FFmpeg documentation: