Reduce Video Sizes ffmpeg Linux Terminal
To reduce a video’s file size from the Linux terminal, use ffmpeg with a command like ffmpeg -i input.mp4 -vcodec libx264 -crf 28 -preset slow output.mp4. This re-encodes the video with the H.264 codec, and the CRF value (18–28, lower is higher quality) controls the trade-off between quality and file size.
Why Video Files Are Large
Video files are large because they store a sequence of images (frames) plus audio. Uncompressed, even a short 1080p clip can be hundreds of megabytes. Compression works by removing redundant data—both within a single frame (spatial) and between consecutive frames (temporal). The codec (encoder/decoder) you choose determines how efficiently this is done. Modern codecs like H.264 (AVC) and H.265 (HEVC) achieve much better compression than older ones like MPEG-2 or Motion JPEG, meaning smaller files at the same visual quality.
What ffmpeg Does
ffmpeg is a free, open-source command-line tool that can read, decode, encode, and transcode almost any audio or video format. It is the Swiss-army knife of media processing on Linux (and other platforms). When you run an ffmpeg command, you tell it the input file, the codecs to use for video and audio, and various parameters that affect quality and size. It then writes a new file, leaving the original untouched (unless you specify the same filename, which is not recommended).
Choosing the Right Command
The most common approach for reducing file size is to re-encode to H.264 with a constant rate factor (CRF). Here are the key parameters:
- CRF (Constant Rate Factor): A value from 0–51 (for H.264). Lower means higher quality and larger file. A typical range for good quality is 18–28. Start with 28 and adjust. A CRF of 28 often reduces size by 50–80% with acceptable quality for most uses.
- Preset: Controls encoding speed vs. compression efficiency. Slower presets (e.g.,
slow,veryslow) produce smaller files at the same quality but take longer. Faster presets (fast,ultrafast) are quicker but yield larger files. - Audio: Re-encode audio with AAC at a lower bitrate (e.g., 128k) to save space. Use
-c:a aac -b:a 128k. - Resolution: If you don’t need full 1080p, downscaling to 720p or 480p dramatically reduces size. Use
-vf scale=1280:720.
Example command with audio and scaling:
ffmpeg -i input.mp4 -vcodec libx264 -crf 28 -preset slow -vf scale=1280:720 -c:a aac -b:a 128k output.mp4
H.264 vs. H.265 (HEVC) vs. VP9
H.264 (libx264) is the most widely compatible and is a safe choice. H.265 (libx265) offers about 30–50% better compression than H.264, meaning even smaller files, but encoding is slower and playback compatibility is not universal (older devices may not play it). VP9 (libvpx-vp9) is a royalty-free codec used by YouTube; it’s also efficient but encoding is very slow. For most personal use, H.264 is recommended. If you need maximum compression and can accept slower encoding, try H.265:
ffmpeg -i input.mp4 -vcodec libx265 -crf 28 -preset slow -c:a aac -b:a 128k output.mp4
Common Mistakes and How to Avoid Them
- Using a too-low CRF: Setting CRF to 0 or 10 will produce a huge file. Stick to 18–28.
- Not checking audio: Audio can be a surprising portion of file size. Lowering the audio bitrate helps.
- Ignoring the original codec: If the input is already H.264, re-encoding to H.264 with a high CRF may not reduce size much; consider a higher CRF or a more efficient codec.
- Forgetting to remove the original: The output is a new file; you must delete the old one manually if you want to free space.
- Using a preset that’s too fast: Ultrafast produces larger files. Use at least
mediumfor a balance.
When to Use Two-Pass Encoding
If you are targeting a specific file size (e.g., you need to fit a video on a USB stick of a known size), use two-pass encoding. This analyzes the video first, then encodes it to hit a target bitrate. The command is more complex:
ffmpeg -i input.mp4 -c:v libx264 -b:v 1M -pass 1 -an -f mp4 /dev/null && ffmpeg -i input.mp4 -c:v libx264 -b:v 1M -pass 2 -c:a aac -b:a 128k output.mp4
Here -b:v 1M sets a video bitrate of 1 Mbps. The first pass creates a log file, the second uses it. This is slower but gives predictable size.
Frequently Asked Questions
Will reducing size always reduce quality?
Yes, any lossy compression reduces quality. The goal is to reduce size while keeping quality acceptable for your purpose. Using a moderate CRF (like 28) often produces files that look nearly identical to the original, especially on smaller screens.
What is the best CRF value?
There is no single best value; it depends on the source and your tolerance for quality loss. For 1080p, CRF 23 is considered visually lossless by many, while CRF 28 is a good balance. Test with a short clip to see what works for you.
Can I reduce size without re-encoding?
Sometimes you can just copy streams with -c copy if the container is inefficient, but that rarely reduces size. You can also trim the video with -ss and -t to remove unwanted parts, which effectively reduces size without re-encoding.
Why is my output file larger than the input?
This happens if the input is already highly compressed (e.g., a low-bitrate H.264 file) and you use a low CRF or a fast preset. Also, if you don’t specify a codec, ffmpeg may use a lossless codec. Always check the input’s properties with ffprobe.
Is it safe to use ffmpeg?
Yes, ffmpeg is a well-established, open-source tool. It does not modify your original file unless you tell it to. Always use a different output filename to avoid accidental overwrites.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.