Every here and there, you may come across a use case where it’s necessary to reduce a video file by 50-80% without any noticeable loss of quality. Often times it’s for front-end performance reasons, but can also come in handy if a server has an upload limit(50MB on a typical WordPress site for example).
There are several ways to approach this, some including online solutions that require you to upload a file to their server, have it processed, then sent back. This can work, but you generally:
- Have to wait longer due to the server processing barrier
- Have less control over the results
- Have to send your data to a server you know nothing about
In this article, I hope to shine light on an alternative that comes with many Linux systems out of the box, is free, and quick to use and learn.
What is ffmpeg?
In short: ffmpeg is a library that will take care of all your multimedia needs. Want to convert a video file between two formats? ffmpeg. Want to compress a file as much as possible? ffmpeg for sure. Once you learn how to use it, it really starts to come in handy. If you’re on a modern Linux distro, it’s most likely already on your system.
Try it! Type
ffmpeg into your terminal. Otherwise, it should be as simple as running
sudo apt install ffmpeg. If you’re on another distro besides Ubuntu/Debian, just type in the equivalent for your package manager.
Making sure you have the right format
First things first, to compress a video file, it needs to be in a common format such as
mp4. Sometimes, video formats can be “locked” to something like
mv4, which makes it hard to do anything with the data. Luckily, converting this with ffmpeg is a breeze. I’ll show an example to convert a
mv4 file into
mp4. From there, it should be easy to compress.
ffmpeg -i input.m4v -vcodec copy -acodec copy output.mp4
input.mv4(the file you want to convert) and turns it into
output.mp4(the name of the new returned file). There’s also a shorthand code:
ffmpeg -i input.m4v -c copy output.mp4.
You can likely do this with any other unplayable media type, just modify the arguments.
Compressing the file
Here’s the fun part: seeing the real magic of ffmpeg. All you need to do is
cd into the directory that contains the video, then run the command you need to compress. Here’s what I use:
ffmpeg -i input.mp4 -vcodec libx265 -crf 20 output.mp4
This command sets the constant rate factor and also lowers the overall bitrate. The number after
-crf can range from 18 to 24, so there’s room for experimentation there.
For my purposes, I uploaded a compressed video through the WordPress admin, but the video on the front end wasn’t loading. Turns out, you can also compress using the
libx264 argument. Try this if the above doesn’t work!
A simpler approach
You can also run ffmpeg with no arguments and get decent results:
ffmpeg -i input.mp4 output.mp4
This works because ffmpeg performs some optimizations when run on its own. It’s worth a try!
Wait, there’s more!
What I presented here is powerful, but only a small fraction of what ffmpeg can accomplish. If you look at the docs, you’ll see there’s a much wider range of potential. For my own purposes, compressing video files is useful enough on its own. It’s always better than relying on a third party service, and even compresses quite a bit more. Mess around with it and see for yourself! At the end of the day, the little tools that come with a Linux build are usually a solid option.