FFmpeg decodes and encodes video and audio streams.
To encode a video using H.264 (which is the default for MP4),
invoke ffmpeg
with the following options:
ffmpeg -i input.mp4 \
-vf 'scale=1920:1080' -preset veryslow \
-movflags faststart output.mp4
Encode a video using H.265 by specifying the codec used for video encoding:
ffmpeg -i input.mp4 \
-vf 'scale=3840:2160' -c:v libx265 \
-movflags faststart output.mp4
To cut a video from time 00:00:12 to 00:00:32:
ffmpeg -ss 00:00:12 -to 00:00:32 -i input.mp4 \
-preset veryslow \
-movflags faststart output.mp4
To extract an image at time 00:00:12:
ffmpeg -ss 00:00:12 -i input.mp4 \
-frames:v 1 -s 480x270 poster.jpg
To extract the first subtitle track:
ffmpeg -i input.mp4 \
-map 0:s:0 subtitles.srt
To just copy a video stream and an audio stream to another container:
ffmpeg -i input.mkv \
-c:v copy -c:a copy \
-movflags faststart output.mp4
Two-pass encoding is recommended for targeting an output file size.
To encode a 90-minute video to fit on a 700 MB CD-ROM:
ffmpeg -i input.mp4 -c:v libx264 -b:v 933k -pass 1 \
-an /dev/null && \
ffmpeg -i input.mp4 -c:v libx264 -b:v 933k -pass 2 \
-ac 2 -c:a libfdk_aac -b:a 128k output.mp4
ffmpeg -hide_banner -h
To show available codecs that were enabled during configuration:
ffmpeg -codecs
To show options for the encoder libx264
:
ffmpeg -hide_banner -h encoder=libx264
The graphics card can be used to accelerate video encoding.
FFmpeg needs to be built with enabled hardware acceleration.
A shell script that builds DEB packages of FFmpeg with NVIDIA GPU acceleration
enabled is found in the files section of this page.
The following command uses the NVIDIA encoder (NVENC) to encode a video
using HEVC (H.265):
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 \
-c:v hevc_nvenc -preset hq \
-movflags faststart output.mp4
Unfortunately, even with high-quality preset, the video quality of hevc_nvenc
is significantly lower compared to a software encoder (e.g. libx265
).
Help about the encoder hevc_nvenc
is shown using the following command:
ffmpeg -hide_banner -h encoder=hevc_nvenc
Disc | Launch | Video codec | Max. resolution |
---|---|---|---|
Ultra HD Blu-ray | 2016 | H.265/HEVC | 3840 × 2160 |
Blu-ray Disc | 2006 | H.264/MPEG-4 AVC | 1920 × 1080 |
DVD | 1996 | MPEG-2 | 720 × 576 (PAL) / 720 × 480 (NTSC) |
Mozilla's recommendations for everyday videos:
ffmpeg -i input.mp4 \
-c:v libvpx-vp9 -c:a libopus \
output.webm
ffmpeg -i input.mp4 \
-c:v libx264 -c:a libfdk_aac \
-movflags faststart output.mp4
YouTube recommended upload encoding settings:
-movflags faststart output.mp4
-c:v libx264 -profile:v high -bf 2 -pix_fmt yuv420p
-c:a libfdk_aac -profile:a aac_low