Handbrake_cli settings for 2020

I want to automate optimizing my tv shows to try to get the majority to play without needing transcoding. My plex server is not very powerful.

I found a script here that I thought i would try: https://gist.github.com/onedr0p/c88f517a55f77ed154cdb00009463a47

In looking at some of the forks of this script i saw people have changed some of the handbrake_cli settings. i am not sure which is better or if there are better options all together.

This is the original script.
handbrake_cli,
‘-i’, input_file,
‘-o’, output_file,
‘-f’, ‘mp4’,
‘–loose-anamorphic’,
‘–modulus’, ‘2’,
‘-e’, ‘x264’,
‘-q’, ‘19’,
‘–cfr’,
‘-a’, ‘1’,
‘-E’, ‘faac’,
‘-6’, ‘dp12’,
‘-R’, ‘Auto’,
‘-B’, ‘320’,
‘-D’, ‘0’,
‘–gain’, ‘0’,
‘–audio-copy-mask’, ‘none’,
‘–audio-fallback’, ‘ffac3’,
‘-x’, ‘level=4.0:ref=16:bframes=16:b-adapt=2:direct=auto:me=tesa:merange=24:subq=11:rc-lookahead=60:analyse=all:trellis=2:no-fast-pskip=1’

This is another persons version of it.
handbrake_cli,
‘-i’, input_file,
‘-o’, output_file,
‘-f’, ‘mp4’,
‘–loose-anamorphic’,
‘–modulus’, ‘2’,
‘-e’, ‘x265’,
‘-q’, ‘22’,
‘–cfr’,
‘-a’, ‘1’,
‘-E’, ‘av_aac’,
‘-R’, ‘24’,
‘-B’, ‘128’,
‘-D’, ‘0’,
‘-w’, ‘720’,
‘-l’, ‘480’,
‘–gain’, ‘0’,
‘-x’, ‘level=4.0:ref=16:bframes=16:b-adapt=2:direct=auto:me=tesa:merange=24:subq=11:rc-lookahead=60:analyse=all:trellis=2:no-fast-pskip=1’

My plex server runs on windows 10.

Thank you.

I would check out this link to see information about each option, but the main differences I see are…

  1. First command uses the x264 codec while second uses x265. X265 is a newer, better codec, but plex doesn’t always play nice with it (just search the forums), so probably better idea to stick with x264.
  2. The first command uses a rate factor of 19, while the second uses 22. For the rate factor, the lower the number, the better the quality, but with bigger file size. Note that I realized the difference in the codes used after I noticed this, so the different rate factors is likely due to the difference on codes.
  3. The second command seems to force the video resolution to 720x480, whereas the first command should leave the resolution unchanged.

For the audio options in both commands, I would suggest just copying the audio over. I have a lot of files where I encoded the audio to AAC for direct playback, but I have noticed lots of artifacts when playing back. These include chirping at high frequencies and other weird things. For those files, I usually end up switching the audio track to the original track and let Plex tranacode on the fly; sounds much better. Audio is much easier to convert, so it shouldn’t bog down your system like video tranacosing does.

Hope this helps!

Thank you so much for this information.

So would you just leave command -E out and take the first one?

You are trying to find a universal setting for all types of content.
Such a setting unfortunately doesn’t exist.
You will alwas have to dadapt your settings to the type of content and the target resolution, or your transcoding results will be not optimal. Either by producing a poorer quality or by wasting storage space due to a too high quality setting.

The first one is for content from DVD (–loose-anamorphic’,)
It is using a constant quality factor of 19 RF, which is better suited for SD and lower resolutions.

It’s been a while since I used HandBrake (I switched to FFmpeg for more control), but if I remember correctly for the first command…

-a  : which audio track to use; in this case, use audio track one
-E  : Audio codec to use; faac in this case
-6  : Sets the channel downmixing; Dolby Pro Logic II in this case?
-R  : Not sure
-B  : bit rate? 320 kbps
-D  : Not sure
-gain: Probably to regain so that peak volume is 0 dB

