How to convert MP4 video file in Linux

How to convert MP4 video file in Linux

Compressing an MP4 video file on Linux can be done easily using various tools. The most common method involves using FFmpeg, but alternatives like HandBrake, Avidemux, and VLC can also be used. Below are methods for compressing MP4 files using different tools, with FFmpeg being the most flexible and powerful option.

Before we begin, we want to let you know you can use https://www.xconvert.com/compress-mp4 to compress your .mp4 online. You can choose Autoscale feature to keep the quality high while reducing file size.


1. Using FFmpeg

FFmpeg is the most powerful command-line tool for multimedia conversion and compression. You can easily adjust the video’s codec, bitrate, resolution, and other parameters to reduce file size.

a. Installing FFmpeg

Ubuntu/Debian:

sudo apt update
sudo apt install ffmpeg

Fedora:

sudo dnf install ffmpeg

Arch Linux:

sudo pacman -S ffmpeg

b. Basic Compression Example

Reduce Video Bitrate:

This will compress the MP4 video by lowering the bitrate:

ffmpeg -i input.mp4 -b:v 1000k -b:a 128k output.mp4
  • -b:v 1000k: Sets the video bitrate to 1000 kbps (adjust to your needs).
  • -b:a 128k: Sets the audio bitrate to 128 kbps.

Using CRF (Constant Rate Factor) for Compression:

CRF is a better way to control the quality and size trade-off for video encoding. A lower CRF value means better quality, but larger file size. Recommended values are between 18 and 28.

ffmpeg -i input.mp4 -vcodec libx264 -crf 28 -preset slow -c:a aac -b:a 128k output.mp4
  • -crf 28: Compresses with a quality factor of 28 (lower value = better quality, higher value = more compression).
  • -preset slow: Slower preset provides better compression but takes more time. You can choose between ultrafast, superfast, fast, medium, slow, slower, veryslow.

Compress by Reducing Resolution:

To reduce the resolution of the video (e.g., downscaling to 1280×720):

ffmpeg -i input.mp4 -vf scale=1280:720 -c:a copy output.mp4

Compress by Changing Codec:

Use H.265 (HEVC) for better compression while maintaining quality. However, it may take longer to encode.

ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4

Compress by Reducing Frame Rate:

Reducing the frame rate can significantly reduce file size.

ffmpeg -i input.mp4 -r 24 output.mp4

This command reduces the frame rate to 24 fps (from the default 30 or 60 fps).


2. Using HandBrake (GUI)

HandBrake is a GUI-based tool that allows for easy video compression and conversion. It also has a command-line version for scripting.

a. Installing HandBrake

Ubuntu/Debian:

sudo add-apt-repository ppa:stebbins/handbrake-releases
sudo apt update
sudo apt install handbrake-cli handbrake

Fedora:

sudo dnf install HandBrake-gui HandBrake-cli

Arch Linux:

sudo pacman -S handbrake

b. Compressing MP4 Using HandBrake

  1. Open HandBrake and select your source MP4 file.
  2. Choose Output Settings:
  • Choose a preset, like “Fast 720p30” or “Fast 1080p30” to compress the video while retaining reasonable quality.
  1. Video Settings:
  • In the “Video” tab, set the encoder to H.264 (x264) or H.265 (x265) for better compression.
  • Adjust the Quality Slider (RF value). A higher RF value results in more compression. RF 22-28 is typically a good balance.
  1. Start Compression: Click “Start” to begin compressing.

3. Using Avidemux (GUI)

Avidemux is a simple tool for video editing and compression with a straightforward GUI.

a. Installing Avidemux

Ubuntu/Debian:

sudo apt update
sudo apt install avidemux

Fedora:

sudo dnf install avidemux

Arch Linux:

sudo pacman -S avidemux-gtk

b. Compressing MP4 Using Avidemux

  1. Open Avidemux and load your MP4 video file.
  2. Select Output Codec:
  • Under “Video Output,” choose MPEG-4 AVC (x264) or HEVC (x265) for more compression.
  • Under “Audio Output,” choose AAC (Faac).
  1. Configure Settings:
  • Adjust settings like bitrate or quality (higher compression levels reduce quality).
  1. Save Video: Click “File” > “Save” and give your file a name.

4. Using VLC Media Player (GUI)

VLC is a widely used media player that also supports video conversion and compression.

a. Installing VLC

Ubuntu/Debian:

sudo apt update
sudo apt install vlc

Fedora:

sudo dnf install vlc

Arch Linux:

sudo pacman -S vlc

b. Compressing MP4 Using VLC

  1. Open VLC and go to Media > Convert / Save.
  2. Add the MP4 file: Click “Add” and select the MP4 file.
  3. Choose Convert Settings:
  • Click “Convert / Save” and choose a profile such as “Video – H.264 + MP3 (MP4)”.
  • Click the wrench icon to adjust settings like video resolution, frame rate, and bitrate.
  1. Start Compression: Set the destination file, then click “Start” to compress the video.

5. Other Useful Tips for Compression

a. Adjust Video Bitrate

Reducing the bitrate is one of the most effective ways to compress a video, but it can also reduce quality. Make sure to find a balance that maintains reasonable quality.

b. Choose the Right Codec

  • H.264 (libx264) is widely supported and offers good compression.
  • H.265 (libx265) offers even better compression but takes longer to encode and may not be as widely supported.

c. Lower Resolution

If the video is originally 1080p or higher, you can compress it by lowering the resolution to 720p or even 480p, depending on your needs.

d. Reduce Frame Rate

If the video doesn’t require high frame rates, reducing the frame rate (e.g., from 60 fps to 30 or 24 fps) can significantly reduce file size.

e. CRF Adjustment

When using FFmpeg, the CRF (Constant Rate Factor) is key to balancing quality and compression. A value of 23 is considered good, but for more compression, a value of 28-30 may still retain acceptable quality.


Example: Compressing MP4 with FFmpeg (Optimal Settings)

Here’s a balanced example that compresses an MP4 video using H.264 with medium quality and moderate compression:

ffmpeg -i input.mp4 -vcodec libx264 -crf 28 -preset slow -b:a 128k output.mp4
  • Explanation:
  • -crf 28: Controls video quality (28 is more compressed, 23 is better quality).
  • -preset slow: Slower encoding for better compression (choose ultrafast, fast, medium, slow, or veryslow).

By using the right combination of bitrate, codec, resolution, and CRF settings, you can compress MP4 files on Linux while maintaining an acceptable level of video quality. Let me know if you need more help or further examples!


Additional Resources

Featured photo by Jakob Owens on Unsplash