Custom FFMpeg Script

Unfortunately, the plugins are not enough and the pre-configured video transcoding options are insufficient to satisfy all the needs when converting video files such that more than often it required to just use a custom shell script and provide ffmpeg with custom parameters.

A custom ffmpeg script can be created by installing the "External Worker Processor Script" plugin. The execution type can be set to Bash Script. In the lower text field labeled with "Arguments to pass to the command or script" can be filled with the following:

"{source_file_path}"
"{file_out_path}"
"{data_json_file}"

The parameters at the top can be changed; in particular DATABASE_FILE should be changed to reflect the full path to a file where the bash script will record the status of the videos that have been transcoded.

The script is designed to track the videos that have been transcoded and will not transcode them again given a followup library enumeration.

The conversion makes the following assumptions:

  • the scripts downscale the video to SDTV quality (480p) meant for TV shows and the likes,
  • only the first video and audio stream are preserved with the rest of the streams ripped out (including subtitles, due to preferring Bazarr as a solution and using external subtitled),
  • the scripts use H264 that seems to be the most compatible codec at the time of writing, with the little loss to peak compression
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2023 - License: MIT            ##
###########################################################################
INPUT_FILE="$1"
CONVERTED_FILE="${1%.*}.mp4"
PROCESSING_FILE="${2%.*}.mp4"
JSON_FILE="$3"
DATABASE_FILE=/tmp/unmanic/unmanic.db
###########################################################################
 
if [ -f "$DATABASE_FILE"]; then
    # Check whether the file to transcode already exists.
    echo -n "Checking if file has been already processed: "
    FILE_HASH=`md5sum "$INPUT_FILE"  | awk '{ print $1 }'`
    COUNT=`echo "SELECT COUNT() FROM 'unmanic' WHERE Hash='$FILE_HASH';" | sqlite3 -cmd ".timeout 60000" "file:///tmp/unmanic/$DATABASE_FILE?mode=ro"`
    if [ $COUNT -gt 0 ]; then
        echo "it has. Exiting."
        exit 0
    fi
fi
 
# Intel QSV convert using FFMpeg
#echo "Using FFMpeg to convert file."
#nice -n19 ffmpeg \
#    -hwaccel qsv \
#    -i "$INPUT_FILE" \
#    -map 0:v:0 -map 0:a:0  \
#    -c:v h264_qsv -crf 23 -profile:v baseline -level 3.0 -pix_fmt nv12 \
#    -c:a aac -ac 2 -b:a 128k \
#    -c:s copy \
#    -vf "scale_qsv=w=-1:h=480:format=bgra,hwdownload,format=bgra" \
#    -preset veryfast \
#    -tune zerolatency \
#    -movflags faststart \
#    -movflags separate_moof \
#    "$PROCESSING_FILE"
 
# Software convert (CPU) using FFMpeg
nice -n19 ffmpeg \
    -i "$INPUT_FILE" \
    -map 0:v:0 -map 0:a:0  \
    -c:v h264 -crf 23 -profile:v baseline -level 3.0 \
    -c:a aac -ac 2 -b:a 128k \
    -c:s copy \
    -vf "scale=w=-1:h=480" \
    -preset veryfast \
    -tune zerolatency \
    -movflags faststart -movflags separate_moof \
    "$PROCESSING_FILE"
 
# Check if the file has been converting by comparing the total duration of the video clips.
LFT=`ffprobe -i "$INPUT_FILE" -show_format -v quiet | grep duration | awk -F '=' '{ print $2 }' | xargs printf "%.0f\n"`
RHT=`ffprobe -i "$PROCESSING_FILE" -show_format -v quiet | grep duration | awk -F '=' '{ print $2 }' | xargs printf "%.0f\n"`
echo "LFT vs. RHT size: "$LFT" vs. "$RHT
if [ $LFT -gt $RHT ]; then
    echo "FFMpeg could not convert the file."
    exit 1
fi
 
# Save file as processed to database.
echo "Adding conerted file hash to database."
FILE_HASH=`md5sum "$PROCESSING_FILE" | awk '{ print $1 }'`
# Initialize database if not already done.
echo "CREATE TABLE IF NOT EXISTS 'unmanic' ( Hash STRING PRIMARY KEY );" | sqlite3 -cmd ".timeout 60000" "$DATABASE_FILE"
echo "INSERT INTO 'unmanic' ( Hash ) VALUES ( '$FILE_HASH' );" | sqlite3 -cmd ".timeout 60000" "$DATABASE_FILE"
 
echo "Passing complete file to unmanic..."
cat << EOF > "$JSON_FILE"
{
    "file_out": "$PROCESSING_FILE"
}
EOF

fuss/unmanic.txt ยท Last modified: 2025/05/31 21:29 by 127.0.0.1

Wizardry and Steamworks

© 2025 Wizardry and Steamworks

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.