From 9c7cbe1554e789f0f702596b4426f50a81f9f77e Mon Sep 17 00:00:00 2001 From: Glahera Date: Mon, 15 Apr 2024 08:27:33 +0700 Subject: [PATCH] Initial Commit - This script was already done before this repo --- Dockerfile | 3 ++ queue.sh | 77 ++++++++++++++++++++++++++++++++++++++++++ transcode.sh | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 Dockerfile create mode 100644 queue.sh create mode 100644 transcode.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6b5b95e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM jrottenberg/ffmpeg:4.1-alpine +RUN apk update && apk add bc redis curl unzip && mkdir rclone && cd rclone && curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip && unzip rclone-current-linux-amd64.zip && cd rclone-*-linux-amd64 && cp rclone /usr/bin/ && chown root:root /usr/bin/rclone && chmod 755 /usr/bin/rclone && rm -r rclone +CMD [redis-server] diff --git a/queue.sh b/queue.sh new file mode 100644 index 0000000..55d4e46 --- /dev/null +++ b/queue.sh @@ -0,0 +1,77 @@ +!#/bin/bash + +#Author: Glahera Matebi +#Description: This script is for sending variables from Radarr and Sonarr to Redis Server, which is then picked up by worker container then processed + +#Defining Redis' host: + +REDIS_CLI="redis_cli -h redis:6379" + +#Defining send function: + +#parse(){echo "LPUSH $prog \"$(echo '$file' | base64)\" | $REDIS_CLI} +parse() { + ##File Duration in Seconds + for_dur=$(ffprobe -hide_banner -v 0 -of default=nw=1:nk=1 -show_entries 'format=duration' -i "$file") + ##Video Codec + v_in_codec=$(ffprobe -hide_banner -v 0 -of default=nw=1:nk=1 -select_streams v:0 -show_entries 'stream=codec_name' -i "$file") + echo_log "Input video codec is $v_in_codec" + ##Audio Codec + a_in_codec=$(ffprobe -hide_banner -v 0 -of default=nw=1:nk=1 -select_streams a:0 -show_entries 'stream=codec_name' -i "$file") + ##Video Stream Size in Bits + v_size=$(ffprobe -v error -select_streams v:0 -show_entries packet=size -of default=nw=1:nk=1 "$file" | awk '{s+=$1*8} END {printf "%.0f\n", s}') + echo_log "Input video stream size is $v_size bits" + ##Audio Stream Size in Bits + a_size=$(ffprobe -v error -select_streams a:0 -show_entries packet=size -of default=nw=1:nk=1 "$file" | awk '{s+=$1*8} END {printf "%.0f\n", s}') + echo_log "Input audio stream size is $a_size bits" + ##File Duration in Seconds + #for_dur=$(ffprobe -hide_banner -v 0 -of default=nw=1:nk=1 -show_entries 'format=duration' -i "$file") + ##Video Bitrate in bps + v_in_rate=$(echo $v_size / $for_dur | bc) + echo_log "Input video bitrate is $v_in_rate bits per second" + ##Audio Bitrate in bps + a_in_rate=$(echo $a_size / $for_dur | bc) + echo_log "Input audio bitrate is $a_in_rate bits per second" + ##File Bitrate in bps + f_in_rate=$(ffprobe -hide_banner -v 0 -of default=nw=1:nk=1 -show_entries 'format=bit_rate' -i "$file") + ##Test if Codecs and Bitrates are Acceptable + if [ "$v_in_codec" == "$v_codec" ] && [ ! $v_in_rate -gt $v_max_rate ] + then + echo_log "Checking video conditions: Passed! No transcoding." + v_trans=0 + else + echo_log "Checking video conditions: Over limit! Transcoding." + v_trans=1 + fi + if [ ! "$a_in_rate" -gt "$a_rate" ] + then + echo_log "Checking audio conditions: Passed! No transcoding." + a_trans=0 + else + echo_log "Checking audio conditions: Over limit! Transcoding." + a_trans=1 + fi + if [ "$v_opts" == "$v_def_opts" ] && [ "$a_opts" == "$a_def_opts" ] && [ ! "$f_in_rate" -gt "$target_rate" ] && [ "$prog" == "sonarr" ] + then + echo_log "No need for transcode, exiting." + else + echo "$push $prog \"$(echo '$file' | base64) $v_trans $a_trans\" | $REDIS_CLI + fi +} + +case $1 in + sonarr) + prog=sonarr + file="$sonarr_episodefile_path" + if [ ! $(echo "($(date +%s) - $(date -d $sonarr_episodefile_episodeairdatesutc +%s))/86400" | bc) -gt 7 ]; then push="LPUSH"; else push="RPUSH" ; fi + parse + ;; + radarr) + prog=radarr + file="$radarr_moviefile_path" + push="RPUSH" + parse + ;; +esac + +exit 0 diff --git a/transcode.sh b/transcode.sh new file mode 100644 index 0000000..e9e6065 --- /dev/null +++ b/transcode.sh @@ -0,0 +1,95 @@ +!#/bin/bash + +#Author: Glahera Matebi + +#Defining Variables: +log=transcode.log +redis_cli="redis-cli -h redis" +tmp_fol=/tmp +ext=mkv +target_rate=8000000 +a_rate=640000 +a_opts="-c:a ac3 -b:a $a_rate" +a_def_opts="-c:a copy" +v_avg_rate=$(echo "$target_rate"-"$a_rate" | bc) +v_min_rate=$(echo "$v_avg_rate"*80/100 | bc) +v_max_rate=$(echo "$v_avg_rate"*120/100 | bc) +v_buf=$(echo "$v_avg_rate"*2 | bc) +##This transcode setting also transcode from HDR to SDR, scale the resolution to 1920 width and encode as h264. Please change to your need. +v_opts="-c:V libx264 -vf scale=1920:-2 -b:V $v_avg_rate -maxrate $v_max_rate -minrate $v_min_rate -bufsize $v_buf -preset slower" +v_def_opts="-c:v copy" +#Defining Functions: + +#Run: +mkdir -p /var/log/transcode +touch /var/log/transcode/$log.ffmpeg +while true; do + ## Receive Job from Redis Server + declare -a args=($(echo "BLPOP sonarr radarr 0" | $redis_cli)) + ## Declaring Origin Program + prog=${args[0]} + echo_log "Start: ${prog^}" + ## Declaring Input File + file=$(echo ${args[1]} | base64 -d) + echo_log "Input File's Path: $file" + + height="$(mediainfo --Inform='Video;%Height%' "$file")" + if [[ ! -z $height ]]; then + height="[${height}p]" + echo_log "Resolution: $height" + fi + + # name = filename without extension + name=$(echo ${file%.*}) + # strip trailing space or hyphen + name=$(echo $name | sed -r 's/[- ]{1,}$//g') + tmp_file="/tmp/tmp_${RANDOM}.$ext" + # check for ffmpeg + ffmpeg=$(which ffmpeg) + if [[ $? != 0 ]]; then + echo_log "ERROR, ffmpeg missing" + exit 1 + fi + start=$(date +%s%N) + echo_log "Transcoding with the following settings: $ffmpeg_options $a_opts $v_opts" + ##Encode Pass 1 + nice -n20 ffmpeg -y -i "$file" -map V $v_opts -pass 1 -f matroska -passlogfile plex /dev/null + ##Encode Pass 2 + nice -n20 ffmpeg -i "$file" $ffmpeg_options $a_opts $v_opts -pass 2 -f matroska -passlogfile plex "$tmp_file" + if [[ $? != 0 ]]; then + echo_log "ERROR, ffmpeg exit code $?" + rm "$tmp_file" + exit 1 + fi + end=$(date +%s%N) + + ## Input file size + isize=$(du -b "$file" | awk '{print $1}') + isizeh=$(du -h "$file" | awk '{print $1}') + + if [[ $remove_original = 1 ]]; then + echo_log "Removing Original File" + rm "$file" + if [[ $? != 0 ]]; then + echo_log "WARNING, original file missing: $file" + fi + fi + + mv "$tmp_file" "$name.$ext" + if [[ $? != 0 ]]; then + echo_log "WARNING, temporary file missing: $tmp_file" + fi + + # Output File Size: + osize=$(du -b "$name.$ext" | awk '{print $1}') + osizeh=$(du -h "$name.$ext" | awk '{print $1}') + # Calculations: + speed=$(echo "scale=2; ($end - $start) / 1000000000 / 60" | bc) + size=$(echo "scale=2; ($isize - $osize)/$isize * 100" | bc) + echo_log "Input Size: $isizeh" + echo_log "Output Size: $osizeh" + echo_log "Encoding Speed: $speed min" + echo_log "Size Change: $size %" + echo_log "Output Filename: `basename "$name.$ext"`" + echo_log "End: ${prog^}" +done