HandBrakeCLI

HandBrake is a free open-source video transcoder. It's goal is to help people rip their DVDs and save them to a file on their computer. Although there are ways to extract Blu-rays using a 3rd party application, I will not covert this in details over here. In my case I like to use the MakeMKV software which allows to do a preprocess and remove the Audio and subtitle tracks I don't need.

HandBrake is available for Windows, OS X and for many Linux distributions. For this last one, you can even use the command lines to convert your videos. This is what we will look at this this tutorial.

Getting the HandBrakeCLI tool

Alright, first thing first, let's download and install the CLI version of HandBrake. To do so, use the following command

On a Debian base distribution, do the following.
apt-get install handbrake-cli

On ArchLinux based distibution, do the following.
pacman -S handbrake-cli

You can access the latest built throught the AUR (Arch User Repository)
apacman -S handbrake-cli-git

Scanning the content with HandBrakeCLI

HandBrake can now be access through command line. Although, make sure you type the following using the H, B, C, L and I capital letters.

HandBrakeCLI

Now lets look at some flags. HandBrake is pretty explicit with his flag which is really great. They almost use the keyboard twice (so many settings!!!!). The two followings are mostly generic flags. They will allow you to get in the help/manual, check if there is an update, select your input device/file name and output file name.

-h	help
-u	check for updates

-i	Input file name or input device (i.e. DVD Player)
-o	Output file name and location

Let say we want to scan the input to know what is the content. We may do this by two ways. I tend to find -t misleading if used with a 0 so I prefer the second option.

-t	title to encode (by using 0 it will automatically can the input)
--scan	scan the input and display its information
--min-duration	Minimum duration (in seconds) a video needs to before being scanned


Starting from here, I'll use the 2K version of Sintel to illustrate the commands (you may download the 4K version for free, it's amazing to watch. Keep in mind it was made in 2010 using Blender).

Analysing the --scan output

HandBrakeCLI -i sintel-2k.mp4 --scan

  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2010-09-29 12:52:03
  Duration: 00:14:47.99, start: 0.000000, bitrate: 3660 kb/s
    Stream #0.0(und): Video: h264 (High), yuv420p, 2048x872 [PAR 1:1 DAR 256:109], 3526 kb/s, 24 fps, 1k tbn, 48 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
    Stream #0.1(und): Audio: aac, 44100 Hz, stereo, fltp, 127 kb/s (default)
    Metadata:
      creation_time   : 2010-09-29 12:52:24
[21:41:02] scan: decoding previews for title 1
Scanning title 1 of 1, preview 1, 10.00 %[21:41:02] scan: audio 0x1: aac, rate=44100Hz, bitrate=127998 Unknown (AAC) (2.0 ch)
Scanning title 1 of 1, preview 10, 100.00 %[21:41:02] scan: 10 previews, 2048x872, 24.000 fps, autocrop = 0/0/0/0, aspect 2.35:1, PAR 1:1
[21:41:03] libhb: scan thread found 1 valid title(s)
+ title 1:
  + stream: /media/hd1/Videos/Movies-Eng/Sintel (2010).mp4
  + duration: 00:14:47
  + size: 2048x872, pixel aspect: 1/1, display aspect: 2.35, 24.000 fps
  + autocrop: 0/0/0/0
  + support opencl: no
  + support hwd: not built-in
  + chapters:
    + 1: cells 0->0, 0 blocks, duration 00:14:47
  + audio tracks:
    + 1, Unknown (AAC) (2.0 ch) (iso639-2: und)
  + subtitle tracks:


Alright, what is all this? Let's look in short pieces at a time.

The following might be the first information you want to know about your video.

Duration: 00:14:47.99, start: 0.000000, bitrate: 3660 kb/s
    Stream #0.0(und): Video: h264 (High), yuv420p, 2048x872 [PAR 1:1 DAR 256:109], 3526 kb/s, 24 fps, 1k tbn, 48 tbc (default)

You can see that the length of the video is 14:48. That could be something important when you scan a DVD. It will allow you to detect what file is the main title. If you get the extened edition of a movie, you may see one with more time. That is the one you want to use.

An other good information is the video bitrate of 3526 kbps and the 24 frame per seconds (fps)

