The ultimate x265/HEVC encoding script: h265ize

If you have tons of movies or shows and are starting to run out of space while your dual socket Xeon or Opteron barely flinches when encoding/decoding x264 videos. You my friend need your videos in h265, say hello to h265ize.

But… before you say “Alright, lets start encoding!”, lets go over a few things first.

Who is HEVC/x265 for?

  • Someone with a powerful processor to decode these HEVC videos. Some sites claim there is a 400% (4 times) increase in processor usage while decoding x265 compared to x264. While this may be true for the most complex videos, I personally have seen an increase of 150%-200% (1.5-2 times) in processor usage. Sometimes even the same usage… (remember there is no hardware decoding support for HEVC and most likely won’t be any for the next 2-3 years.)
  • Someone looking to save large amounts of storage on their huge Blu-ray collection. (Large sized videos is where this codec really shines.)
  • Those that don’t intend to decode these on phones, tablets, or even weak PCs. I haven’t tried it but I know my phone and old laptop choked on decoding some really big x264 encodes.

Some drawbacks (As well as things noticeable to the average movie consumer if they look hard enough)

  • Re-encoding small videos without tweaking the default settings extensively is a waste of time. In most cases you will end up with an even larger video file than what you start with if the previous encoding was done correctly.
  • It seems to be unable to completely encode some really high quality animes (this is has been specific to coalgirls encodes). This is due to a problem with handbrake or x265, not h265ize.
  • Darks (mostly blacks and dark greys) in low contrast scenes seems to suffer significantly because of the low bitrate. These don’t destroy the quality of the film in anyway and if you just enjoy watching movies this won’t impact your experience either.

Real world results

 Film Type  x264 Size (Before Encoding)  HEVC Size (After Encoding)  HEVC Constant Quality
 Anime Movie  8.5 GB  2.54 GB  19
 Movie (Action/Adventure)  16.39 GB  6.40 GB  20
 Movie (Action/Adventure)  7.93 GB  3.54 GB  20
 American Animation  13.43 GB  5.25 GB  20
 Movie (Action/Adventure)  6.56 GB  3.01 GB  20
 Anime Show (26 episodes)  18.45 GB  11.4 GB  19 or 20
 Movie (Action/Adventure)  8.72 GB  4.1 GB  19
 American Animation  25 GB  7.9 GB  19
 Movie (Adventure)  4.8 GB  2.7 GB  19
 Anime Movie (Adventure)  4.37 GB  0.56 GB  19

Test Case (Ghost_in_the_Shell_Arise_[1080p,BluRay,aac,x264]_-_THORA Episode 1)

For this example I have encoded Ghost_in_the_Shell_Arise_[1080p,BluRay,aac,x264]_-_THORA Episode 1 (located here) to hevc using h265ize using the animeHigh preset. To me the quality still seems superb and I can’t find any noticeable degradation of quality. Without any optimizations for ssim and psnr calculations set for the x265 encoder we still achieved some pretty good numbers. But to be completely honest I don’t know much about SSIM and PSNR, so I’ll just leave the numbers here. These values were calculated using Emanuele Oriani’s qpsnr.

Remember these numbers are lower than they would be if x265 was tuned for SSIM or PSNR (which it should be but I’m too lazy).

Avg SSIM:    0.98539072            More data:  Ghost_in_the_Shell_Arise_1080pBluRayaacx264_-_THORA-Episode-1-SSIM

Avg PSNR:   45.07134367          More data:  Ghost_in_the_Shell_Arise_1080pBluRayaacx264_-_THORA-Episode-1-PSNR

The original x264 video had a file size of 4.37 GB.

The processed x265 video had a file size of 0.56 GB.

The processed x265 can be downloaded here.

Constant Quality 20 vs 19

I haven’t messed with it too much, but it seems that a constant quality of 19 compared to 20 provides a significant increase quality and helps with the black block problem. I would recommend it. Food for thought.

The Script and Usage

You can access this script and more info on its usage on github.

The whole script is written in bash. It also encodes via handbrakecli

Example Walk-though

First off lets start with the idea that you have the following in the corresponding directories;

  • /home/user/videos/big_buck_bunny_1080p_h264.mp4 (The video file we want to convert is an mp4 encoded with the x264 codec and is located in the /home/user/videos directory.)
  • /home/user/h265ize (The h265ize script we want to user to encode every video file located in the /home/user/videos directory.)
  • /home/user/h265 (This is the folder we want all our videos to output to.)

Now lets also define the following;

  • We want a constant quality of 23
  • We want to use the slow profile
  • We want our output in the mkv format

Now that we know where our files are, lets make sure the h265ize script is executable by running the command: sudo chmod +x /home/user/h265ize. Finally we can come up with the command that will allow us to encode. (Reference the github repo for more options) We start with the executing the script ./home/user/h265ize. Next the quality we want (-q option for constant quality) ./home/user/h265ize -q 23.  Next use the slow profile (-m option for encoder profile) ./home/user/h265ize -q 23 -m slow. We don’t need to define the mkv format for output since it’s already the default. Now to output to the /home/user/h265 directory we add the -d option for destination ./home/user/h265ize -q 23 -m slow -d /home/user/h265. And finally we add the folder containing our videos with no flag at the end ./home/user/h265ize -q 23 -m slow -d /home/user/h265 /home/user/videos.

So our final command comes out to look like ./home/user/h265ize -q 23 -m slow -d /home/user/h265 /home/user/videos.

OF COURSE ALL OF THIS IS POSSIBLE THANKS TO HANDBRAKE & FFMPEG

Features

  • Batch file processing (can process a whole folder)
  • Automatically detects video files (only processes video files found within a folder)
  • Works out of the box (in theory and personal practice)
  • Detects all audio tracks
  • Preserves audio codecs (if not possible; falls back to ac3 audio, this hasn’t happened to me yet, I think you would have to use a really strange audio codec)
  • Preserves audio track titles
  • Preserves flac audio sample rate and bit depth (currently supports 44.1khz/16bit and 96khz/24bit, 24bit is popular in high quality anime)
  • Detects and preserves all subtitles
  • Detects audio language, if audio language is not your native language and native language subtitles are provided, makes those subtitle default
  • Automatically upconvert vobsub/dvdsubs to srt subtitles on mkv files
  • Detects bit depth and uses appropriate encoder profile (10-bit is common in high quality anime, supports 8-bit and 10-bit)
  • Verbose and preview mode
  • File override detection (doesn’t accidentally write over a file that already exists, other than in preview mode)
  • Detects if file is already encoded in x265 and skips it
  • Ability to make encoding previews
  • Faulty encoding detection based on before and after video durations
  • Maintains file structure in output folder (So in theory you could just take your 3tb movie folder and throw it into the script and the output folder should look that same but with x265 videos)

So how do I watch these?

Right now I recommend you watch them with VLC or transcode them using Plex Media Server.

Lastly…

If you find any bugs feel free to leave a reply.

  42 comments for “The ultimate x265/HEVC encoding script: h265ize

  1. Pingback: yasam ayavefe

Leave a Reply