lidarr-dl/sabnzbd/video.bash

216 lines
7 KiB
Bash
Raw Normal View History

2023-07-07 02:22:44 +02:00
#!/usr/bin/with-contenv bash
2023-07-07 14:57:15 +02:00
scriptVersion="1.0.0"
2023-07-07 02:22:44 +02:00
set -e
set -o pipefail
2023-07-07 14:57:15 +02:00
######## Settings
videoLanguages="eng"
requireLanguageMatch="true"
enableSma="true"
enableSmaTagging="true"
2023-07-07 02:22:44 +02:00
2023-07-07 14:57:15 +02:00
######## Package dependencies installation
2023-07-07 02:22:44 +02:00
InstallRequirements () {
2023-07-07 15:03:41 +02:00
if [ ! -f "/config/scripts/video.txt" ]; then
2023-07-07 02:22:44 +02:00
echo "Installing Required Packages..."
apk add -U --update --no-cache curl jq python3-dev py3-pip &>/dev/null
pip install --upgrade --no-cache-dir -U yq &>/dev/null
echo "Done"
fi
if [ ! -f /usr/local/sma/manual.py ]; then
echo "************ setup SMA ************"
echo "************ setup directory ************"
mkdir -p /usr/local/sma
echo "************ download repo ************"
git clone https://github.com/mdhiggins/sickbeard_mp4_automator.git /usr/local/sma
mkdir -p /usr/local/sma/config && \
echo "************ create logging file ************"
mkdir -p /usr/local/sma/config && \
touch /usr/local/sma/config/sma.log && \
chgrp users /usr/local/sma/config/sma.log && \
chmod g+w /usr/local/sma/config/sma.log && \
echo "************ install pip dependencies ************" && \
python3 -m pip install --upgrade pip && \
pip3 install -r /usr/local/sma/setup/requirements.txt
fi
}
2023-07-07 14:57:15 +02:00
# auto-clean up log file to reduce space usage
if [ -f "/config/scripts/video.txt" ]; then
find /config/scripts -type f -name "video.txt" -size +1024k -delete
fi
2023-07-07 02:22:44 +02:00
2023-07-07 14:57:15 +02:00
touch "/config/scripts/video.txt"
chmod 666 "/config/scripts/video.txt"
exec &> >(tee -a "/config/scripts/video.txt")
2023-07-07 02:22:44 +02:00
function Configuration {
log "SABnzbd Job: $jobname"
log "SABnzbd Category: $category"
2023-07-07 14:57:15 +02:00
log "Script Versiion: $scriptVersion"
2023-07-07 02:22:44 +02:00
log "CONFIGURATION VERIFICATION"
log "##########################"
2023-07-07 14:57:15 +02:00
log "Preferred Audio/Subtitle Languages: ${videoLanguages}"
if [ "${requireLanguageMatch}" = "true" ]; then
2023-07-07 02:22:44 +02:00
log "Require Matching Language :: Enabled"
else
log "Require Matching Language :: Disabled"
fi
2023-07-07 14:57:15 +02:00
if [ ${enableSma} = true ]; then
log "Sickbeard MP4 Automator (SMA): ENABLED"
if [ ${enableSmaTagging} = true ]; then
2023-07-07 02:22:44 +02:00
tagging="-a"
log "Sickbeard MP4 Automator (SMA): Tagging: ENABLED"
else
tagging="-nt"
log "Sickbeard MP4 Automator (SMA): Tagging: DISABLED"
fi
else
log "Sickbeard MP4 Automator (SMA): DISABLED"
fi
2023-07-07 14:57:15 +02:00
if [ -z "enableSmaTagging" ]; then
enableSmaTagging=FALSE
2023-07-07 02:22:44 +02:00
fi
}
function log {
m_time=`date "+%F %T"`
echo $m_time" :: $scriptVersion :: "$1
}
VideoLanguageCheck () {
count=0
fileCount=$(find "$1" -type f -regex ".*/.*\.\(m4v\|wmv\|mkv\|mp4\|avi\)" | wc -l)
log "Processing ${fileCount} video files..."
find "$1" -type f -regex ".*/.*\.\(m4v\|wmv\|mkv\|mp4\|avi\)" -print0 | while IFS= read -r -d '' file; do
count=$(($count+1))
baseFileName="${file%.*}"
fileName="$(basename "$file")"
extension="${fileName##*.}"
log "$count of $fileCount :: Processing $fileName"
videoData=$(ffprobe -v quiet -print_format json -show_streams "$file")
videoAudioTracksCount=$(echo "${videoData}" | jq -r ".streams[] | select(.codec_type==\"audio\") | .index" | wc -l)
videoSubtitleTracksCount=$(echo "${videoData}" | jq -r ".streams[] | select(.codec_type==\"subtitle\") | .index" | wc -l)
log "$count of $fileCount :: $videoAudioTracksCount Audio Tracks Found!"
log "$count of $fileCount :: $videoSubtitleTracksCount Subtitle Tracks Found!"
videoAudioLanguages=$(echo "${videoData}" | jq -r ".streams[] | select(.codec_type==\"audio\") | .tags.language")
videoSubtitleLanguages=$(echo "${videoData}" | jq -r ".streams[] | select(.codec_type==\"subtitle\") | .tags.language")
# Language Check
2023-07-07 14:57:15 +02:00
log "$count of $fileCount :: Checking for preferred languages \"$videoLanguages\""
2023-07-07 02:22:44 +02:00
preferredLanguage=false
2023-07-07 14:57:15 +02:00
IFS=',' read -r -a filters <<< "$videoLanguages"
2023-07-07 02:22:44 +02:00
for filter in "${filters[@]}"
do
videoAudioTracksLanguageCount=$(echo "${videoData}" | jq -r ".streams[] | select(.codec_type==\"audio\") | select(.tags.language==\"${filter}\") | .index" | wc -l)
videoSubtitleTracksLanguageCount=$(echo "${videoData}" | jq -r ".streams[] | select(.codec_type==\"subtitle\") | select(.tags.language==\"${filter}\") | .index" | wc -l)
log "$count of $fileCount :: $videoAudioTracksLanguageCount \"$filter\" Audio Tracks Found!"
log "$count of $fileCount :: $videoSubtitleTracksLanguageCount \"$filter\" Subtitle Tracks Found!"
if [ "$preferredLanguage" == "false" ]; then
if echo "$videoAudioLanguages" | grep -i "$filter" | read; then
preferredLanguage=true
elif echo "$videoSubtitleLanguages" | grep -i "$filter" | read; then
preferredLanguage=true
fi
fi
done
if [ "$preferredLanguage" == "false" ]; then
2023-07-07 14:57:15 +02:00
if [ ${enableSma} = true ]; then
2023-07-07 02:22:44 +02:00
if [ "$smaProcessComplete" == "false" ]; then
return
fi
fi
2023-07-07 14:57:15 +02:00
if [ "$requireLanguageMatch" == "true" ]; then
2023-07-07 02:22:44 +02:00
log "$count of $fileCount :: ERROR :: No matching languages found in $(($videoAudioTracksCount + $videoSubtitleTracksCount)) Audio/Subtitle tracks"
log "$count of $fileCount :: ERROR :: Disable "
rm "$file" && log "INFO: deleted: $fileName"
fi
fi
log "$count of $fileCount :: Processing complete for: ${fileName}!"
done
}
VideoFileCheck () {
# check for video files
if find "$1" -type f -regex ".*/.*\.\(m4v\|wmv\|mkv\|mp4\|avi\)" | read; then
sleep 0.1
else
log "ERROR: No video files found for processing"
exit 1
fi
}
VideoSmaProcess (){
count=0
fileCount=$(find "$1" -type f -regex ".*/.*\.\(m4v\|wmv\|mkv\|mp4\|avi\)" | wc -l)
log "Processing ${fileCount} video files..."
find "$1" -type f -regex ".*/.*\.\(m4v\|wmv\|mkv\|mp4\|avi\)" -print0 | while IFS= read -r -d '' file; do
count=$(($count+1))
baseFileName="${file%.*}"
fileName="$(basename "$file")"
extension="${fileName##*.}"
log "$count of $fileCount :: Processing $fileName"
if [ -f "$file" ]; then
if [ -f /usr/local/sma/config/sma.log ]; then
rm /usr/local/sma/config/sma.log
fi
log "$count of $fileCount :: Processing with SMA..."
2023-07-07 14:57:15 +02:00
if [ -f "/config/scripts/sma.ini" ]; then
2023-07-07 02:22:44 +02:00
# Manual run of Sickbeard MP4 Automator
2023-07-07 14:57:15 +02:00
if python3 /usr/local/sma/manual.py --config "/config/scripts/sma.ini" -i "$file" $tagging; then
2023-07-07 02:22:44 +02:00
log "$count of $fileCount :: Complete!"
else
log "$count of $fileCount :: ERROR :: SMA Processing Error"
rm "$file" && log "INFO: deleted: $fileName"
fi
else
log "$count of $fileCount :: ERROR :: SMA Processing Error"
2023-07-07 14:57:15 +02:00
log "$count of $fileCount :: ERROR :: \"/config/scripts/sma.ini\" configuration file is missing..."
2023-07-07 02:22:44 +02:00
rm "$file" && log "INFO: deleted: $fileName"
fi
fi
done
smaProcessComplete="true"
}
function Main {
SECONDS=0
error=0
folderpath="$1"
jobname="$3"
category="$5"
smaProcessComplete="false"
Configuration
VideoFileCheck "$folderpath"
VideoLanguageCheck "$folderpath"
VideoFileCheck "$folderpath"
2023-07-07 14:57:15 +02:00
if [ ${enableSma} = true ]; then
2023-07-07 02:22:44 +02:00
VideoSmaProcess "$folderpath" "$category"
fi
VideoFileCheck "$folderpath"
VideoLanguageCheck "$folderpath"
VideoFileCheck "$folderpath"
duration=$SECONDS
echo "Post Processing Completed in $(($duration / 60 )) minutes and $(($duration % 60 )) seconds!"
}
2023-07-07 02:24:50 +02:00
InstallRequirements
2023-07-07 02:22:44 +02:00
Main "$@"
exit $?