Scanning title 1 of 1, preview 1, 10.00 %[21:41:02] scan: audio 0x1: aac, rate=44100Hz, bitrate=127998 Unknown (AAC) (2.0 ch)
Scanning title 1 of 1, preview 10, 100.00 %[21:41:02] scan: 10 previews, 2048x872, 24.000 fps, autocrop = 0/0/0/0, aspect 2.35:1, PAR 1:1


In the first line we can the the audio information. This audio has a frequency of 44.1 KHz with a bitrate of 128 kbits. They used the AAC encoding for a sterio mode (2.0 ch). The Second line shows some new information about the video like the aspect ratio and the croping done by HandBrake. The cropping is used to remove a number of lines on each edge of the video when you get annoying black of green stripe this is the way to remove them. The crop order is the following: top/bottom/left/right.

+ chapters:
    + 1: cells 0->0, 0 blocks, duration 00:14:47
  + audio tracks:
    + 1, Unknown (AAC) (2.0 ch) (iso639-2: und)
  + subtitle tracks:


When you ripped a movie from a DVD or Bluray, you may want to only check those last lines. They will tell your the audio and subtitle track numbers and language (we will look at those in the next section).

Transcoding your video

You now know all the information you need to know about your video. Let's look in detail in all the features you may want to play with.

Video flags

-f	 format (av_mp4, av_mkv)
-e	 encoder (x264, x265, mpeg4, mpeg2, VP8, Theora)

-q	 quality (default = 20, min = 0, max = 51)
 or
-b	 video bitrate
-2 (opt) Uses 2 pass before encoding the video
-T (opt) Activate the turbo mode on the first pass

--vfr	 Variable Frame Rate (preserve the source timing)
 or
--cfr	 Constant Frame Rate (use the -r flag to define the new rate for your video)

This example shows how to convert the video in x265 format with a quality of 21. For a DVD, it's recommended to use a quality between 18 and 20, and for a Blu-ray a quality between 19 and 21. The higher the quality number, the more compress is the image (so a bigger lost in the final quality).

HandBrakeCLI -i sintel-2k.mp4 -f av_mkv -e x265 -q 21 --vfr -o sintel.mkv

This example shows how to convert a video in mpeg4 format with a bitrate of 3000

HandBrakeCLI -i sintel-2k.mp4 -f av_mp4 -e mpeg4 -b 3000 -2 -T --cfr 23.976 -o sintel.mp4

Audio flags

-a	audio tracks (specify the order of your audio tracks seperated by commas. i.e. 1, 3, 2)
-E	audio encoder  (av_aac, fdk_aac, fdk_haac, copy:aac, 
			ac3, copy:ac3,
			copy:dts, copy:dtshd,
                        mp3, copy:mp3,
                        vorbis
                        flac16, flac24,
                        copy)
		Note : copy:* will passthrough if it's a supported format.
		Defaults : av_mp4	av_aac
			   av_mkv	mp3

-B	Audio bitrate (each encoder hage a different min and max bitrate)
 or
-Q	Audio quality (each encoder have a different min and max quality)

-6 (opt) Mix down (mono, left_only, right_only, stereo, dpl1, dpl2, 5point1, 6point1, 7point1, 5_2_lfe)
-R (opt) Audio rate sample rate (8, 11.025, 12, 16, 22.05, 24, 32, 44.1, 48 KHz)
-A (opt) Track name ("English", "French", ...)

This example shows how to convert the audio using a passthrough

HandBrakeCLI -i sintel-2k.mp4 -a "1" -E "copy:acc" -A "English" -o sintel.mp4

Suppose we had the Spanish audio for the video and wanted to quality on the English track

HandBrakeCLI -i sintel-2k.mp4 -a "2,1" -E "copy:*,mp3" -6 "5point1,sterio" -B "default,128" -o sintel.mp4

Subtitle flags

-s	 Subtible track (specify the order of your subtitles separeted by commas.
-S (opt)

If you have subtitle to your input file, here is how you add them to your output file

HandBrakeCLI -i sintel-2k.mp4 -s "1,3,2,5" -S "English,Espanol,Francais,Nihongo" -o sintel.mkv

Sources

Links

Advertisement