Compress Video Using Command Line on Linux
Use FFmpeg, the compress video command on Linux to efficiently compress video files by adjusting codecs, quality, and resolution. The typical command balances file size and quality for optimized storage and bandwidth.
With a command like the below one, you can control file size effectively while balancing video quality, making FFmpeg the go-to solution for video compression on Linux:
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset fast output.mp4
This command uses FFmpeg to compress a video file, where:
-i input.mp4specifies the input video.-c:v libx264selects the H.264 codec for video compression.-crf 28adjusts the Constant Rate Factor, controlling the balance between file size and quality.-preset fastspeeds up the compression at the expense of some efficiency.output.mp4is the name of the compressed output file.
🤖AI Overview:
Compress Video Command on Linux employs FFmpeg, a powerful open-source utility, to reduce video file sizes while maintaining quality. The process involves specifying input, codec settings like H.264, and parameters such as CRF and resolution to control compression effectively. This ensures efficient storage usage and distribution on Linux systems.
Prerequisites to Compress Video Command on Linux using FFmpeg
To compress video using FFmpeg on Linux, your machine needs to meet all the below specifications:
- A Linux VPS running a compatible distribution.
- A non-root user with
sudoprivileges. - Access to Terminal/Command Line.
- FFmpeg installed.
Step 1: Install FFmpeg on Linux
FFmpeg can be easily installed using your system’s package manager and its official website.
Install FFmpeg via Package Manager
Run the command, depending on your Linux distribution, to install the latest stable version of FFmpeg:
sudo apt update
sudo apt install ffmpeg
CentOS/RHEL:
sudo yum install epel-release
sudo yum install ffmpeg
sudo dnf install ffmpeg
sudo pacman -S ffmpeg
Install FFmpeg from official website (Alternative Method)
If you prefer the latest version or need a custom build, you can download FFmpeg directly from the official website and follow the below steps:
- Once downloaded, extract the archive:
tar -xvzf ffmpeg-release.tar.gz
- Navigate to the extracted folder and compile FFmpeg:
./configure
make
sudo make install
Step 2: Verify Installation
After installation, verify that FFmpeg is correctly installed by running:
ffmpeg -version
Step 3: Compress Video Using FFmpeg
To compress a video with FFmpeg, the general command structure involves selecting an input file, specifying a codec, and defining the desired output.
Check the basic command to compress a video:
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset fast -c:a aac output.mp4
Advanced Compression Techniques via FFmpeg
Adjusting the Compression Quality with CRF
The Constant Rate Factor (CRF) is key for balancing video quality and file size. CRF ranges from 0 to 51, where:
- 0 is lossless (highest quality, large file size).
- 51 is the worst quality, yielding a tiny file size. Typically, values between 18-28 offer a good compromise between size and quality.
For example, to compress the video while maintaining decent quality, run:
ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
Reducing Video Resolution to Compress Size Further
To reduce the resolution and file size, use the -vf (video filter) option to scale the video.
For example, use the command below to reduce the resolution to 720p and compress the video, resulting in a smaller file size:
ffmpeg -i input.mp4 -vf scale=1280:720 -c:v libx264 -crf 23 output.mp4
Compressing with Audio Settings
You can also adjust the audio bitrate to reduce the overall file size.
For example, -b:a 128k is sufficient for most use cases and reduces the audio file size. To set the audio bitrate to 128 kbps, run:
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -c:a aac -b:a 128k output.mp4
Practical Use Cases for FFmpeg on Linux
Setting a Maximum Bitrate
You can limit the maximum bitrate to prevent excessive file sizes. To restrict the video bitrate to 1000 kbps, run:
ffmpeg -i input.mp4 -c:v libx264 -b:v 1000k -c:a aac -b:a 128k output.mp4
Two-Pass Encoding for Better Quality
This method is slower but can produce higher quality at lower bitrates.
To ensure the video maintains a consistent quality at a lower file size, run the commands below one by one:
ffmpeg -i input.mp4 -c:v libx264 -b:v 1000k -pass 1 -an -f mp4 /dev/null
ffmpeg -i input.mp4 -c:v libx264 -b:v 1000k -pass 2 -c:a aac -b:a 128k output.mp4
Convert video to audio
ffmpeg -i input.mp4 -q:a 0 -map a output.mp3
Change video format
ffmpeg -i input.mov output.mp4
Reduce Resolution
ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4
Compress with Audio
ffmpeg -i input.mp4 -c:v libx264 -b:a 128k output.mp4
Batch Compressing Videos
You can batch compress multiple videos using a simple loop in the terminal.
To loop through all .mp4 files in the directory and compress them one by one, run:
for file in *.mp4; do ffmpeg -i "$file" -c:v libx264 -crf 28 -preset fast -c:a aac "${file%.mp4}_compressed.mp4"; done
That’s it! Using the Compress Video Command on Linux with FFmpeg, an open-source powerhouse, you can compress videos efficiently while retaining quality.
Conclusion
Mastering the compress video command on Linux is indispensable for developers who manage video content effectively.
ffmpeg offers powerful and flexible options that enable high-quality compression while preserving compatibility and efficiency.
By understanding codec selection, CRF, presets, resolution scaling, and audio compression, you can tailor video encoding workflows to precise needs.
Always verify output integrity and apply best practices to achieve optimal compression results on Linux systems.
This knowledge empowers developers to deliver performant video applications and services in diverse Linux environments.
FAQ
2. How do I install FFmpeg for video compression on Linux?
Install "FFmpeg" using your Linux distribution's package manager (e.g., apt or yum), or via Snap or Flatpak for the latest versions.
3. What is a common FFmpeg command for basic video compression?
Use "ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset fast output.mp4" to balance compression and quality.
4. What does the CRF parameter control?
CRF controls video quality versus file size; values 18 to 28 are typical, with lower values yielding higher quality.
5. How can I reduce video resolution when compressing?
Add the "-vf scale=width:height" filter to scale video resolution during compression.
6. Can audio be compressed along with video?
Yes, specify audio codec and bitrate (e.g., -c:a aac -b:a 128k) to compress audio.
7. What advanced techniques improve compression quality?
Use two-pass encoding and hardware acceleration (e.g., NVENC, VAAPI) if supported for better quality and performance.
8. Is batch video compression possible on Linux?
Yes, automate compression with shell scripting loops running FFmpeg commands on multiple files.
9. How to convert video formats during compression?
Specify different output file extensions with FFmpeg to convert formats, like mov to mp4.
10. How do video compression and archive tools differ?
FFmpeg compresses multimedia data; tar and zip are for file archiving and packaging.