CLI arguments for best quality, lowest file size

Discussion of the HandBrake command line interface (CLI)
Forum rules
An Activity Log is required for support requests. Please read How-to get an activity log? for details on how and why this should be provided.
Post Reply
PeterPan669
Posts: 9
Joined: Mon Jan 27, 2020 6:58 pm

CLI arguments for best quality, lowest file size

Post by PeterPan669 »

Hello all,

I am looking for some guidance on how to convert my (mostly) bluray .mkv's to a lower file size, but keeping the best quality. I pass all audio tracks & subtitles.

Also I was wondering on the correct way to utilize hardware support.
So far I came up with this:

Code: Select all

HandbrakeCLI.exe -i "file.mkv" -t 1 -o "file.mkv" -f mkv -O  --decomb -e nvenc_h264 -q 20 --pfr --subtitle 0,1,2,3,4,5,6 --all-audio -E copy --audio-fallback ac3 -R Auto --x264-preset=slow --two-pass --turbo --x264-profile=main --h264-level=4.0 --verbose=0
1. Is "-e nvenc_h264" the correct / optimal way to use hardware support? I used "-e x264" earlier and noticed that it didn't use my GPU's. Are there any downsides compared to the 2 modes?

2. I guess the --x264-preset=slow is the "HQ" profile, correct? Am I using this correct i.c.w. the --quality setting (20), or am I doing unneeded steps here?

3. I can't seem to get the "--two-pass & --turbo" to work; Do I need to specify them and if so, where? Or is this default? In the output it doesn't seem to be using it. Also, is it needed for my goals? I read somewhere that 2-pass would increase quality.

4. Overall, I've taken bits and pieces from the web to form this string. So in all honesty I'm not sure if this is the correct way to do it. e.g. the decomb setting, or --pfr.
Again, my goals are:

- best quality / lowest possible file size (without gimping quality);
- passthrough all audio / subtitle tracks;
- hardware support via my nvidia GPU's;
- speed / time is not a factor; It can take longer if it reaches said goals.

If anyone could take a look and adjust this, that would be grand :D Thank you in advance!

--some output for good measure:

Code: Select all

[20:11:29] Compile-time hardening features are enabled
[20:11:30] Nvenc version 9.1

