Handbrake-cli encoding tests x265

  • Posted on: 15 October 2018
  • By: charm
Handbrake-cli encoding video on seperate systems

When you're working with Blu-ray media file sizes can be a nightmare. Back when I got my 8TB drive I had slightly over 5TB of files. Eight terabytes seemed like it would be enough at the time, but here it is less than a year later and the drive is dangerously close to full (308GB free). Drive space wasn't a problem when I was ripping my DVDs, but when I "rip" my Blu-ray discs it's more like dumping them to the drive, there's no re-encoding done. Up until a few days ago I was only ripping the main movie on my Blu-ray discs, but after discovering KODI has an option to display additional disc media via the context menu and an add-on I started ripping the additional items on the disc.

At one point I looked at my DVD and Blu-ray collection and over 700 DVDs took up barely 1TB whereas just 70 Blu-ray discs took up several terabytes. My collection has grown a lot on the Blu-ray front thanks to cheap discs popping up everywhere (last weekend I bought 5 from Sunrise for $4.99/ea). With less than 350GB of storage left I could either look at buying another drive, add my old 3TB drives to expand the storage, or finally look at compressing the Blu-ray files. I chose the last option, compressing the Blu-ray files.

When ripping Blu-rays I use MakeMKV. I've used MakeMKV for several years, it's been an enormous help, both for Blu-rays and DVDs that Handbrake can't seem to figure out. The problem with MakeMKV is that it doesn't compress the Blu-ray files. Handbrake can compress files, but I didn't want my home workstation taken up encoding video. The file sizes of some of the Blu-ray files is over 35GB and encoding such a file would take a considerable time. There are 2 other machines I could use to encode the files, the strongest of which is our KODI media centre machine which boasts an i5-2300 CPU.

I took a 500MB file and re-encoded it using the command-line version of handbrake, handbrake-cli. I wanted superior quality, but smaller file size. Although I'd love for encodes to finish quickly because of the amount of video I have to encode it's less of an issue if I encode on our media centre. During the encoding on our KODI box I had a DVD quality file playing in the background. It continued to play while the encoding was going on.

To begin with I created a folder called scripts in my home directory and added it to the $PATH in .bashrc:

mkdir ~/scripts
echo 'export PATH="$PATH":/home/charm/scripts' >> ~/.bashrc

In that scripts folder I have a very simple bash script (hb.sh):

HandBrakeCLI -i "$1" -o "${1%.*}.mkv" --preset="H.265 MKV 1080p30"

Now my test file was actually already a .mkv file, so I renamed it .mp4. For future tests I'm simply going to modify the "${1%.*}.mkv" portion to be something like "${1%-encode.%}.mkv". The resulting quality was excellent and the resulting file size was pretty great too, only 50MB from 500MB. I tested across 3 systems, the KODI media centre was the fastest, with my A8-5600K workstation CPU coming in second. This sample was a small file just to test the feasability of re-encoding all my Blu-ray files. Here are the results:

Original File Size: 509535320 bytes (510 MB)

A8-5600K APU: 1103.37 seconds to complete, an average of 3.11 fps.
i3-4010U CPU: 1482.81 seconds to complete, an average of 2.31 fps.
i5-2300 CPU: 818.25 seconds to complete, an average of 4.19 fps.

Strangely the APU file size was slightly larger than the i3 or i5 which both resulted in the exact same, slightly smaller, file size. I believe this is because I was running Ubuntu 18.04 on the A8-5600K and used a slightly older version of handbrake-cli. I don't believe using the newer version would result in much of a difference since the versions are pretty close to one another.

A8-5600K file size: 50196329 bytes (50.19MB)
Intel file sizes: 50155711 bytes (50.15MB)

These frames per second might seem really low, part of the reason for this I believe is the H.265 codec. Initially when I tried this on the i3-4010U I used the very old version of handbrake-cli that came with Ubuntu 16.04. It encoded the file in some terrible version of  H.264 (noticably blocky) because the old version of handbrake doesn't support the preset "H.265 MKV 1080p30." The encoding took just over a minute, even on the i3. After installing the newest version of handbrake using a ppa (see https://launchpad.net/~stebbins/+archive/ubuntu/handbrake-releases) the 265 encoding worked and I discovered just how much slower the i3 was.

This was the start of my experimenting, which has become more urgent given the low drive space. My next test is to take a small movie with subtitles and see if the encoding keeps those embedded subtitles. I'm also considering using the "High Pass" preset to see if it will encode faster with similar quality. If you've already done any of this feel free to email me: c h a s l i n u x-at-g m a i l . com.

Update October 16/2018: Last night I started the script on a 35GB file. On the i5-2300 the encoding will take roughly 12-13 hours based on several hours having passed. This might seem like a very long time, but there's actually some good news in this, so far the frames per second seems to be a couple of frames higher 6 hours into the process. Unfortunately the Handbrake 265 preset seems to have dropped the subtitles and the quality turned out much worse than the short video I encoded above. The file size difference was enormous 1.8GB vs 35GB. That said it felt a bit like watching something recorded on a VCR vs a DVD with noticeable black banding. I'm going to try encoding the file with a decent H264 setting.