To just copy the first audio track, I think you would just need -a 1 -E copy, and then you can get rid of the rest, but you’ll have to double check the documentation to be sure

For @OttoKerner’s comment, he is 100% right that there really isn’t a one-size-fits-all setting for different types of content. For example, here is some information about constant rate factor (CRF or constant quality) settings for different resolutions; pertinent information below:

  • RF 18-22 for 480p/576p Standard Definition1
  • RF 19-23 for 720p High Definition2
  • RF 20-24 for 1080p Full High Definition3
  • RF 22-28 for 2160p 4K Ultra High Definition4

The basic idea is that with a higher resolution video, you can compress it a little more before you start start seeing issues. The higher compression also helps reduce space as file sizes grow very fast as resolution increases. If disk space is not a concern for you, you can just convert everything at the CRF of 19, or slightly higher within the range above; HD stuff will look awesome but be kind of large while SD stuff should look `the same’ as the original content.

For the loose-anamorphic, I never used that option, but that may also explain why some of me encodes had incorrect aspect ratios hahaha. I `fixed’ these later with FFmpeg.

So this script above actually does try to convert using ffmpeg first if that fails it failovers to handbrake.

“”" Convert files found to mp4 using ffmeg “”"
def convert_ffmpeg(input_file, output_file):
logger.info(“ffmpeg - converting %s to %s”, input_file, output_file)

try:
    dev_null = open(os.devnull, 'w')
    return_code = subprocess.call([
        ffmpeg_cli,
        '-n',
        '-fflags', '+genpts',
        '-i', input_file,
        '-vcodec', 'copy',
        '-acodec', 'aac',
        '-strict', '-2',
        output_file
    ], stdout=dev_null, stderr=dev_null)

All it does is to change the audio track to AAC [stereo?], but leaves the video stream as-is.

AAC is not a bad choice for stereo audio, but many Plex clients will trigger transcoding again if you serve them more than 2 channels in AAC.

I don’t know how proficient you are at coding, but I have a ton of python utilities that I use for encoding/tagging/organizing my media in Plex. The code can be found on my github page.

I don’t have an code that you can run on an entire directory or anything, but you could make a simple wrapper script:

#!/usr/bin/env python3
import os
from video_utils.videoconverter import VideoConverter

VC = VideoConverter(outDir = '/path/to/converted/files/')
for root, dirs, items in os.walk(srcDir):
  for item in items:
    if item.endswith( ('.avi', '.mkv') ):
      path = os.path.join(root, item)
      VC.transcode(path)

If you have all the dependencies, and follow my input naming convention, this should:

  • Convert files to MP4 with some-what ideal encoding settings
  • Place movies in /path/to/converted/files/Movies/Movie Name (year)/Movie Name (year).....mp4 matching the Plex convention
  • Place tv shows in /path/to/converted/files/TV Shows/Series Name (year)/Season XX/SxxEyy - Name of episode.....mp4 closely matching the Plex convention
  • Tag mp4 files with data from TVDb or TMDb

Output file names also include extra information such as audio and video codecs and what not.

There are some caveats with my code though:

  • Need FFmpeg
  • Need MediaInfo
  • For metadata tagging and proper output file naming, need TMDb and TVDb API keys
  • Code expects certain file naming on input; have not tested with non-standard names.
1 Like

I will take a look. I spend most of my day coding but mostly in C#. I have done some python. Currently all my media is re-named based on tvdb with tvrenamer so my path and names should all be really good.

Thanks for your help

No problem. I do have an in_place keyword you can set when initializing the VideoConverter class; this should place the file next to the original (with the same name?) with the new extension (.mp4 or .mkv based on mkv keyword).

1 Like

I’m worried that development on this may have stopped, but Tdarr seemed really promising.

The other thing to consider is just using Plex’s “Optimize” function. It has a lot going for it …

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.