Roelldev's Blog

Auto convert new files with Handbrake and bash

Juli, 2020

Hey there, building on my previous article on how to convert all media in a folder with Handbrake, we now want to automate the process of converting files if they get added to a watched directory.
This script uses fswatch to watch the file system for changes in a directory.

I will use Ubuntu server and bash to run the script. Off cause, you can run this script on every system that supports fswatch and Handbrake. To automate the script to run in the background, I will use a systemd service. On macOS you could use Launchd as alternative to systemd.

Install

Dependencies to run the script.

Handbrake-cli

sudo apt-get install handbrake-cli 

Fswatch

sudo apt install fswatch

The Script

Create a file lets name it watch-media and edit it.

nano watch-media

#!/bin/bash

pathToWatch=$1
destExt=$2
processedFilesPath=".processed-files";

touch $processedFilesPath;

# -r, is used to watch directories recursively.
# --event Created, only trigger for new created files.
fswatch -0 -r --event Created $pathToWatch | while read -d "" file; do

    filename=$(basename -- "$file");
    extension="${filename##*.}";
    nameWithoutExtension="${file%.*}";

    # Check if file has been processed before.
    if ! grep -Fxq $filename $processedFilesPath; then

        # Check if file is already of dest extension type.
        if [ "$extension" != "$destExt" ]; then
            echo $file
            # echo "" | prevent HandBrake to use same stdin as script
            echo "" | HandBrakeCLI -i "$file" -o "$nameWithoutExtension.$destExt" < /dev/null
            # save processed name into processedFiles to prevent converting same file multiple times.
            echo "$filename" >> $processedFilesPath; 
        else
            echo "${file} is already of ${destExt} format!";
        fi
    else
        echo "${file} already processed!";
    fi
done;

Press ctrl + x then press y and enter to save your file.

Usage

Set execute permissions.

chmod +x watch-media

Watch /media/movies directory for new files and convert them to mp4.

./watch-media /media/movies mp4

Run script as systemd service in the background

To run the script in the background on your server its best to wrap the script into a systemd service. You need admin privileges in order to create the script.

Here is a good tutorial for creating a systemd service: Creating a Linux service with systemd

Create a file inside your sysmtemd folder:

sudo nano /etc/systemd/system/auto-convert-movies.service 

[Unit]
Description=Auto convert files with handbrake
After=network.target
StartLimitIntervalSec=0
[Service]
WorkingDirectory=/path/to/your-script/folder
Type=simple
Restart=always
RestartSec=1
User=john
ExecStart=/bin/bash /path/to/watch-media-script /media/movies mp4

[Install]
WantedBy=multi-user.target

Press ctrl + x then press y and enter to save your file.

Start service.

sudo systemctl start auto-convert-movies

Enable on system startup.

sudo systemctl enable auto-convert-movies

The script is now running in the background and will convert every file that gets moved / created in the /media/movies directory. Let me know if the script worked for you.

I would appreciate some feedback, you can contact me at: roelldev@gmail.com

Source