# Handbrake\_cli settings for 2020

**URL:** <https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255>\
**Category:** General Discussions\
**Tags:** media-optimizer\
**Created:** [November 10, 2020, 7:50pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255 "2020-11-10T19:50:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pricejt](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pricejt/32/96887_2.png) [@pricejt](https://forums.plex.tv/u/pricejt)\
**Post date:** [November 10, 2020, 7:50pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/1 "2020-11-10T19:50:08Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![kwodz](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@kwodz](https://forums.plex.tv/u/kwodz)\
**Post date:** [November 12, 2020, 5:00am UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/2 "2020-11-12T05:00:27Z")

</div>

I would check out [this link](https://handbrake.fr/docs/en/1.3.0/cli/command-line-reference.html) 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!

---

<div class="post-metadata">

**Author:** ![pricejt](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pricejt/32/96887_2.png) [@pricejt](https://forums.plex.tv/u/pricejt)\
**Post date:** [November 12, 2020, 2:08pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/3 "2020-11-12T14:08:46Z")

</div>

Thank you so much for this information.

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

---

<div class="post-metadata">

**Author:** ![OttoKerner](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/ottokerner/32/10079_2.png) [@OttoKerner](https://forums.plex.tv/u/OttoKerner)\
**Post date:** [November 12, 2020, 2:14pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/4 "2020-11-12T14:14:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kwodz](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@kwodz](https://forums.plex.tv/u/kwodz)\
**Post date:** [November 12, 2020, 3:01pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/5 "2020-11-12T15:01:18Z")

</div>

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](https://handbrake.fr/docs/en/latest/workflow/adjust-quality.html) 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 Definition[1](https://handbrake.fr/docs/en/latest/workflow/adjust-quality.html#fn:1)
- RF 19-23 for 720p High Definition[2](https://handbrake.fr/docs/en/latest/workflow/adjust-quality.html#fn:2)
- RF 20-24 for 1080p Full High Definition[3](https://handbrake.fr/docs/en/latest/workflow/adjust-quality.html#fn:3)
- RF 22-28 for 2160p 4K Ultra High Definition[4](https://handbrake.fr/docs/en/latest/workflow/adjust-quality.html#fn:4)

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.

---

<div class="post-metadata">

**Author:** ![pricejt](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pricejt/32/96887_2.png) [@pricejt](https://forums.plex.tv/u/pricejt)\
**Post date:** [November 12, 2020, 3:40pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/6 "2020-11-12T15:40:54Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![OttoKerner](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/ottokerner/32/10079_2.png) [@OttoKerner](https://forums.plex.tv/u/OttoKerner)\
**Post date:** [November 12, 2020, 3:42pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/7 "2020-11-12T15:42:53Z")

</div>

> [@pricejt](#):
>
> ```auto
> '-vcodec', 'copy',
> '-acodec', 'aac',
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![kwodz](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@kwodz](https://forums.plex.tv/u/kwodz)\
**Post date:** [November 12, 2020, 4:31pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/8 "2020-11-12T16:31:06Z")

</div>

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](https://github.com/kwodzicki/video_utils/).

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.

---

<div class="post-metadata">

**Author:** ![pricejt](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/pricejt/32/96887_2.png) [@pricejt](https://forums.plex.tv/u/pricejt)\
**Post date:** [November 12, 2020, 4:38pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/9 "2020-11-12T16:38:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kwodz](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@kwodz](https://forums.plex.tv/u/kwodz)\
**Post date:** [November 12, 2020, 4:57pm UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/10 "2020-11-12T16:57:54Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Volts](https://avatars.discourse-cdn.com/v4/letter/v/a5b964/32.png) [@Volts](https://forums.plex.tv/u/Volts)\
**Post date:** [November 14, 2020, 7:04am UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/11 "2020-11-14T07:04:50Z")

</div>

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

> **[GitHub - HaveAGitGat/Tdarr: Tdarr - Distributed transcode automation using...](https://github.com/HaveAGitGat/Tdarr)**
>
> Tdarr - Distributed transcode automation using FFmpeg/HandBrake + Audio/Video library analytics + video health checking (Windows, macOS, Linux & Docker)

---

<div class="post-metadata">

**Author:** ![Volts](https://avatars.discourse-cdn.com/v4/letter/v/a5b964/32.png) [@Volts](https://forums.plex.tv/u/Volts)\
**Post date:** [November 14, 2020, 7:07am UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/12 "2020-11-14T07:07:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [February 12, 2021, 7:07am UTC](https://forums.plex.tv/t/handbrake-cli-settings-for-2020/652255/13 "2021-02-12T07:07:53Z")

</div>

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