Nov 18th 2019
At the moment of creating this blog, I had the problem of having pretty heavy
mp4 videos and I knew that
webm files tend to be smaller and more streaming friendly. So I ended up making a script that converts an
mp4 file to
webm using the
ffmpeg open-source command.
This is the following you have to do to make the conversion.
If you install ffmpeg with the default repositories you will install ffmpeg version 3. In my case I just installed the version 4 which worked like a charm. To install ffmpeg 4 you need to add a repository with this command:
$ sudo add-apt-repository ppa:jonathonf/ffmpeg-4
Be sure you have the latest version of everything
$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt-get install ffmpeg
And say yes to all.
At this moment if you check the ffmpeg version you installed. It should say ffmpeg version 4.x.x
$ ffmpeg -version
You have already ffmpeg installed. Cool!
There are many ways to convert
webm format. The best quality converstion that worked for me was the two-pass enconding described here.
As you have to put input and output filenames multiple times plus many parameters and stuff, I just made a
nodejs script that do that job for me.
Create an empty file in the same directory you have the videos you want to convert. Name it
convert.js and put this on it:
const util = require('util');
The conversion script does not require any extra package which is awesome so we don’t need to install anything.
To run it you just need go to the directory you have the videos and run:
$ node convert.js filename.mp4
This will generate a
filename.webm in the same directory.
As two-pass conversion is pretty slow (but generate awesome results) it will take some time so don’t worry if nothing happens for a while. Just wait.
I didn’t have too much time to make the script. I just needed something that does the job so is not a super strict-clean-perfect-correct script.
Please consider ths following limitations:
- It will not convert the file when the filename has spaces on it so rename your file if that happens.
- The script prints nothing until the hole
webmfile has been made so be patient and don’t worry.
Feel free to fix anything you want. Here is the gist.