[20:11:30] hb_init: starting libhb thread
[20:11:30] thread 1 started ("libhb")
HandBrake 1.3.1 (2020010400) - MinGW x86_64 - https://handbrake.fr
8 CPUs detected
Opening --file name removed by OP--.mkv...
[20:11:30] CPU: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
[20:11:30]  - Intel microarchitecture Kaby Lake
[20:11:30]  - logical processor count: 8
[20:11:30] Intel Quick Sync Video support: no
[20:11:30] hb_scan: path=--file name removed by OP--.mkv, title_index=1
udfread ERROR: ECMA 167 Volume Recognition failed
src/libbluray/disc/disc.c:323: failed opening UDF image --file name removed by OP--.mkv
src/libbluray/disc/disc.c:424: error opening file BDMV\index.bdmv
src/libbluray/disc/disc.c:424: error opening file BDMV\BACKUP\index.bdmv
src/libbluray/bluray.c:2585: nav_get_title_list(--file name removed by OP--.mkv\) failed
[20:11:30] bd: not a bd - trying as a stream/file instead
libdvdnav: Using dvdnav version 6.0.1
libdvdread: Encrypted DVD support unavailable.
libdvdread:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.IFO failed
libdvdread:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.BUP failed
libdvdread: Can't open file VIDEO_TS.IFO.
libdvdnav: vm: failed to read VIDEO_TS.IFO
[20:11:30] dvd: not a dvd - trying as a stream/file instead
Input #0, matroska,webm, from '--file name removed by OP--.mkv':
  Metadata:
    encoder         : libebml v1.3.4 + libmatroska v1.4.5
    creation_time   : 2017-05-05T12:17:44.000000Z
  Duration: 00:44:11.44, start: 0.000000, bitrate: 6470 kb/s
    Chapter #0:0: start 0.000000, end 292.542000
    Metadata:
      title           : 00:00:00.000
    Chapter #0:1: start 292.542000, end 549.382000
    Metadata:
      title           : 00:04:52.542
    Chapter #0:2: start 549.382000, end 698.656000
    Metadata:
      title           : 00:09:09.382
    Chapter #0:3: start 698.656000, end 941.691000
    Metadata:
      title           : 00:11:38.656
    Chapter #0:4: start 941.691000, end 1123.414000
    Metadata:
      title           : 00:15:41.691
    Chapter #0:5: start 1123.414000, end 1282.323000
    Metadata:
      title           : 00:18:43.414
    Chapter #0:6: start 1282.323000, end 1423.422000
    Metadata:
      title           : 00:21:22.323
    Chapter #0:7: start 1423.422000, end 1617.824000
    Metadata:
      title           : 00:23:43.422
    Chapter #0:8: start 1617.824000, end 1895.936000
    Metadata:
      title           : 00:26:57.824
    Chapter #0:9: start 1895.936000, end 2119.075000
    Metadata:
      title           : 00:31:35.936
    Chapter #0:10: start 2119.075000, end 2392.223000
    Metadata:
      title           : 00:35:19.075
    Chapter #0:11: start 2392.223000, end 2651.443000
    Metadata:
      title           : 00:39:52.223
    Stream #0:0: Video: hevc (Main 10), yuv420p10le(tv), 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 23.98 tbc (default)
    Metadata:
      title           : HEVC x265 10bit Video / 6000 kbps / 1080p / 23.976 fps / Main 10 Profile 4
      BPS             : 6001844
      BPS-eng         : 6001844
      DURATION        : 00:44:11.357000000
      DURATION-eng    : 00:44:11.357000000
      NUMBER_OF_FRAMES: 63569
      NUMBER_OF_FRAMES-eng: 63569
      NUMBER_OF_BYTES : 1989129075
      NUMBER_OF_BYTES-eng: 1989129075
      _STATISTICS_WRITING_APP: mkvmerge v9.6.0 ('Slave To Your Mind') 64bit
      _STATISTICS_WRITING_APP-eng: mkvmerge v9.6.0 ('Slave To Your Mind') 64bit
      _STATISTICS_WRITING_DATE_UTC: 2017-05-05 12:17:44
      _STATISTICS_WRITING_DATE_UTC-eng: 2017-05-05 12:17:44
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:1(eng): Audio: aac (LC), 48000 Hz, 5.1, fltp (default)
    Metadata:
      title           : LC-AAC / 5.1 / 48 kHz
      BPS             : 458577
      BPS-eng         : 458577
      DURATION        : 00:44:11.434000000
      DURATION-eng    : 00:44:11.434000000
      NUMBER_OF_FRAMES: 124286
      NUMBER_OF_FRAMES-eng: 124286
      NUMBER_OF_BYTES : 151986023
      NUMBER_OF_BYTES-eng: 151986023
      _STATISTICS_WRITING_APP: mkvmerge v9.6.0 ('Slave To Your Mind') 64bit
      _STATISTICS_WRITING_APP-eng: mkvmerge v9.6.0 ('Slave To Your Mind') 64bit
      _STATISTICS_WRITING_DATE_UTC: 2017-05-05 12:17:44
      _STATISTICS_WRITING_DATE_UTC-eng: 2017-05-05 12:17:44
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
    Stream #0:2(eng): Subtitle: hdmv_pgs_subtitle, 1920x1080
    Metadata:
      BPS             : 7826
      BPS-eng         : 7826
      DURATION        : 00:44:08.312000000
      DURATION-eng    : 00:44:08.312000000
      NUMBER_OF_FRAMES: 822
      NUMBER_OF_FRAMES-eng: 822
      NUMBER_OF_BYTES : 2590832
      NUMBER_OF_BYTES-eng: 2590832
      _STATISTICS_WRITING_APP: mkvmerge v9.6.0 ('Slave To Your Mind') 64bit
      _STATISTICS_WRITING_APP-eng: mkvmerge v9.6.0 ('Slave To Your Mind') 64bit
      _STATISTICS_WRITING_DATE_UTC: 2017-05-05 12:17:44
      _STATISTICS_WRITING_DATE_UTC-eng: 2017-05-05 12:17:44
      _STATISTICS_TAGS: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
      _STATISTICS_TAGS-eng: BPS DURATION NUMBER_OF_FRAMES NUMBER_OF_BYTES
