Using Handbrake CLI for video encoding in CentOS

HandBrakeIcon128This tutorial will guide you how to install Handbrake CLI in CentOS and use it for video encoding / converting.

Installation

It is suggested to use LinuxTech repo to install Handbrake CLI on CentOS automatically (Ubuntu can easily add ppa:stebbins/handbrake-releases). First, add the repo at /etc/yum.repos.d/linuxtech.repo as follows:

[bash][linuxtech-release]
name=LinuxTECH.NET el6 main repo
baseurl=http://linuxsoft.cern.ch/linuxtech/el6/release/
http://pkgrepo.linuxtech.net/el6/release/
mirrorlist=http://pkgrepo.linuxtech.net/el6/release/mirrorlist.txt
mirrorlist_expire=7d
enabled=1
gpgcheck=1
gpgkey=http://pkgrepo.linuxtech.net/el6/release/RPM-GPG-KEY-LinuxTECH.NET

[linuxtech-testing]
name=LinuxTECH.NET el6 testing repo
baseurl=http://linuxsoft.cern.ch/linuxtech/el6/testing/
http://pkgrepo.linuxtech.net/el6/testing/
mirrorlist=http://pkgrepo.linuxtech.net/el6/testing/mirrorlist.txt
mirrorlist_expire=7d
enabled=0
gpgcheck=1
gpgkey=http://pkgrepo.linuxtech.net/el6/release/RPM-GPG-KEY-LinuxTECH.NET

[linuxtech-backports]
name=LinuxTECH.NET el6 backports repo
baseurl=http://linuxsoft.cern.ch/linuxtech/el6/backports/
http://pkgrepo.linuxtech.net/el6/backports/
mirrorlist=http://pkgrepo.linuxtech.net/el6/backports/mirrorlist.txt
mirrorlist_expire=7d
enabled=0
gpgcheck=1
gpgkey=http://pkgrepo.linuxtech.net/el6/release/RPM-GPG-KEY-LinuxTECH.NET
priority=200[/bash]

Remember that we add priority=200 to the repo so that this repo will not overwrite any packages of other repos.
Next, we will install Handbrake CLI:

[bash]yum install -y handbrake-cli[/bash]

Usage

We can refer more about its usage at Handbrake CLI guide. Some basic usage is as follows:

  1. Encode a source video located at the path VIDEO_TS to an output file called movie.mp4. It will use x264 (H264 is good for html5 video player) with a CRF (Constant Quality) of 20 to encode the video, and encode the audio as 160 Kbps AAC:

    [bash]HandBrakeCLI -i VIDEO_TS -o movie.mp4 -e x264 -q 20 -B 160[/bash]

  2. Encode video with max-width 480 and optimized for web:

    [bash]HandBrakeCLI -i input_video.mkv -o output_video.mp4 -e x264 -X 480 -O[/bash]

  3. Encode the video with subtitle enabled (remember -t 0 with –subtitle-burn):

    [bash]HandBrakeCLI -t 0 -i input_video.mkv -o output_video.mp4 -e x264 -b 1000 -B 192 -s 1 –subtitle-burn[/bash]

  4. To encode a whole folder, write a bash script (handbrake_folder_convert.sh) as follows:

    [bash]#!/bin/bash

    # Convert automatically a folder of video files to H264 mp4 videos
    # Parameter: SRC: Source folder; DEST: Destination folder
    # The mp4 file will have the max-width of 480 and optimized for web

    SRC=/home/video/
    DEST=/home/www/mp4files/
    DEST_EXT=mp4
    HANDBRAKE_CLI=HandBrakeCLI

    for FILE in `ls $SRC`
    do
    filename=$(basename $FILE)
    extension=${filename##*.}
    filename=${filename%.*}

    $HANDBRAKE_CLI -i $SRC/$FILE -o $DEST/$filename.$DEST_EXT -e x264 -q 22 -r 12 -B 64 -X 480 -O
    sleep 2m #TRICK – To avoid being banned from VPS providers because of CPU abusing ^,^
    done[/bash]

That’s it 🙂 One important note is that converting / encoding video is a very CPU consuming process, so if you do not have the right to use dedicated CPU core(s), do not use it since you will be banned from your hosting provider.

Leave a Reply