lidarr-dl/services/LyricExtractor.sh

85 lines
3.6 KiB
Bash
Raw Normal View History

2023-09-07 14:34:05 +02:00
#!/usr/bin/env bash
scriptName="LyricExtractor"
#### Import Settings
2024-11-26 22:01:19 +01:00
# shellcheck source=../config/extended.conf
source /var/lib/lidarr/extended.conf
2023-09-07 14:34:05 +02:00
#### Import Functions
2024-11-26 22:01:19 +01:00
source ../universal/functions.bash
2023-09-07 14:34:05 +02:00
#### Create Log File
SECONDS=0
2024-11-26 22:01:19 +01:00
getArrAppInfo "/var/lib/lidarr"
2023-09-07 15:00:53 +02:00
verifyApiAccess
if [ -z "$lidarr_album_id" ]; then
lidarr_album_id="$1"
fi
getAlbumArtist="$(curl -s "$arrUrl/api/v1/album/$lidarr_album_id" -H "X-Api-Key: ${arrApiKey}" | jq -r .artist.artistName)"
getAlbumArtistPath="$(curl -s "$arrUrl/api/v1/album/$lidarr_album_id" -H "X-Api-Key: ${arrApiKey}" | jq -r .artist.path)"
getTrackPath="$(curl -s "$arrUrl/api/v1/trackFile?albumId=$lidarr_album_id" -H "X-Api-Key: ${arrApiKey}" | jq -r .[].path | head -n1)"
2023-09-07 14:34:05 +02:00
getFolderPath="$(dirname "$getTrackPath")"
2023-09-07 15:00:53 +02:00
getAlbumFolderName="$(basename "$getFolderPath")"
log "Processing :: $getAlbumFolderName :: Processing Files..."
2023-09-07 14:34:05 +02:00
2024-11-26 22:01:19 +01:00
if echo "$getFolderPath" | grep "$getAlbumArtistPath" | read -r; then
2023-09-07 14:34:05 +02:00
if [ ! -d "$getFolderPath" ]; then
log "ERROR :: \"$getFolderPath\" Folder is missing :: Exiting..."
fi
else
log "ERROR :: $getAlbumArtistPath not found within \"$getFolderPath\" :: Exiting..."
exit
fi
2024-11-26 22:01:19 +01:00
if ls "$getFolderPath" | grep "lrc" | read -r; then
2023-09-07 14:34:05 +02:00
log "Processing :: $getAlbumFolderName :: Removing existing lrc files"
find "$getFolderPath" -type f -iname "*.lrc" -delete
fi
find "$getFolderPath" -type f -regex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" -print0 | while IFS= read -r -d '' file; do
fileName=$(basename -- "$file")
fileExt="${fileName##*.}"
fileNameNoExt="${fileName%.*}"
if [ "$fileExt" == "flac" ]; then
log "Processing :: $getAlbumFolderName :: $fileName :: Getting Lyrics from embedded metadata"
getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".format.tags.LYRICS" 2>/dev/null | sed "s/null//g" | sed "/^$/d")"
2023-09-09 01:07:30 +02:00
if [ -z "$getLyrics" ]; then
getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".format.tags.Lyrics" 2>/dev/null | sed "s/null//g" | sed "/^$/d")"
fi
2023-09-09 01:07:30 +02:00
if [ -z "$getLyrics" ]; then
getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".format.tags.lyrics" 2>/dev/null | sed "s/null//g" | sed "/^$/d")"
fi
2023-09-07 14:34:05 +02:00
fi
if [ "$fileExt" == "opus" ]; then
log "Processing :: $getAlbumFolderName :: $fileName :: Getting Lyrics from embedded metadata"
getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".streams[].tags.LYRICS" 2>/dev/null | sed "s/null//g" | sed "/^$/d")"
2023-09-09 01:07:30 +02:00
if [ -z "$getLyrics" ]; then
getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".streams[].tags.Lyrics" 2>/dev/null | sed "s/null//g" | sed "/^$/d")"
fi
2023-09-09 01:07:30 +02:00
if [ -z "$getLyrics" ]; then
getLyrics="$(ffprobe -loglevel 0 -print_format json -show_format -show_streams "$file" | jq -r ".streams[].tags.lyrics" 2>/dev/null | sed "s/null//g" | sed "/^$/d")"
fi
2023-09-07 15:00:53 +02:00
fi
2024-11-26 22:01:19 +01:00
if [ -n "$getLyrics" ]; then
2023-09-07 15:00:53 +02:00
lrcFile="${file%.*}.lrc"
log "Processing :: $getAlbumFolderName :: $fileName :: Extracting Lyrics..."
echo -n "$getLyrics" > "$lrcFile"
log "Processing :: $getAlbumFolderName :: $fileName :: Lyrics extracted to: $fileNameNoExt.lrc"
chmod 666 "$lrcFile"
else
log "Processing :: $getAlbumFolderName :: $fileName :: Lyrics not found..."
fi
2023-09-07 14:34:05 +02:00
done
duration=$SECONDS
2024-11-26 22:01:19 +01:00
log "Processing :: $getAlbumFolderName :: Finished in $(( duration / 60 )) minutes and $(( duration % 60 )) seconds!"
2023-09-07 14:34:05 +02:00
exit