Compress Video Command on Linux
The compress video command on Linux typically involves using FFmpeg, a powerful open-source tool that supports various codecs and formats.
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.mp4
specifies the input video.-c:v libx264
selects the H.264 codec for video compression.-crf 28
adjusts the Constant Rate Factor, controlling the balance between file size and quality.-preset fast
speeds up the compression at the expense of some efficiency.output.mp4
is the name of the compressed output file.
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
sudo
privileges. - 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
This guide explained how to use the Compress Video Command on Linux with FFmpeg to efficiently reduce file sizes.
From installation to advanced compression techniques, it detailed how to adjust parameters like CRF, bitrate, and resolution for optimal results.
Since some video files are often compressed using tar or zip before uploading to servers, for managing compressed video files, refer to Tar Command in Linux and Zip Command in Linux.