2023-07-13 22:01:58 +02:00
#!/usr/bin/with-contenv bash
2024-02-05 15:24:02 +01:00
scriptVersion = "3.6"
2023-07-12 16:02:33 +02:00
scriptName = "Video"
2023-07-10 15:42:34 +02:00
2023-07-20 12:57:57 +02:00
### Import Settings
source /config/extended.conf
#### Import Functions
source /config/extended/functions
2023-07-19 16:43:27 +02:00
2023-07-14 13:30:07 +02:00
verifyConfig ( ) {
2024-01-12 07:56:49 +01:00
if [ -z " $videoContainer " ] ; then
videoContainer = "mkv"
fi
2023-07-15 16:59:43 +02:00
2024-02-05 15:24:02 +01:00
if [ -z " $disableImvd " ] ; then
disableImvd = "fasle"
fi
2023-07-14 13:30:07 +02:00
if [ " $enableVideo " != "true" ] ; then
log "Script is not enabled, enable by setting enableVideo to \"true\" by modifying the \"/config/extended.conf\" config file..."
log "Sleeping (infinity)"
sleep infinity
fi
2024-02-05 15:24:02 +01:00
if [ " $disableImvd " = "true" ] ; then
log "Script is not enabled, enable by setting disableImvd to \"false\" by modifying the \"/config/extended.conf\" config file..."
log "Sleeping (infinity)"
sleep infinity
fi
2023-07-14 13:30:07 +02:00
if [ -z " $downloadPath " ] ; then
downloadPath = "/config/extended/downloads"
fi
2023-07-15 16:16:10 +02:00
if [ -z " $videoScriptInterval " ] ; then
videoScriptInterval = "15m"
fi
2023-07-14 13:30:07 +02:00
if [ -z " $videoPath " ] ; then
log "ERROR: videoPath is not configured via the \"/config/extended.conf\" config file..."
log "Updated your \"/config/extended.conf\" file with the latest options, see: https://github.com/RandomNinjaAtk/arr-scripts/blob/main/lidarr/extended.conf"
log "Sleeping (infinity)"
sleep infinity
fi
}
2023-07-13 13:01:14 +02:00
2023-07-10 15:15:31 +02:00
Configuration ( ) {
2023-07-15 17:04:42 +02:00
if [ " $dlClientSource " = "tidal" ] || [ " $dlClientSource " = "both" ] ; then
sourcePreference = tidal
fi
log "-----------------------------------------------------------------------------"
log "|~) _ ._ _| _ ._ _ |\ |o._ o _ |~|_|_|"
log "|~\(_|| |(_|(_)| | || \||| |_|(_||~| | |<"
log " Presents: $scriptName ( $scriptVersion ) "
log " May the beats be with you!"
log "-----------------------------------------------------------------------------"
log "Donate: https://github.com/sponsors/RandomNinjaAtk"
log "Project: https://github.com/RandomNinjaAtk/arr-scripts"
log "Support: https://github.com/RandomNinjaAtk/arr-scripts/discussions"
log "-----------------------------------------------------------------------------"
sleep 5
log ""
log "Lift off in..." ; sleep 0.5
log "5" ; sleep 1
log "4" ; sleep 1
log "3" ; sleep 1
log "2" ; sleep 1
log "1" ; sleep 1
2023-07-20 12:57:57 +02:00
2023-07-10 15:15:31 +02:00
verifyApiAccess
2023-07-15 17:04:42 +02:00
videoDownloadPath = " $downloadPath /videos "
log " CONFIG :: Download Location :: $videoDownloadPath "
2023-07-10 15:15:31 +02:00
log " CONFIG :: Music Video Location :: $videoPath "
log " CONFIG :: Subtitle Language set to: $youtubeSubtitleLanguage "
log " CONFIG :: Video container set to format: $videoContainer "
if [ " $videoContainer " = = "mkv" ] ; then
log " CONFIG :: yt-dlp format: $videoFormat "
fi
if [ " $videoContainer " = = "mp4" ] ; then
log "CONFIG :: yt-dlp format: --format-sort ext:mp4:m4a --merge-output-format mp4"
fi
if [ -n " $videoDownloadTag " ] ; then
log " CONFIG :: Video download tag set to: $videoDownloadTag "
fi
if [ -f "/config/cookies.txt" ] ; then
cookiesFile = "/config/cookies.txt"
log "CONFIG :: Cookies File Found! (/config/cookies.txt)"
else
log "CONFIG :: ERROR :: Cookies File Not Found!"
log "CONFIG :: ERROR :: Add yt-dlp compatible cookies.txt to the following location: /config/cookies.txt"
cookiesFile = ""
fi
log "CONFIG :: Complete"
}
ImvdbCache ( ) {
if [ -z " $artistImvdbSlug " ] ; then
return
fi
if [ ! -d "/config/extended/cache/imvdb" ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: Creating Cache Folder... "
2023-07-10 15:15:31 +02:00
mkdir -p "/config/extended/cache/imvdb"
chmod 777 "/config/extended/cache/imvdb"
fi
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: Caching Records... "
2023-07-10 15:15:31 +02:00
if [ ! -f /config/extended/cache/imvdb/$artistImvdbSlug ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: Recording Artist Slug into cache "
2023-07-10 15:15:31 +02:00
echo -n " $lidarrArtistName " > /config/extended/cache/imvdb/$artistImvdbSlug
fi
2023-08-01 13:58:19 +02:00
count = 0
attemptError = "false"
until false; do
count = $(( $count + 1 ))
2023-09-21 13:08:00 +02:00
artistImvdbVideoUrls = $( curl -s " https://imvdb.com/n/ $artistImvdbSlug " --compressed -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate, br' -H 'DNT: 1' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Sec-Fetch-Dest: document' -H 'Sec-Fetch-Mode: navigate' -H 'Sec-Fetch-Site: none' -H 'Sec-Fetch-User: ?1' | grep " $artistImvdbSlug " | grep -Eoi '<a [^>]+>' | grep -Eo 'href="[^\"]+"' | grep -Eo '(http|https)://[^"]+' | grep -i " .com/video/ $artistImvdbSlug / " | sed " s%/[0-9] $%%g " | sort -u)
2023-08-01 13:58:19 +02:00
if echo " $artistImvdbVideoUrls " | grep -i "imvdb.com" | read; then
break
else
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ERROR :: Cannot connect to imvdb, retrying... "
sleep 0.5
fi
if [ $count = = 10 ] ; then
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ artistImvdbVideoUrlsCount } :: ERROR :: All attempts at connecting failed, skipping... "
attemptError = "true"
break
fi
done
2023-09-21 13:08:00 +02:00
2023-08-01 13:58:19 +02:00
if [ " $attemptError " = = "true" ] ; then
return
fi
2023-07-10 15:15:31 +02:00
artistImvdbVideoUrlsCount = $( echo " $artistImvdbVideoUrls " | wc -l)
cachedArtistImvdbVideoUrlsCount = $( ls /config/extended/cache/imvdb/$lidarrArtistMusicbrainzId --* 2>/dev/null | wc -l)
2023-08-01 13:58:19 +02:00
if [ " $artistImvdbVideoUrlsCount " = = " $cachedArtistImvdbVideoUrlsCount " ] ; then
2023-09-08 20:28:49 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: Cache is already up-to-date ( $artistImvdbVideoUrlsCount == $cachedArtistImvdbVideoUrlsCount ), skipping... "
2023-07-10 15:15:31 +02:00
return
else
2023-09-08 20:28:49 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: Cache needs updating ( ${ artistImvdbVideoUrlsCount } != ${ cachedArtistImvdbVideoUrlsCount } )... "
2023-07-10 15:15:31 +02:00
if [ -f " /config/extended/logs/video/complete/ $lidarrArtistMusicbrainzId " ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: Removing Artist completed log file to allow artist re-processing... "
2023-07-10 15:15:31 +02:00
rm " /config/extended/logs/video/complete/ $lidarrArtistMusicbrainzId "
fi
fi
2023-09-21 13:08:00 +02:00
2023-07-10 15:15:31 +02:00
sleep 0.5
imvdbProcessCount = 0
for imvdbVideoUrl in $( echo " $artistImvdbVideoUrls " ) ; do
imvdbProcessCount = $(( $imvdbProcessCount + 1 ))
imvdbVideoUrlSlug = $( basename " $imvdbVideoUrl " )
imvdbVideoData = " /config/extended/cache/imvdb/ $lidarrArtistMusicbrainzId -- $imvdbVideoUrlSlug .json "
#echo "$imvdbVideoUrl :: $imvdbVideoUrlSlug :: $imvdbVideoId"
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ artistImvdbVideoUrlsCount } :: Caching video data... "
2023-07-10 15:15:31 +02:00
if [ -f " $imvdbVideoData " ] ; then
if [ ! -s " $imvdbVideoData " ] ; then # if empty, delete file
rm " $imvdbVideoData "
fi
fi
if [ -f " $imvdbVideoData " ] ; then
if jq -e . >/dev/null 2>& 1 <<< " $( cat " $imvdbVideoData " ) " ; then # verify file is valid json
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ artistImvdbVideoUrlsCount } :: Video Data already downloaded "
2023-07-10 15:15:31 +02:00
continue
fi
fi
if [ ! -f " $imvdbVideoData " ] ; then
count = 0
until false; do
count = $(( $count + 1 ))
#echo "$count"
if [ ! -f " $imvdbVideoData " ] ; then
2023-09-21 13:08:00 +02:00
imvdbVideoId = $( curl -s " $imvdbVideoUrl " --compressed -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate, br' -H 'DNT: 1' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Sec-Fetch-Dest: document' -H 'Sec-Fetch-Mode: navigate' -H 'Sec-Fetch-Site: none' -H 'Sec-Fetch-User: ?1' | grep "<p>ID:" | grep -o "[[:digit:]]*" )
imvdbVideoJsonUrl = " https://imvdb.com/api/v1/video/ $imvdbVideoId ?include=sources,featured,credits "
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ artistImvdbVideoUrlsCount } :: Downloading Video data "
2023-09-21 13:08:00 +02:00
curl -s " $imvdbVideoJsonUrl " --compressed -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate, br' -H 'DNT: 1' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Sec-Fetch-Dest: document' -H 'Sec-Fetch-Mode: navigate' -H 'Sec-Fetch-Site: none' -H 'Sec-Fetch-User: ?1' -o " $imvdbVideoData "
2023-07-10 15:15:31 +02:00
sleep 0.5
fi
if [ -f " $imvdbVideoData " ] ; then
2023-08-01 13:07:11 +02:00
if jq -e . >/dev/null 2>& 1 <<< " $( cat " $imvdbVideoData " ) " ; then
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ artistImvdbVideoUrlsCount } :: Download Complete "
break
else
2023-07-10 15:15:31 +02:00
rm " $imvdbVideoData "
if [ $count = 2 ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ artistImvdbVideoUrlsCount } :: Download Failed, skipping... "
2023-07-10 15:15:31 +02:00
break
fi
2023-08-01 13:07:11 +02:00
fi
else
2023-08-01 14:33:28 +02:00
if [ $count = 5 ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ artistImvdbVideoUrlsCount } :: Download Failed, skipping... "
2023-07-10 15:15:31 +02:00
break
fi
fi
done
fi
done
}
DownloadVideo ( ) {
2023-07-15 17:04:42 +02:00
if [ -d " $videoDownloadPath /incomplete " ] ; then
rm -rf " $videoDownloadPath /incomplete "
2023-07-10 15:15:31 +02:00
fi
2023-07-15 17:04:42 +02:00
if [ ! -d " $videoDownloadPath /incomplete " ] ; then
mkdir -p " $videoDownloadPath /incomplete "
chmod 777 " $videoDownloadPath /incomplete "
2023-07-10 15:15:31 +02:00
fi
if echo " $1 " | grep -i "youtube" | read; then
if [ $videoContainer = mkv ] ; then
if [ ! -z " $cookiesFile " ] ; then
2023-07-15 17:04:42 +02:00
yt-dlp -f " $videoFormat " --no-video-multistreams --cookies " $cookiesFile " -o " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } " --embed-subs --sub-lang $youtubeSubtitleLanguage --merge-output-format mkv --remux-video mkv --no-mtime --geo-bypass " $1 "
2023-07-10 15:15:31 +02:00
else
2023-07-15 17:04:42 +02:00
yt-dlp -f " $videoFormat " --no-video-multistreams -o " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } " --embed-subs --sub-lang $youtubeSubtitleLanguage --merge-output-format mkv --remux-video mkv --no-mtime --geo-bypass " $1 "
2023-07-10 15:15:31 +02:00
fi
2023-07-15 17:04:42 +02:00
if [ -f " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } .mkv " ] ; then
chmod 666 " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } .mkv "
2023-07-10 15:15:31 +02:00
downloadFailed = false
else
downloadFailed = true
fi
else
if [ ! -z " $cookiesFile " ] ; then
2023-07-15 17:04:42 +02:00
yt-dlp --format-sort ext:mp4:m4a --merge-output-format mp4 --no-video-multistreams --cookies " $cookiesFile " -o " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } " --embed-subs --sub-lang $youtubeSubtitleLanguage --no-mtime --geo-bypass " $1 "
2023-07-10 15:15:31 +02:00
else
2023-07-15 17:04:42 +02:00
yt-dlp --format-sort ext:mp4:m4a --merge-output-format mp4 --no-video-multistreams -o " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } " --embed-subs --sub-lang $youtubeSubtitleLanguage --no-mtime --geo-bypass " $1 "
2023-07-10 15:15:31 +02:00
fi
2023-07-15 17:04:42 +02:00
if [ -f " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } .mp4 " ] ; then
chmod 666 " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } .mp4 "
2023-07-10 15:15:31 +02:00
downloadFailed = false
else
downloadFailed = true
fi
fi
fi
}
DownloadThumb ( ) {
2023-07-15 17:04:42 +02:00
curl -s " $1 " -o " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } .jpg "
chmod 666 " $videoDownloadPath /incomplete/ ${ 2 } ${ 3 } .jpg "
2023-07-10 15:15:31 +02:00
}
VideoProcessWithSMA ( ) {
2023-07-15 17:04:42 +02:00
find " $videoDownloadPath /incomplete " -type f -regex ".*/.*\.\(mkv\|mp4\)" -print0 | while IFS = read -r -d '' video; do
2023-07-10 15:15:31 +02:00
count = $(( $count + 1 ))
file = " ${ video } "
filenoext = " ${ file %.* } "
filename = " $( basename " $video " ) "
extension = " ${ filename ##*. } "
filenamenoext = " ${ filename %.* } "
if [ [ $filenoext .$videoContainer = = *.mkv ] ]
then
if python3 /usr/local/sma/manual.py --config "/config/extended/sma.ini" -i " $file " -nt & >/dev/null; then
sleep 0.01
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: $2 :: Processed with SMA... "
2023-07-10 15:15:31 +02:00
rm /usr/local/sma/config/*log*
else
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: $2 :: ERROR: SMA Processing Error "
2023-07-10 15:15:31 +02:00
rm " $video "
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: $2 :: INFO: deleted: $filename "
2023-07-10 15:15:31 +02:00
fi
else
if python3 /usr/local/sma/manual.py --config "/config/extended/sma-mp4.ini" -i " $file " -nt & >/dev/null; then
sleep 0.01
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: $2 :: Processed with SMA... "
2023-07-10 15:15:31 +02:00
rm /usr/local/sma/config/*log*
else
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: $2 :: ERROR: SMA Processing Error "
2023-07-10 15:15:31 +02:00
rm " $video "
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: $2 :: INFO: deleted: $filename "
2023-07-10 15:15:31 +02:00
fi
fi
done
}
VideoTagProcess ( ) {
2023-07-15 17:04:42 +02:00
find " $videoDownloadPath /incomplete " -type f -regex ".*/.*\.\(mkv\|mp4\)" -print0 | while IFS = read -r -d '' video; do
2023-07-10 15:15:31 +02:00
count = $(( $count + 1 ))
file = " ${ video } "
filenoext = " ${ file %.* } "
filename = " $( basename " $video " ) "
extension = " ${ filename ##*. } "
filenamenoext = " ${ filename %.* } "
artistGenres = ""
OLDIFS = " $IFS "
IFS = $'\n'
artistGenres = ( $( echo $lidarrArtistData | jq -r ".genres[]" ) )
IFS = " $OLDIFS "
if [ ! -z " $artistGenres " ] ; then
for genre in ${ !artistGenres[@] } ; do
artistGenre = " ${ artistGenres [ $genre ] } "
OUT = $OUT " $artistGenre / "
done
genre = " ${ OUT %??? } "
else
genre = ""
fi
if [ [ $filenoext .$videoContainer = = *.mkv ] ] ; then
mv " $filenoext . $videoContainer " " $filenoext -temp. $videoContainer "
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: ${ 1 } ${ 2 } $3 :: Tagging file "
2023-07-10 15:15:31 +02:00
ffmpeg -y \
-i " $filenoext -temp. $videoContainer " \
-c copy \
-metadata TITLE = " ${ 1 } " \
-metadata DATE_RELEASE = " $3 " \
-metadata DATE = " $3 " \
-metadata YEAR = " $3 " \
-metadata GENRE = " $genre " \
-metadata ARTIST = " $lidarrArtistName " \
-metadata ALBUMARTIST = " $lidarrArtistName " \
-metadata ENCODED_BY = "lidarr-extended" \
2023-07-15 17:04:42 +02:00
-attach " $videoDownloadPath /incomplete/ ${ 1 } ${ 2 } .jpg " -metadata:s:t mimetype = image/jpeg \
2023-07-10 15:15:31 +02:00
" $filenoext . $videoContainer " & >/dev/null
rm " $filenoext -temp. $videoContainer "
chmod 666 " $filenoext . $videoContainer "
else
mv " $filenoext . $videoContainer " " $filenoext -temp. $videoContainer "
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: ${ 1 } ${ 2 } $3 :: Tagging file "
2023-07-10 15:15:31 +02:00
ffmpeg -y \
-i " $filenoext -temp. $videoContainer " \
2023-07-15 17:04:42 +02:00
-i " $videoDownloadPath /incomplete/ ${ 1 } ${ 2 } .jpg " \
2023-07-10 15:15:31 +02:00
-map 1 \
-map 0 \
-c copy \
-c:v:0 mjpeg \
-disposition:0 attached_pic \
-movflags faststart \
-metadata TITLE = " ${ 1 } " \
-metadata ARTIST = " $lidarrArtistName " \
-metadata DATE = " $3 " \
-metadata GENRE = " $genre " \
" $filenoext . $videoContainer " & >/dev/null
rm " $filenoext -temp. $videoContainer "
chmod 666 " $filenoext . $videoContainer "
fi
done
}
VideoNfoWriter ( ) {
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: ${ 3 } :: Writing NFO "
2023-07-15 17:04:42 +02:00
nfo = " $videoDownloadPath /incomplete/ ${ 1 } ${ 2 } .nfo "
2023-07-10 15:15:31 +02:00
if [ -f " $nfo " ] ; then
rm " $nfo "
fi
echo "<musicvideo>" >> " $nfo "
echo " <title> ${ 3 } ${ 4 } </title> " >> " $nfo "
echo " <userrating/>" >> " $nfo "
echo " <track/>" >> " $nfo "
echo " <studio/>" >> " $nfo "
artistGenres = ""
OLDIFS = " $IFS "
IFS = $'\n'
artistGenres = ( $( echo $lidarrArtistData | jq -r ".genres[]" ) )
IFS = " $OLDIFS "
if [ ! -z " $artistGenres " ] ; then
for genre in ${ !artistGenres[@] } ; do
artistGenre = " ${ artistGenres [ $genre ] } "
echo " <genre> $artistGenre </genre> " >> " $nfo "
done
fi
echo " <premiered/>" >> " $nfo "
echo " <year> $6 </year> " >> " $nfo "
if [ " $5 " = "musicbrainz" ] ; then
OLDIFS = " $IFS "
IFS = $'\n'
for artistName in $( echo " $musicbrainzVideoArtistCreditsNames " ) ; do
echo " <artist> $artistName </artist> " >> " $nfo "
done
IFS = " $OLDIFS "
fi
if [ " $5 " = "imvdb" ] ; then
echo " <artist> $lidarrArtistName </artist> " >> " $nfo "
for featuredArtistSlug in $( echo " $imvdbVideoFeaturedArtistsSlug " ) ; do
if [ -f /config/extended/cache/imvdb/$featuredArtistSlug ] ; then
featuredArtistName = " $( cat /config/extended/cache/imvdb/$featuredArtistSlug ) "
echo " <artist> $featuredArtistName </artist> " >> " $nfo "
fi
done
fi
echo " <albumArtistCredits>" >> " $nfo "
echo " <artist> $lidarrArtistName </artist> " >> " $nfo "
echo " <musicBrainzArtistID> $lidarrArtistMusicbrainzId </musicBrainzArtistID> " >> " $nfo "
echo " </albumArtistCredits>" >> " $nfo "
echo " <thumb> ${ 1 } ${ 2 } .jpg</thumb> " >> " $nfo "
echo " <source> $8 </source> " >> " $nfo "
echo "</musicvideo>" >> " $nfo "
tidy -w 2000 -i -m -xml " $nfo " & >/dev/null
chmod 666 " $nfo "
}
LidarrTaskStatusCheck ( ) {
alerted = no
until false
do
2023-07-10 15:47:10 +02:00
taskCount = $( curl -s " $arrUrl /api/v1/command?apikey= ${ arrApiKey } " | jq -r '.[] | select(.status=="started") | .name' | wc -l)
2023-07-10 15:15:31 +02:00
if [ " $taskCount " -ge "1" ] ; then
if [ " $alerted " = "no" ] ; then
alerted = yes
log "STATUS :: LIDARR BUSY :: Pausing/waiting for all active Lidarr tasks to end..."
fi
sleep 2
else
break
fi
done
}
AddFeaturedVideoArtists ( ) {
if [ " $addFeaturedVideoArtists " != "true" ] ; then
log "-----------------------------------------------------------------------------"
log "Add Featured Music Video Artists to Lidarr :: DISABLED"
log "-----------------------------------------------------------------------------"
return
fi
log "-----------------------------------------------------------------------------"
log "Add Featured Music Video Artists to Lidarr :: ENABLED"
log "-----------------------------------------------------------------------------"
2023-07-10 15:47:10 +02:00
lidarrArtistsData = " $( curl -s " $arrUrl /api/v1/artist?apikey= ${ arrApiKey } " | jq -r ".[]" ) "
2023-07-10 15:15:31 +02:00
artistImvdbUrl = $( echo $lidarrArtistsData | jq -r '.links[] | select(.name=="imvdb") | .url' )
videoArtists = $( ls /config/extended/cache/imvdb/ | grep -Ev ".*--.*" )
videoArtistsCount = $( ls /config/extended/cache/imvdb/ | grep -Ev ".*--.*" | wc -l)
if [ " $videoArtistsCount " = = "0" ] ; then
log " $videoArtistsCount Artists found for processing, skipping... "
return
fi
loopCount = 0
for slug in $( echo $videoArtists ) ; do
loopCount = $(( $loopCount + 1 ))
artistName = " $( cat /config/extended/cache/imvdb/$slug ) "
if echo " $artistImvdbUrl " | grep -i " imvdb.com/n/ ${ slug } $" | read; then
log " $loopCount of $videoArtistsCount :: $artistName :: Already added to Lidarr, skipping... "
continue
fi
log " $loopCount of $videoArtistsCount :: $artistName :: Processing url :: https://imvdb.com/n/ $slug "
artistNameEncoded = " $( jq -R -r @uri <<< " $artistName " ) "
2023-07-10 15:47:10 +02:00
lidarrArtistSearchData = " $( curl -s " $arrUrl /api/v1/search?term= ${ artistNameEncoded } &apikey= ${ arrApiKey } " ) "
2023-07-10 15:15:31 +02:00
lidarrArtistMatchedData = $( echo $lidarrArtistSearchData | jq -r " .[] | select(.artist) | select(.artist.links[].url | contains (\"imvdb.com/n/ ${ slug } \")) " 2>/dev/null)
if [ ! -z " $lidarrArtistMatchedData " ] ; then
data = " $lidarrArtistMatchedData "
artistName = " $( echo " $data " | jq -r ".artist.artistName" ) "
foreignId = " $( echo " $data " | jq -r ".foreignId" ) "
else
log " $loopCount of $videoArtistsCount :: $artistName :: ERROR : Musicbrainz ID Not Found, skipping... "
continue
fi
2023-07-10 15:47:10 +02:00
data = $( curl -s " $arrUrl /api/v1/rootFolder " -H " X-Api-Key: $arrApiKey " | jq -r ".[]" )
2023-07-10 15:15:31 +02:00
path = " $( echo " $data " | jq -r ".path" ) "
qualityProfileId = " $( echo " $data " | jq -r ".defaultQualityProfileId" ) "
metadataProfileId = " $( echo " $data " | jq -r ".defaultMetadataProfileId" ) "
data = " {
\" artistName\" : \" $artistName \" ,
\" foreignArtistId\" : \" $foreignId \" ,
\" qualityProfileId\" : $qualityProfileId ,
\" metadataProfileId\" : $metadataProfileId ,
\" monitored\" :true,
\" monitor\" :\" all\" ,
\" rootFolderPath\" : \" $path \" ,
\" addOptions\" :{ \" searchForMissingAlbums\" :false}
} "
if echo " $lidarrArtistIds " | grep " ^ ${ foreignId } $" | read; then
log " $loopCount of $videoArtistsCount :: $artistName :: Already in Lidarr ( $foreignId ), skipping... "
continue
fi
log " $loopCount of $videoArtistsCount :: $artistName :: Adding $artistName to Lidarr ( $foreignId )... "
LidarrTaskStatusCheck
2023-07-10 15:47:10 +02:00
lidarrAddArtist = $( curl -s " $arrUrl /api/v1/artist " -X POST -H 'Content-Type: application/json' -H " X-Api-Key: $arrApiKey " --data-raw " $data " )
2023-07-10 15:15:31 +02:00
done
}
NotifyWebhook ( ) {
if [ " $webHook " ]
then
content = " $1 : $2 "
curl -X POST " { $webHook } " -H 'Content-Type: application/json' -d '{"event":"' " $1 " '", "message":"' " $2 " '", "content":"' " $content " '"}'
fi
}
VideoProcess ( ) {
Configuration
AddFeaturedVideoArtists
log "-----------------------------------------------------------------------------"
log "Finding Videos"
log "-----------------------------------------------------------------------------"
if [ -z " $videoDownloadTag " ] ; then
2023-07-10 15:47:10 +02:00
lidarrArtists = $( wget --timeout= 0 -q -O - " $arrUrl /api/v1/artist?apikey= $arrApiKey " | jq -r .[ ] )
2023-07-10 15:15:31 +02:00
lidarrArtistIds = $( echo $lidarrArtists | jq -r .id)
else
2023-07-10 15:47:10 +02:00
lidarrArtists = $( curl -s " $arrUrl /api/v1/tag/detail " -H 'Content-Type: application/json' -H " X-Api-Key: $arrApiKey " | jq -r -M " .[] | select(.label == \" $videoDownloadTag \") | .artistIds " )
2023-07-10 15:15:31 +02:00
lidarrArtistIds = $( echo $lidarrArtists | jq -r .[ ] )
fi
lidarrArtistIdsCount = $( echo " $lidarrArtistIds " | wc -l)
processCount = 0
for lidarrArtistId in $( echo $lidarrArtistIds ) ; do
processCount = $(( $processCount + 1 ))
2023-07-10 15:47:10 +02:00
lidarrArtistData = $( wget --timeout= 0 -q -O - " $arrUrl /api/v1/artist/ $lidarrArtistId ?apikey= $arrApiKey " )
2023-07-10 15:15:31 +02:00
lidarrArtistName = $( echo $lidarrArtistData | jq -r .artistName)
lidarrArtistMusicbrainzId = $( echo $lidarrArtistData | jq -r .foreignArtistId)
if [ " $lidarrArtistName " = = "Various Artists" ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: Skipping, not processed by design... "
2023-07-10 15:15:31 +02:00
continue
fi
lidarrArtistPath = " $( echo " ${ lidarrArtistData } " | jq -r " .path" ) "
lidarrArtistFolder = " $( basename " ${ lidarrArtistPath } " ) "
lidarrArtistFolderNoDisambig = " $( echo " $lidarrArtistFolder " | sed " s/ (.*) $//g " | sed " s/\. $//g " ) " # Plex Sanitization, remove disambiguation
lidarrArtistNameSanitized = " $( echo " $lidarrArtistFolderNoDisambig " | sed 's% (.*)$%%g' ) "
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: Checking for IMVDB Slug "
2023-07-10 15:15:31 +02:00
artistImvdbUrl = $( echo $lidarrArtistData | jq -r '.links[] | select(.name=="imvdb") | .url' )
artistImvdbSlug = $( basename " $artistImvdbUrl " )
if [ ! -z " $artistImvdbSlug " ] ; then
2023-08-01 14:22:03 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: Slug :: $artistImvdbSlug "
2023-07-10 15:15:31 +02:00
else
2023-08-01 14:22:03 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ERROR :: Slug Not Found, skipping... "
2023-07-10 15:15:31 +02:00
continue
fi
2023-08-01 14:22:03 +02:00
if [ -d /config/extended/logs/video/complete ] ; then
if [ -f " /config/extended/logs/video/complete/ $lidarrArtistMusicbrainzId " ] ; then
# Only update cache for artist if the completed log file is older than 7 days...
if [ [ $( find " /config/extended/logs/video/complete/ $lidarrArtistMusicbrainzId " -mtime +7 -print) ] ] ; then
ImvdbCache
fi
2023-08-01 14:27:56 +02:00
else
ImvdbCache
2023-08-01 14:22:03 +02:00
fi
else
# Always run cache process if completed log folder does not exist
ImvdbCache
fi
2023-07-10 15:15:31 +02:00
if [ -d /config/extended/logs/video/complete ] ; then
2023-08-01 14:22:03 +02:00
# If completed log file found for artist, end processing and skip...
2023-07-10 15:15:31 +02:00
if [ -f " /config/extended/logs/video/complete/ $lidarrArtistMusicbrainzId " ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: Music Videos previously downloaded, skipping... "
2023-07-10 15:15:31 +02:00
continue
fi
fi
if [ -z " $artistImvdbSlug " ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: No IMVDB artist link found, skipping... "
2023-07-10 15:15:31 +02:00
# Create log of missing IMVDB url...
if [ ! -d "/config/extended/logs/video/imvdb-link-missing" ] ; then
mkdir -p "/config/extended/logs/video/imvdb-link-missing"
chmod 777 "/config/extended/logs/video"
chmod 777 "/config/extended/logs/video/imvdb-link-missing"
fi
if [ -d "/config/extended/logs/video/imvdb-link-missing" ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: Logging missing IMVDB artist in folder: /config/extended/logs/video/imvdb-link-missing "
2023-07-10 15:15:31 +02:00
touch " /config/extended/logs/video/imvdb-link-missing/ ${ lidarrArtistFolderNoDisambig } --mbid- ${ lidarrArtistMusicbrainzId } "
fi
else
# Remove missing IMVDB log file, now that it is found...
if [ -f " /config/extended/logs/video/imvdb-link-missing/ ${ lidarrArtistFolderNoDisambig } --mbid- ${ lidarrArtistMusicbrainzId } " ] ; then
rm " /config/extended/logs/video/imvdb-link-missing/ ${ lidarrArtistFolderNoDisambig } --mbid- ${ lidarrArtistMusicbrainzId } "
fi
imvdbArtistVideoCount = $( ls /config/extended/cache/imvdb/$lidarrArtistMusicbrainzId --*.json 2>/dev/null | wc -l)
if [ $imvdbArtistVideoCount = 0 ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: No videos found, skipping... "
2023-07-10 15:15:31 +02:00
else
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: Processing $imvdbArtistVideoCount Videos! "
2023-07-10 15:15:31 +02:00
find /config/extended/cache/imvdb -type f -empty -delete # delete empty files
imvdbProcessCount = 0
for imvdbVideoData in $( ls /config/extended/cache/imvdb/$lidarrArtistMusicbrainzId --*.json) ; do
imvdbProcessCount = $(( $imvdbProcessCount + 1 ))
imvdbVideoTitle = " $( cat " $imvdbVideoData " | jq -r .song_title) "
2023-08-18 20:35:04 +02:00
videoTitleClean = " $( echo " $imvdbVideoTitle " | sed 's%/%-%g' ) "
videoTitleClean = " $( echo " $videoTitleClean " | sed -e "s/[:alpha:][:digit:]._' -/ /g" -e "s/ */ /g" | sed 's/^[.]*//' | sed 's/[.]*$//g' | sed 's/^ *//g' | sed 's/ *$//g' ) "
2023-07-10 15:15:31 +02:00
imvdbVideoYear = ""
2023-08-01 13:07:11 +02:00
imvdbVideoYear = " $( cat " $imvdbVideoData " | jq -r .year) "
2023-07-10 15:15:31 +02:00
imvdbVideoImage = " $( cat " $imvdbVideoData " | jq -r .image.o) "
imvdbVideoArtistsSlug = " $( cat " $imvdbVideoData " | jq -r .artists[ ] .slug) "
echo " $lidarrArtistName " > /config/extended/cache/imvdb/$imvdbVideoArtistsSlug
imvdbVideoFeaturedArtistsSlug = " $( cat " $imvdbVideoData " | jq -r .featured_artists[ ] .slug) "
imvdbVideoYoutubeId = " $( cat " $imvdbVideoData " | jq -r ".sources[] | select(.is_primary==true) | select(.source==\"youtube\") | .source_data" ) "
#"/config/extended/cache/musicbrainz/$lidarrArtistId--$lidarrArtistMusicbrainzId--recordings.json"
#echo "$imvdbVideoTitle :: $imvdbVideoYear :: $imvdbVideoYoutubeId :: $imvdbVideoArtistsSlug"
if [ -z " $imvdbVideoYoutubeId " ] ; then
continue
fi
videoDownloadUrl = " https://www.youtube.com/watch?v= $imvdbVideoYoutubeId "
plexVideoType = "-video"
if [ -d " $videoPath / $lidarrArtistFolderNoDisambig " ] ; then
if [ -f " $videoPath / $lidarrArtistFolderNoDisambig / ${ videoTitleClean } ${ plexVideoType } .nfo " ] ; then
if cat " $videoPath / $lidarrArtistFolderNoDisambig / ${ videoTitleClean } ${ plexVideoType } .nfo " | grep "source" | read; then
sleep 0
else
sed -i '$d' " $videoPath / $lidarrArtistFolderNoDisambig / ${ videoTitleClean } ${ plexVideoType } .nfo "
echo " <source>youtube</source>" >> " $videoPath / $lidarrArtistFolderNoDisambig / ${ videoTitleClean } ${ plexVideoType } .nfo "
echo "</musicvideo>" >> " $videoPath / $lidarrArtistFolderNoDisambig / ${ videoTitleClean } ${ plexVideoType } .nfo "
tidy -w 2000 -i -m -xml " $videoPath / $lidarrArtistFolderNoDisambig / ${ videoTitleClean } ${ plexVideoType } .nfo " & >/dev/null
fi
fi
if [ [ -n $( find " $videoPath / $lidarrArtistFolderNoDisambig " -maxdepth 1 -iname " ${ videoTitleClean } ${ plexVideoType } .mkv " ) ] ] || [ [ -n $( find " $videoPath / $lidarrArtistFolderNoDisambig " -maxdepth 1 -iname " ${ videoTitleClean } ${ plexVideoType } .mp4 " ) ] ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: ${ imvdbVideoTitle } :: Previously Downloaded, skipping... "
2023-07-10 15:15:31 +02:00
continue
fi
fi
if [ ! -z " $imvdbVideoFeaturedArtistsSlug " ] ; then
for featuredArtistSlug in $( echo " $imvdbVideoFeaturedArtistsSlug " ) ; do
if [ -f /config/extended/cache/imvdb/$featuredArtistSlug ] ; then
featuredArtistName = " $( cat /config/extended/cache/imvdb/$featuredArtistSlug ) "
fi
find /config/extended/cache/imvdb -type f -empty -delete # delete empty files
if [ -z " $featuredArtistName " ] ; then
continue
fi
done
fi
if [ ! -z " $cookiesFile " ] ; then
videoData = " $( yt-dlp --cookies " $cookiesFile " -j " $videoDownloadUrl " ) "
else
videoData = " $( yt-dlp -j " $videoDownloadUrl " ) "
fi
videoThumbnail = " $imvdbVideoImage "
if [ -z " $imvdbVideoYear " ] ; then
videoUploadDate = " $( echo " $videoData " | jq -r .upload_date) "
videoYear = " ${ videoUploadDate : 0 : 4 } "
else
videoYear = " $imvdbVideoYear "
fi
videoSource = "youtube"
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: ${ imvdbVideoTitle } :: $videoDownloadUrl ... "
2023-07-10 15:15:31 +02:00
DownloadVideo " $videoDownloadUrl " " $videoTitleClean " " $plexVideoType " "IMVDB"
if [ " $downloadFailed " = "true" ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: $lidarrArtistName :: IMVDB :: ${ imvdbProcessCount } / ${ imvdbArtistVideoCount } :: ${ imvdbVideoTitle } :: Download failed, skipping... "
2023-07-10 15:15:31 +02:00
continue
fi
DownloadThumb " $imvdbVideoImage " " $videoTitleClean " " $plexVideoType " "IMVDB"
VideoProcessWithSMA "IMVDB" " $imvdbVideoTitle "
VideoTagProcess " $videoTitleClean " " $plexVideoType " " $videoYear " "IMVDB"
VideoNfoWriter " $videoTitleClean " " $plexVideoType " " $imvdbVideoTitle " "" "imvdb" " $videoYear " "IMVDB" " $videoSource "
if [ ! -d " $videoPath / $lidarrArtistFolderNoDisambig " ] ; then
mkdir -p " $videoPath / $lidarrArtistFolderNoDisambig "
chmod 777 " $videoPath / $lidarrArtistFolderNoDisambig "
fi
2023-07-15 17:04:42 +02:00
mv $videoDownloadPath /incomplete/* " $videoPath / $lidarrArtistFolderNoDisambig " /
2023-07-10 15:15:31 +02:00
done
fi
fi
if [ ! -d /config/extended/logs/video ] ; then
mkdir -p /config/extended/logs/video
chmod 777 /config/extended/logs/video
fi
if [ ! -d /config/extended/logs/video/complete ] ; then
mkdir -p /config/extended/logs/video/complete
chmod 777 /config/extended/logs/video/complete
fi
touch " /config/extended/logs/video/complete/ $lidarrArtistMusicbrainzId "
# Import Artist.nfo file
if [ -d " $lidarrArtistPath " ] ; then
if [ -d " $videoPath / $lidarrArtistFolderNoDisambig " ] ; then
if [ -f " $lidarrArtistPath /artist.nfo " ] ; then
if [ ! -f " $videoPath / $lidarrArtistFolderNoDisambig /artist.nfo " ] ; then
2023-08-01 13:07:11 +02:00
log " ${ processCount } / ${ lidarrArtistIdsCount } :: Copying Artist NFO to music-video artist directory "
2023-07-10 15:15:31 +02:00
cp " $lidarrArtistPath /artist.nfo " " $videoPath / $lidarrArtistFolderNoDisambig /artist.nfo "
chmod 666 " $videoPath / $lidarrArtistFolderNoDisambig /artist.nfo "
fi
fi
fi
fi
done
}
2023-07-12 16:02:33 +02:00
log "Starting Script...."
2023-07-10 15:15:31 +02:00
for ( ( ; ; ) ) ; do
let i++
2023-07-14 13:30:07 +02:00
logfileSetup
verifyConfig
2023-07-12 16:02:33 +02:00
getArrAppInfo
verifyApiAccess
2023-07-10 15:15:31 +02:00
VideoProcess
2023-07-15 16:16:10 +02:00
log " Script sleeping for $videoScriptInterval ... "
sleep $videoScriptInterval
2023-07-10 15:15:31 +02:00
done
exit