Video Format Conversion: MP4 to WebM and Other Formats
One of the most common FFmpeg tasks is converting a video from one container format and codec to another.
1. Basic Format Conversion Syntax
The fundamental FFmpeg conversion command follows this pattern:
ffmpeg -i input_file.mp4 output_file.webmFFmpeg automatically selects the best codecs for the output container. However, you should usually specify codecs explicitly for predictable results.
2. MP4 to WebM (for Web Browsers)
WebM using the VP9 video codec and Opus audio codec is ideal for web delivery:
# Convert to WebM using VP9 video and Opus audio
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus output.webm3. Stream Copy (No Re-encoding)
If you want to change the container format without re-encoding (fastest option, no quality loss):
# Copy streams directly without re-encoding
ffmpeg -i input.mkv -c copy output.mp4This only works if the target container supports the existing codecs inside the source file.
4. Trimming Video Duration
To extract a specific time range from a video without re-encoding:
# Extract 30 seconds starting at timestamp 00:01:00
ffmpeg -i input.mp4 -ss 00:01:00 -t 00:00:30 -c copy output_trimmed.mp4Published on Last updated: