The Wonder that is FFmpeg

I’ve recently discovered FFmpeg and I’m absolutely blown away how good it is. In the past when I’ve had to encode video I’ve generally used Handbrake but at the end of the day this is just a wrapper over the top of FFmpeg. If you’re willing to learn the commands necessary to drive FFmpeg directly it’s a far more powerful tool. It’s also available for a wide range of operating systems including Windows and Linux.

Converting FLAC to MP3

I’m sure some of you can tell the difference in quality between a FLAC and a high bitrate MP3 but I can’t so I decided to re-encode my FLAC files. Using FFmpeg I can do a whole folder with a single command under Linux. The 320k is the bitrate of the resultant file. A setting of 320k roughly halves the size of a FLAC for the files I’ve converted so far.

for i in *.flac; do ffmpeg -i "$i" -ab 320k "${i%.*}.mp3"; done

Reducing the Size of Video Files

I had a video file that was around 900MB for 25 minutes of footage, that seemed like a lot considering it was an animation which usually compress well. The following command reduced it down to 150MB with no noticeable loss of quality. The -crf 28 sets the video quality, high values result in more compression.

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

The input file was x264 with two audio tracks (stereo and 5.1). The output was x265 with just the stereo track. If you want to keep all the audio tracks then use this command (obviously the file size will be slightly larger). Note that I switch to the Windows version of FFmpeg here, my Windows machine is more powerful than any of my Linux servers.

ffmpeg.exe -i input.mkv -map 0 -c:v libx265 -crf 28 -c:a copy output.mkv

A Windows batch file to re-encode a folder full of video files using the above settings is shown below. Note that you need to create the output directory first and this will try to encode all the MKV files in the directory, if you want to do other types of files just change the extension. A nice feature on Windows is if you click in the command prompt window you can pause the execution of the job until you press escape.

for %%a in ("*.mkv") do ffmpeg -i "%%a" -map 0 -c:v libx265 -crf 28 -c:a copy "output\%%a"

Further Reading