Use HandBrakeCLI to convert Videos files to mp4 in Ubuntu Server (via ssh command line)

1. Download and Install HandBrake CLI
http://handbrake.fr/downloads2.php

2. How to use -- my case as example

My video file located at: ~/video/mvi_0805.mov

cd ~/video

HandBrakeCLI -i mvi_0805.mov -o mvi_0805.mp4 -e x264 -q 22 -r 15 -B 64 -X 480 -O

MP4 outcome:
H264 video, Framerate 15, Video Bitrate: 406 Kb/s, Dimentions: 480 x 272, progressive downloads.
FFaac audio, 2 channels, 63 Kb/s, 48 KHz

Commandline Explained:

  • -i original file
  • -o convert to destination file
  • -e x264: video format H264
  • -q or --quality: controls the video quality.(RF 0 applies no compression. RF 20 throws away detail. I use 22 for web mp4. 20 or 22 are good choices)
  • -r or --rate: controls the video framerate, or FPS. Your options are 5, 10, 12, 15, 23.976, 24, 25, or 29.97. I use 15. I also use 10 for less motion video.
  • -B or --ab: allows you to set an average audio bitrate in kilobits per second. 64 is good for stereo, you can use 129 for music.
  • -X or --maxWidth: sets an upper boundary on the width. Smaller values will be permitted, but anything larger will be scaled down and the height adjusted to match the film's aspect ratio.)
  • -O or --optimize: Rearranges MP4 files so they play better over the web as progressive downloads.

CLI Guide:
https://trac.handbrake.fr/wiki/CLIGuide

One can create a script to convert one folder automatically.
http://www.davelachapelle.ca/guides/handbrake-cli-encoding/
I made one:

#!/bin/bash

# this script is to convert automatically a folder of video files to mp4
# You need to change SRC -- Sourse folder and DEST -- Destination folder
# The mp4 format is 480x270

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
done

One also can schedule to convert different folders of video files to mp4 in certain Days Sequence at crontab

I scheduled to convert 45 hours lectures videos to mp4 in three days.

titles => 5 min

Here's a way to get a list of the title #'s that are 5 minutes or greater.. this can be used in a loop to convert multiple titles per disk
titlenos=$($HANDBRAKE_CLI -t 0 -i "$DEST/$filename" 2>&1 |grep 'scan: duration'|grep -n '^'|sort -k 5|while read title; do if (( 299000 < $(sed 's/^.*(\([0-9]\+\) ms.*$/\1/g' <<<"$title") )); then echo "$title"|awk -F":" '{print $1}'; fi; done|sort -V)

script for pile of ISOs

Thank you, I had a need to convert a bunch of ISOs in a directory structure and was able to use your script to work something out

FWIW

# You need to change SRC -- Sourse folder and DEST -- Destination folder
# The mp4 format is 480x270

SRC=/source/
DEST=/destination
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI

while read FILE; do
filename=$(basename "$FILE")
dname=$(dirname "$FILE")
extension=${filename##*.}
filename=${filename%.*}
mkdir "$DEST/$filename" || :
sudo mount -o loop "$FILE" "$DEST/$filename"
titleno=$($HANDBRAKE_CLI -t 0 -i "$DEST/$filename" 2>&1|grep 'scan: duration'|grep -n '^'|sort -k 5|tail -n 1|awk -F":" '{print $1}') # use the longest title..
$HANDBRAKE_CLI -t $titleno -i "$DEST/$filename" -o "$DEST/$filename.$DEST_EXT" -e x264 -q 22 -r 12 -B 64 -X 480 -O
sudo umount "$DEST/$filename"
done< <(find $SRC -name "*.iso")

I tried handbrake to convert

I tried handbrake to convert my 2 minute 300+MB .MOV file that I recorded using and copied from my actioncam.

I was unsuccessul. Posting the output in case someone has an idea.

$ HandBrakeCLI -i REC_0024.MOV -o REC_0024.mp4 -e x264 -q 22 -r 30 -B 64 -X 1300 -O
[11:14:03] hb_init: starting libhb thread
HandBrake rev5474 (2014032499) - Linux x86_64 - <
>
4 CPUs detected
Opening REC_0024.MOV...
[11:14:03] hb_scan: path=REC_0024.MOV, title_index=1
libbluray/bdnav/index_parse.c:162: indx_parse(): error opening REC_0024.MOV/BDMV/index.bdmv
libbluray/bdnav/index_parse.c:162: indx_parse(): error opening REC_0024.MOV/BDMV/BACKUP/index.bdmv
libbluray/bluray.c:2182: nav_get_title_list(REC_0024.MOV) failed
[11:14:03] bd: not a bd - trying as a stream/file instead
libdvdnav: Using dvdnav version 4.2.1
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.IFO failed
libdvdnav:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.BUP failed
libdvdread: Can't open file VIDEO_TS.IFO.
libdvdnav: vm: failed to read VIDEO_TS.IFO
[11:14:03] dvd: not a dvd - trying as a stream/file instead
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'REC_0024.MOV':
Metadata:
major_brand : qt
minor_version : 0
compatible_brands: qt
creation_time : 2004-01-01 00:00:00
Duration: 00:02:19.00, start: 0.000000, bitrate: 18359 kb/s
Stream #0.0(eng): Video: h264 (High), yuv420p, 1920x1080, 17697 kb/s, 30 fps, 30 tbr, 30k tbn
Metadata:
creation_time : 2004-01-01 00:00:00
Stream #0.1(eng): Audio: pcm_s16le, 32000 Hz, 1 channels, s16, 512 kb/s
Metadata:
creation_time : 2004-01-01 00:00:00
[11:14:04] scan: decoding previews for title 1
[11:14:04] scan: audio 0x1: pcm_s16le, rate=32000Hz, bitrate=512000 English (pcm_s16le) (1.0 ch)
Scanning title 1 of 1, preview 10, 100.00 %[11:14:05] scan: 10 previews, 1920x1080, 30.000 fps, autocrop = 0/0/0/0, aspect 16:9, PAR 1:1
[11:14:05] libhb: scan thread found 1 valid title(s)
+ title 1:
+ stream: REC_0024.MOV
+ duration: 00:02:19
+ size: 1920x1080, pixel aspect: 1/1, display aspect: 1.78, 30.000 fps
+ autocrop: 0/0/0/0
+ chapters:
+ 1: cells 0->0, 0 blocks, duration 00:02:19
+ audio tracks:
+ 1, English (pcm_s16le) (1.0 ch) (iso639-2: eng)
+ subtitle tracks:
[11:14:05] Width out of bounds, scaling down to 1300
[11:14:05] New dimensions 1296 * 736
[11:14:05] 1 job(s) to process
[11:14:05] starting job
[11:14:05] work: mixdown not specified, track 1 setting mixdown Mono
[11:14:05] sync: expecting 4170 video frames
ERROR: Invalid audio codec: 0x100
[11:14:05] render: 0 frames output, 0 dropped and 0 duped for CFR/PFR
[11:14:05] render: lost time: 0 (0 frames)
[11:14:05] render: gained time: 0 (0 frames) (0 not accounted for)
[11:14:05] libhb: work result = 0

Encode done!

HandBrake has exited.

error file names with spaces

I kept getting an error because my file names had spaces. I added this below the variables section;

IFS=$(echo -en "\n\b")

Hope it helps someone.

To fix spaces problems

Hi,
To fix spaces problems, you could use a double quote :

"$FILENAME"
instead of
$FILENAME

Excuse my ignorance but can't

Excuse my ignorance but can't figure out exactly where is that "$filename" or $filename in the following script:

#!/bin/bash
SRC=~/hbCLI/src
DEST=~/hbCLI/dest
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI
PRESET="Fast 1080p30"
for FILE in `ls $SRC`
do
filename=$(basename $FILE)
extension=${filename##*.}
filename=${filename%.*}
$HANDBRAKE_CLI -i $SRC/$FILE -o $DEST/$filename.$DEST_EXT --preset="$PRESET" --optimize
done

Try this

#!/bin/bash
SRC=~/hbCLI/src
DEST=~/hbCLI/dest
DEST_EXT=mp4
HANDBRAKE_CLI=HandBrakeCLI
PRESET="Fast 1080p30"
for FILE in `ls $SRC`
do
filename=$(basename $FILE)
extension=${filename##*.}
filename=${filename%.*}
$HANDBRAKE_CLI -i "$SRC/$FILE" -o "$DEST/$filename.$DEST_EXT" --preset="$PRESET" --optimize
done

I've added double quotes (") around both filenames.

I have a similar script. To

I have a similar script. To make it easier, I look at HandBrake presets to figure out what is available to me, so we do not need to re-invent the wheel.

HandBrakeCli --preset-list

Then in the script, define a variable to contain the preset you need.

#ripto="Classic"
#ripto="AppleTV"
ripto="iPad"
#ripto="iPod"

I just have a few listed, and I comment and uncomment as needed.

The call to HandBrakeCLI is is invoke as:

$HANDBRAKECLI --optimize --preset "$ripto" -i $SRC/$FILE -o $DEST/$filename.DEST_EXT

I use the --optimize option for web streaming (plex, etc).

Hope that helps!

-K

does that work on numerous

does that work on numerous types of files that need converting, ie mkv/avi etc ?