[20:11:30] scan: decoding previews for title 1
[20:11:30] scan: audio 0x1: aac, rate=48000Hz, bitrate=1 English (AAC LC) (5.1 ch)
Scanning title 1 of 1, preview 9, 90.00 %[20:11:31] scan: 10 previews, 1920x1080, 23.976 fps, autocrop = 0/0/0/0, aspect 16:9, PAR 1:1
[20:11:31] scan: supported video decoders: avcodec qsv
[20:11:31] libhb: scan thread found 1 valid title(s)
+ Using preset: CLI Default
+ title 1:
  + stream: --file name removed by OP--.mkv
  + duration: 00:44:11
  + size: 1920x1080, pixel aspect: 1/1, display aspect: 1.78, 23.976 fps
  + autocrop: 0/0/0/0
  + chapters:
    + 1: duration 00:04:53
    + 2: duration 00:04:17
    + 3: duration 00:02:29
    + 4: duration 00:04:03
    + 5: duration 00:03:02
    + 6: duration 00:02:39
    + 7: duration 00:02:21
    + 8: duration 00:03:14
    + 9: duration 00:04:38
    + 10: duration 00:03:43
    + 11: duration 00:04:33
    + 12: duration 00:04:19
  + audio tracks:
    + 1, English (AAC LC) (5.1 ch) (iso639-2: eng)
  + subtitle tracks:
    + 1, English [PGS]
Warning: Could not find subtitle track 0, skipped
Warning: Could not find subtitle track 2, skipped
Warning: Could not find subtitle track 3, skipped
Warning: Could not find subtitle track 4, skipped
Warning: Could not find subtitle track 5, skipped
Warning: Could not find subtitle track 6, skipped
[20:11:31] Starting work at: Mon Jan 27 20:11:31 2020

[20:11:31] 1 job(s) to process
[20:11:31] Starting Task: Encoding Pass
[20:11:31] Skipping crop/scale filter
[20:11:31] Auto Passthru: allowed codecs are AAC, AC3, E-AC3, TrueHD, DTS, DTS-HD, MP3, FLAC
[20:11:31] Auto Passthru: fallback is AC3
[20:11:31] Auto Passthru: using AAC Passthru for track 1
[20:11:31] job configuration:
[20:11:31]  * source
[20:11:31]    + --file name removed by OP--.mkv
[20:11:31]    + title 1, chapter(s) 1 to 12
[20:11:31]    + container: matroska,webm
[20:11:31]    + data rate: 6470 kbps
[20:11:31]  * destination
[20:11:31]    + J:\HANDBRAKE_TEMP\--file name removed by OP--.mkv
[20:11:31]    + container: Matroska (libavformat)
[20:11:31]      + chapter markers
[20:11:31]  * video track
[20:11:31]    + decoder: hevc
[20:11:31]    + filters
[20:11:31]      + Decomb (mode=7)
[20:11:31]      + Framerate Shaper (mode=2)
[20:11:31]        + frame rate: 23.976 fps -> peak rate limited to 23.976 fps
[20:11:31]    + Output geometry
[20:11:31]      + storage dimensions: 1920 x 1080
[20:11:31]      + pixel aspect ratio: 1 : 1
[20:11:31]      + display dimensions: 1920 x 1080
[20:11:31]    + encoder: H.264 (NVEnc)
[20:11:31]      + preset:  slow
[20:11:31]      + profile: main
[20:11:31]      + level:   4.0
[20:11:31]      + quality: 20.00 (CQ)
[20:11:31]      + color profile: 1-1-1
[20:11:31]  * subtitle track 1, English [PGS] (track 0, id 0x2, Picture) -> Passthrough
[20:11:31]  * audio track 1
[20:11:31]    + name: LC-AAC / 5.1 / 48 kHz
[20:11:31]    + decoder: English (AAC LC) (5.1 ch) (track 1, id 0x1)
[20:11:31]      + samplerate: 48000 Hz
[20:11:31]    + AAC Passthru
[20:11:31] sync: expecting 63571 video frames
[20:11:31] encavcodecInit: H.264 (Nvidia NVENC)
[20:11:31] encavcodec: encoding at rc=vbr 20.00
[20:11:31] encavcodec: encoding with stored aspect 1/1
[20:11:31] sync: first pts video is 0
[20:11:31] sync: "00:00:00.000" (1) at frame 1 time 0
[20:11:31] sync: first pts audio 0x1 is 810
[20:11:31] sync: first pts subtitle 0x2 is 101430


