Transcoderr/queue.sh

78 lines
3.3 KiB
Bash

!#/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