How to get Handbrake CLI on your Synology NAS

Challenge

You want to convert mkv movies to m4v for your iDevice but have no access to HandbrakeCLI on your Synology NAS.

Solution

Use docker :-)

Explanation

  • Install in your docker console in DSM a handbrake-cli docker (I used supercoder/docker-handbrake-cli:latest).
  • After getting the image you don’t have to start it. That happens from the command line
  • ssh into your nas (if you don’t know how than you should probably not want to use this tutorial ;-)
  • On the Synology Nas docker runs under the root user so the command needs to be run with sudo rights.
  • Run docker command with the correct settings (see below)
  • Done

for the tech savvy under us this might already be enough but don’t worry I will expand on the above explanation…

In my home folder I created a bin folder that I put on the $PATH

  • login on your nas with ssh then:
1
2
cd ~
mkdir bin
  • Add this line to the .bashrc file in your home dir (create it if it does not exist)
1
export PATH=$PATH:~/bin
  • cd bin
  • create a file called m4v in the bin folder and put the following content in it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# !/bin/sh
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
mkv="$(basename $1)"
map="$(dirname $1)"
m4v="${mkv%.*}.m4v"
m4v="$(basename $m4v)"
docker run \
--cpuset-cpus="1-2" \
--rm \
-v "$(pwd)/$map:/input:ro" \
-v "/volume1/video/m4v/inbox:/output:rw" \
supercoder/docker-handbrake-cli:latest \
-i "/input/$mkv" \
-o "/output/$m4v" \
-e x264 -q 20.0 -r 30 --pfr -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0.0 \
--audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4 \
-X 1280 -Y 720 --loose-anamorphic --modulus 2 -m \
--x264-preset medium --h264-profile high --h264-level 3.1
IFS=$SAVEIFS

This script does the following:

  • Get the first parameter and save it in the mkv variable
  • Also save a m4v variable with the same name but then with the m4v extension
  • Run the docker handbrake image and map the folder you are standing in to the image folder /input and a standard place where you want to save your m4v files to /output then execute the HandbrakeCLI command with all the options given mapping /input/$mkv as input file and wel the rest are handbrake options you can read the manual for :-)
  • This will in effect take the provided movie from the current directory and convert it to an m4v formatted movie in the mapped output folder. Most of the options given are what I found working great on my iDevice and most other devices.
  • After finishing the docker image will close and remove its state automatically
  • Change the rights to the script so it can execute
1
chmod +x m4v

tips / troubleshooting

  • The command must be run as root (sudo) because docker runs as root
  • Run the command under a screen so that you can actually log off when running the “long running” command e.g…
1
2
3
4
5
6
7
8
#start screen
screen -S handbrake
#navigate to the desired folder e.g.
cd /volume1/video/Movies/Suicide.Squad
m4v Suicide.Squad.mkv
# leave screen with ctrl+a+d
# enter screen again with
screen -r handbrake

If done like this you can leave the screen instance with ctrl+a+d and then log off from your ssh session. The command will keep on running…

  • change any and all options as you like :-) but you should probably only have to change /volume1/video/m4v/inbox as that should be a location on you nas
  • if you get access rights errors, make sure that the folders you want to convert from and to have the correct rights.

Multiple files to convert…

create a file in ~/bin called something like m4vAll and give it execute rights (`chmod +x m4vAll) with the following content

1
2
3
4
5
6
#!/bin/sh
find . -iname "*mkv" -print0 | while read -d $'\0' file
do
m4v "$file"
test $? -gt 0 && break;
done

This will find all mkv files from the current folder onwards and convert it with the m4v command. It will accept spaces in names… so just navigate to the folder you want to start from and call sudo m4vAll and it will do its thing ;-)

Have fun and drop a comment if you like the article. A beer is also good…

Github

The code needed for this project can be found on my github.com page.