mduell
Veteran User
Posts: 6926
Joined: Sat Apr 21, 2007 8:54 pm

Re: CLI arguments for best quality, lowest file size

Post by mduell »

1) Since you're talking about "best quality, lowest file size", you can forget the hardware encoders immediately. They're fast, they are not efficient.

2) Sure, slow is the HB team's reasonable suggestion for best balancing encoding speed vs efficiency for that preset. Your personal preference may disagree in either direction. The primary control for quality is the RF target, not the encoder preset.

3) For quality based encoding there's no need for either.

4) If you really want best quality at lowest file size, use the encoder preset veryslow. If you want to use your nVidia GPUs, forget about best quality at low file size.
Decomb without comb detection will just result in deinterlacing every frame. If you want decomb to work as expected, you need comb detection on.

PeterPan669
Posts: 9
Joined: Mon Jan 27, 2020 6:58 pm

Re: CLI arguments for best quality, lowest file size

Post by PeterPan669 »

Hello Mduell,

Thanks for clearing a few things up!
Based on your advice:
mduell wrote:
Mon Jan 27, 2020 7:39 pm
1) Since you're talking about "best quality, lowest file size", you can forget the hardware encoders immediately. They're fast, they are not efficient.
- set the argument back to "-e x264";
mduell wrote:
Mon Jan 27, 2020 7:39 pm
3) For quality based encoding there's no need for either.
-removed the "2-pass / turbo" switches;
mduell wrote:
Mon Jan 27, 2020 7:39 pm
4) If you really want best quality at lowest file size, use the encoder preset veryslow. If you want to use your nVidia GPUs, forget about best quality at low file size.
Decomb without comb detection will just result in deinterlacing every frame. If you want decomb to work as expected, you need comb detection on.
- removed the "decomb" switch;

So far, my new command looks like this:

Code: Select all

HandbrakeCLI.exe -i "file.mkv" -t 1 -o "file.mkv" -f mkv -O -e x264 -q 20 --pfr --subtitle 0,1,2,3,4,5,6 --all-audio -E copy --audio-fallback ac3 -R Auto --x264-preset=veryslow --x264-profile=main --h264-level=4.0 --verbose=0
mduell wrote:
Mon Jan 27, 2020 7:39 pm
2) Sure, slow is the HB team's reasonable suggestion for best balancing encoding speed vs efficiency for that preset. Your personal preference may disagree in either direction. The primary control for quality is the RF target, not the encoder preset.
- Does this mean that the preset is not required at all? So I only need to set the -quality ?
Or based on your earlier commend, should I go with the "--x264-preset=veryslow" and drop the "-q 20" ?
- According to the --help, -O is only for .MP4, correct? So I might as well drop that altogether?
- Are the "--x264-profile=main / --h264-level=4.0" settings of any use?
- Any comments on the --pfr setting?


Thanks again! 8)

PeterPan669
Posts: 9
Joined: Mon Jan 27, 2020 6:58 pm

Re: CLI arguments for best quality, lowest file size

Post by PeterPan669 »

PeterPan669 wrote:
Mon Jan 27, 2020 7:59 pm
- According to the --help, -O is only for .MP4, correct? So I might as well drop that altogether?
- Are the "--x264-profile=main / --h264-level=4.0" settings of any use?
- Any comments on the --pfr setting?
I've read a bit on these questions, and if I'm not mistaken:

- drop the -O as it is only for MP4;
- the 'main' profile is more or less a preset of available arguments. Best leave it auto, or maybe better, don't specify it at all (https://handbrake.fr/docs/en/1.0.0/tech ... evels.html);
- same goes for the 4.0 level; Best leave it out I guess (https://en.wikipedia.org/wiki/Advanced_ ... ing#Levels);
- I think best to leave this clear as well, since this setting (--pfr) isn't doing anything without -r (which is not specified) and the --vfr is default if nothing is specified, which means keep the source timings.

-- Unless I'm making a grave error on any of the above, so far I came up with this:

Code: Select all

HandbrakeCLI.exe -i "file.mkv" -t 1 -o "file.mkv" -f mkv -e x264 -q 20 --subtitle 0,1,2,3,4,5,6 --all-audio -E copy --audio-fallback ac3 -R Auto --x264-preset=veryslow --verbose=0

Thus, my only still standing question:
Should I specify the 'quality' setting, or is the 'x264-preset=veryslow' enough to reach my goals?
E.g. final command:

Code: Select all

HandbrakeCLI.exe -i "file.mkv" -t 1 -o "file.mkv" -f mkv -e x264 --subtitle 0,1,2,3,4,5,6 --all-audio -E copy --audio-fallback ac3 -R Auto --x264-preset=veryslow --verbose=0
Thanks once more! :D

mduell
Veteran User
Posts: 6926
Joined: Sat Apr 21, 2007 8:54 pm

Re: CLI arguments for best quality, lowest file size

Post by mduell »

PeterPan669 wrote:
Mon Jan 27, 2020 7:59 pm
- Does this mean that the preset is not required at all? So I only need to set the -quality ?
Or based on your earlier commend, should I go with the "--x264-preset=veryslow" and drop the "-q 20" ?
- According to the --help, -O is only for .MP4, correct? So I might as well drop that altogether?
- Are the "--x264-profile=main / --h264-level=4.0" settings of any use?
- Any comments on the --pfr setting?
If you don't specify an encoder preset, you'll get the default (medium). Medium is not as efficient (quality for size) as veryslow. You said you want the best quality for size and encode time was not a consideration.

Yes, -O is for MP4, so you should drop it if you use MKV.

Unless you have a specific constraint in your playback environment that you know of requiring a specific profile or level, then you should not specify them.

PFR is fine if your playback environment supports variable framerate. But without a framerate limitation, it's quite pointless.

PeterPan669
Posts: 9
Joined: Mon Jan 27, 2020 6:58 pm

Re: CLI arguments for best quality, lowest file size

Post by PeterPan669 »

Sweet, thx!
I dropped the redundant arguments and went with the veryslow preset. I did notice that without --quality, the RF is set at 22. I added --quality 20.
Not sure if that's making a lot of difference in the end quality, or how that relates to the preset tho. I'll compare output size, time and quality of both settings. So far it'll take 2 hours instead of the 20 minutes I had on the fast setting :lol:

Thanks again for your help Mduell!

User avatar
BradleyS
Moderator
Posts: 1769
Joined: Thu Aug 09, 2007 12:16 pm

Re: CLI arguments for best quality, lowest file size

Post by BradleyS »

https://handbrake.fr/docs/en/latest/cli ... rence.html

Some of the options you're using don't exist in HandBrake. You probably mean --encoder-preset and the like.

PeterPan669
Posts: 9
Joined: Mon Jan 27, 2020 6:58 pm

Re: CLI arguments for best quality, lowest file size

Post by PeterPan669 »

Hi Bradley,

I noticed, but they seem to work;
--x264-preset=veryslow, or --x264-preset=slow, or --x264-preset=fast will actually change the behavior (visible in the output) ad do the level and profile settings.

User avatar
BradleyS
Moderator
Posts: 1769
Joined: Thu Aug 09, 2007 12:16 pm

Re: CLI arguments for best quality, lowest file size

Post by BradleyS »

https://github.com/HandBrake/HandBrake/ ... st.c#L2286

That's because we kept them around for compatibility. They're quite old and deprecated so I recommend the newer syntax.

PeterPan669
Posts: 9
Joined: Mon Jan 27, 2020 6:58 pm

Re: CLI arguments for best quality, lowest file size

Post by PeterPan669 »

Aah ok, figures :)
Cool, will do so. I'm only using the preset (removed the profile / level), so the new command I use now is --encoder-preset=veryslow.
Thanks for letting me know.

PeterPan669
Posts: 9
Joined: Mon Jan 27, 2020 6:58 pm

Re: CLI arguments for best quality, lowest file size

Post by PeterPan669 »

To close the loop:
Preset "fast" gives away too much (visible quality).
Preset Slow: good quality, ~40 minutes for a 45 minute movie; size: 1600 mb.
Preset Very Slow: good quality (no visible difference from the 'slow' preset), ~2 hours and 20 minutes(!) for the same 45 minute movie; size: 1400 mb.
--
In my case, since time isn't a problem since I created unattended scripts for this, the very slow is good. It has the best quality preserved, and I like the 200 mb smaller file size in exchange for nearly 2 hours extra encoding. Maybe for most other people, the 'slow' preset is best due to the lower encoding time required.

Thanks all for the good help!

mduell
Veteran User
Posts: 6926
Joined: Sat Apr 21, 2007 8:54 pm

Re: CLI arguments for best quality, lowest file size

Post by mduell »

Note the quality isn't quite the same at a fixed RF target across the different speed presets.

PeterPan669
Posts: 9
Joined: Mon Jan 27, 2020 6:58 pm

Re: CLI arguments for best quality, lowest file size

Post by PeterPan669 »

Understood. Just that comparing it 'by eye' and also my own (in)capabilities, I couldn't notice anything that disturbed me. Ofc, I understand YMMV.

User avatar
BradleyS
Moderator
Posts: 1769
Joined: Thu Aug 09, 2007 12:16 pm

Re: CLI arguments for best quality, lowest file size

Post by BradleyS »

Glad you found something you like.

You might find this interesting: https://handbrake.fr/docs/en/1.3.0/tech ... mance.html

One thing to note, while slower encoder presets result in smaller file sizes with x264 (which seems sane), the opposite is true with x265 (ummm). ;)

PeterPan669
Posts: 9
Joined: Mon Jan 27, 2020 6:58 pm

Re: CLI arguments for best quality, lowest file size

Post by PeterPan669 »

Good read! The one thing stopping me from actually using x265 is that I'm not certain my HTPC supports the playback. Think I'll go and test it today :wink: .
What's your personal experience with x264 vs. x265?

User avatar
BradleyS
Moderator
Posts: 1769
Joined: Thu Aug 09, 2007 12:16 pm

Re: CLI arguments for best quality, lowest file size

Post by BradleyS »

x265 is good at two things: low bit rate encodes, and Ultra HD encodes.

Post Reply