This note explains how to compress videos using FFmpeg with HEVC (H.265) and H.264 (AVC), focusing on broad compatibility across devices β PCs, TVs, ARM devices, mobile, and browsers.
| Parameter | Recommendation |
|---|---|
| Container | MP4 (.mp4) β universal support |
| Video Codec | HEVC / H.265 (hvc1) or H.264 (avc1) |
| Audio Codec | AAC LC |
| Audio Bitrate | 128β192 kbps |
| Resolution | Match target device (1080p, 720p) |
| Frame Rate | Keep source unless downscaling |
| Chroma / Bit Depth | 4:2:0, 8-bit (broad compatibility) |
HEVC gives smaller file sizes at same quality; H.264 maximizes legacy device support.
-vf scale=WIDTH:-2- Adjusts width, keeps aspect ratio (height auto, even number)
| Option | Meaning |
|---|---|
-rc:v vbr |
Variable bitrate, adjusts bitrate per scene complexity |
-cq:v N |
Constant quality target (lower = higher quality, bigger file) |
-crf N |
Constant Rate Factor (libx265 / libx264), lower = higher quality |
HEVC CQ examples (1080p):
- 18β22 β very high quality
- 23β27 β balanced quality & size
- 28β30 β smaller file, some compression artifacts
-b:v 3000k -maxrate 3500k -bufsize 7000k-b:vβ target bitrate-maxrateβ max bitrate peak-bufsizeβ VBV Buffer Size (Rate Control Window)- Common rule: bufsize = 2 Γ maxrate β avoids quality drops
-movflags +faststart- Enables streaming / instant playback.
- HEVC β
-tag:v hvc1for iOS / modern player support - H.264 β MP4 uses
avc1by default
- AAC LC, 128β192 kbps β universal support
ffmpeg -y -hwaccel cuda -i input.mp4 \
-map 0:v -vf scale=1920:-2 \
-c:v hevc_nvenc -preset p7 -rc:v vbr -cq:v 25 -tag:v hvc1 \
-map 0:a -c:a aac -b:a 160k \
-movflags +faststart \
output_hevc.mp4Explanation:
-hwaccel cudaβ NVIDIA GPU acceleration-map 0:vβ Select all video streams-vf scale=1920:-2β Scale width to 1920px, auto height to maintain aspect ratio-c:v hevc_nvencβ Use NVENC hardware HEVC encoder-preset p7β slowest NVENC preset β best quality / smallest file-rc:v vbrβ Variable bitrate, adjusts to scene complexity-cq:v 25β Constant quality target (lower = higher quality, bigger file)-tag:v hvc1β HEVC tag for MP4, ensures broad device compatibility-map 0:a -c:a aac -b:a 160kβ Encode all audio streams to AAC 160 kbps-movflags +faststartβ Allows streaming / instant playback
HEVC Remux (already HEVC, fix compatibility)
ffmpeg -i input.mp4 -c copy -tag:v hvc1 -movflags +faststart output_fixed.mp4ffmpeg -y -hwaccel cuda -i input.mp4 \
-map 0:v -vf scale=1920:-2 \
-c:v h264_nvenc -preset p7 -rc:v vbr -cq:v 23 -profile:v high -level 4.0 \
-map 0:a -c:a aac -b:a 160k \
-movflags +faststart \
output_h264.mp4Explanation:
-c:v h264_nvencβ NVENC hardware H.264 encoder-profile:v high -level 4.0β Ensures compatibility with most devices (TVs, browsers, PCs)- Other options same as HEVC example, optimized for H.264
ffmpeg -i input.mp4 \
-c:v libx265 -preset slow -crf 24 \
-c:a aac -b:a 160k \
-movflags +faststart \
output_x265.mp4Explanation:
-crf 24β constant quality, smaller CRF = better quality, larger file- Preset
slowβ better compression - CPU encoding is slower than NVENC but may yield slightly smaller files at same quality
ffmpeg -i input.mp4 \
-c:v libx264 -preset slow -crf 23 -profile:v high -level 4.0 \
-c:a aac -b:a 160k \
-movflags +faststart \
output_x264.mp4Explanation:
- CRF 23 β good balance of quality and file size
- High profile + level 4.0 β maximum compatibility
- HEVC vs H.264
- HEVC β modern devices, smaller file
- H.264 β maximum compatibility
- CQ / CRF
- Lower = higher quality, larger file
- Higher = smaller file, may show artifacts
- Audio β AAC LC 128β192 kbps
- Streaming & Web β always use
-movflags +faststart - Scaling β keep width/height divisible by 2 (
-2auto-fixes height) - Check FFmpeg options
ffmpeg -h encoder=hevc_nvenc ffmpeg -h encoder=h264_nvenc ffmpeg -h encoder=libx265 ffmpeg -h encoder=libx264