Update audio-pp.bash

pull/11/head
RandomNinjaAtk 4 years ago committed by GitHub
parent 1c9fada994
commit a669394a00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,252 +1,219 @@
#!/usr/bin/with-contenv bash #!/usr/bin/with-contenv bash
export LC_ALL=C.UTF-8
# SETTINGS export LANG=C.UTF-8
AudioVerification="${AUDIO_VERIFY}" # TRUE = ENABLED, Verifies FLAC/MP3 files for errors (fixes MP3's, deletes bad FLAC files) TITLESHORT="APP"
ConversionFormat="${AUDIO_FORMAT}" # SET TO: OPUS or AAC or MP3 or ALAC or FLAC - converts lossless FLAC files to set format
ConversionBitrate="${AUDIO_BITRATE}" # Set to desired bitrate when converting to OPUS/AAC/MP3 format types Main () {
ReplaygainTagging="${AUDIO_REPLAYGAIN}" # TRUE = ENABLED, adds replaygain tags for compatible players (FLAC ONLY) # SETTINGS
DetectNonSplitAlubms="${AUDIO_DSFA}" # TRUE = ENABLED :: Uses "MaxFileSize" to detect and mark download as failed if detected AudioVerification="${AUDIO_VERIFY}" # TRUE = ENABLED, Verifies FLAC/MP3 files for errors (fixes MP3's, deletes bad FLAC files)
MaxFileSize="${AUDIO_DSFAS}" # M = MB, G = GB :: Set size threshold for detecting single file albums ConversionFormat="${AUDIO_FORMAT}" # SET TO: OPUS or AAC or MP3 or ALAC or FLAC - converts lossless FLAC files to set format
RequireBeetsMatch="${AUDIO_REQUIREBEETSTAGGING}" # true = enabled :: skips importing files that could not be matched using beets ConversionBitrate="${AUDIO_BITRATE}" # Set to desired bitrate when converting to OPUS/AAC/MP3 format types
ReplaygainTagging="${AUDIO_REPLAYGAIN}" # TRUE = ENABLED, adds replaygain tags for compatible players (FLAC ONLY)
#============FUNCTIONS============ DetectNonSplitAlubms="${AUDIO_DSFA}" # TRUE = ENABLED :: Uses "MaxFileSize" to detect and mark download as failed if detected
MaxFileSize="${AUDIO_DSFAS}" # M = MB, G = GB :: Set size threshold for detecting single file albums
settings () {
#============FUNCTIONS============
echo "Configuration:"
echo "Remove Non Audio Files: ENABLED" settings () {
echo "Duplicate File CleanUp: ENABLED"
if [ "${AudioVerification}" = TRUE ]; then echo "Configuration:"
echo "Audio Verification: ENABLED" echo "Remove Non Audio Files: ENABLED"
else echo "Duplicate File CleanUp: ENABLED"
echo "Audio Verification: DISABLED" if [ "${AudioVerification}" = TRUE ]; then
fi echo "Audio Verification: ENABLED"
echo "Format: $ConversionFormat"
if [ "${ConversionFormat}" = FLAC ]; then
echo "Bitrate: lossless"
echo "Replaygain Tagging: ENABLED"
elif [ "${ConversionFormat}" = ALAC ]; then
echo "Bitrate: lossless"
else
echo "Conversion Bitrate: ${ConversionBitrate}k"
fi
if [ "${DetectNonSplitAlubms}" = TRUE ]; then
echo "Detect Non Split Alubms: ENABLED"
echo "Max File Size: $MaxFileSize"
else
echo "DetectNonSplitAlubms: DISABLED"
fi
if [ "${TagWithBeets}" = TRUE ]; then
echo "Tag with Beets: ENABLED"
else
echo "Tag with Beets: DISABLED"
fi
if [ "${RequireBeetsMatch}" = true ]; then
echo "Require Beets Match: ENABLED"
else
echo "Require Beets Match: DISABLED"
fi
echo "Processing: $1"
}
clean () {
if find "$1" -type f -iregex ".*/.*\.\(flac\|mp3\|m4a\|alac\|ogg\|opus\)" | read; then
if find "$1" -type f -not -iregex ".*/.*\.\(flac\|mp3\|m4a\|alac\|ogg\|opus\)" | read; then
find "$1" -type f -not -iregex ".*/.*\.\(flac\|mp3\|m4a\|alac\|ogg\|opus\)" -delete
fi
find "$1" -mindepth 2 -type f -exec mv "{}" "$1"/ \;
find "$1" -mindepth 1 -type d -delete
else else
echo "ERROR: NO AUDIO FILES FOUND" && exit 1 echo "Audio Verification: DISABLED"
fi fi
} echo "Format: $ConversionFormat"
if [ "${ConversionFormat}" = FLAC ]; then
duplicatefilecleanup () { echo "Bitrate: lossless"
duplicate="FALSE" echo "Replaygain Tagging: ENABLED"
if find "$1" -type f -mindepth 1 -iname "*([0-9]).*" | read; then elif [ "${ConversionFormat}" = ALAC ]; then
find "$1" -type f -mindepth 1 -iname "*([0-9]).*" -delete echo "Bitrate: lossless"
duplicate="TRUE" else
echo "Conversion Bitrate: ${ConversionBitrate}k"
fi fi
if [ "${DetectNonSplitAlubms}" = TRUE ]; then
if find "$1" -type f -mindepth 1 -iname "*.[0-9].*" | read; then echo "Detect Non Split Alubms: ENABLED"
find "$1" -type f -mindepth 1 -iname "*.[0-9].*" -delete echo "Max File Size: $MaxFileSize"
duplicate="TRUE" else
echo "DetectNonSplitAlubms: DISABLED"
fi fi
if find "$1" -type f -mindepth 1 -iname "*.flac" | read; then echo "Processing: $1"
if find "$1"/* -type f -not -iname "*.flac" | read; then
find "$1"/* -type f -not -iname "*.flac" -delete }
clean () {
if find "$1" -type f -iregex ".*/.*\.\(flac\|mp3\|m4a\|alac\|ogg\|opus\)" | read; then
if find "$1" -type f -not -iregex ".*/.*\.\(flac\|mp3\|m4a\|alac\|ogg\|opus\)" | read; then
find "$1" -type f -not -iregex ".*/.*\.\(flac\|mp3\|m4a\|alac\|ogg\|opus\)" -delete
fi
find "$1" -mindepth 2 -type f -exec mv "{}" "$1"/ \;
find "$1" -mindepth 1 -type d -delete
else
echo "ERROR: NO AUDIO FILES FOUND" && exit 1
fi
}
duplicatefilecleanup () {
duplicate="FALSE"
if find "$1" -type f -mindepth 1 -iname "*([0-9]).*" | read; then
find "$1" -type f -mindepth 1 -iname "*([0-9]).*" -delete
duplicate="TRUE" duplicate="TRUE"
fi fi
fi
if [ "${duplicate}" = TRUE ]; then
echo "DUPLICATE FILE CLEANUP"
echo "DUPLICATE FILE CLEANUP COMPLETE"
fi
}
detectsinglefilealbums () { if find "$1" -type f -mindepth 1 -iname "*.[0-9].*" | read; then
if find "$1" -type f -iregex ".*/.*\.\(flac\|mp3\|m4a\|alac\|ogg\|opus\)" -size +${MaxFileSize} | read; then find "$1" -type f -mindepth 1 -iname "*.[0-9].*" -delete
echo "ERROR: Non split album detected" && exit 1 duplicate="TRUE"
fi fi
}
verify () { if find "$1" -type f -mindepth 1 -iname "*.flac" | read; then
if find "$1" -iname "*.flac" | read; then if find "$1"/* -type f -not -iname "*.flac" | read; then
verifytrackcount=$(find "$1"/ -iname "*.flac" | wc -l) find "$1"/* -type f -not -iname "*.flac" -delete
echo "Verifying: $verifytrackcount Tracks" duplicate="TRUE"
if ! [ -x "$(command -v flac)" ]; then fi
echo "ERROR: FLAC verification utility not installed (ubuntu: apt-get install -y flac)"
else
for fname in "$1"/*.flac; do
filename="$(basename "$fname")"
if flac -t --totally-silent "$fname"; then
echo "Verified Track: $filename"
else
echo "ERROR: Track Verification Failed: \"$filename\""
rm -rf "$1"/*
sleep 0.1
exit 1
fi
done
fi fi
fi if [ "${duplicate}" = TRUE ]; then
if find "$1" -iname "*.mp3" | read; then echo "DUPLICATE FILE CLEANUP"
verifytrackcount=$(find "$1"/ -iname "*.mp3" | wc -l) echo "DUPLICATE FILE CLEANUP COMPLETE"
echo ""
echo "Verifying: $verifytrackcount Tracks"
if ! [ -x "$(command -v mp3val)" ]; then
echo "MP3VAL verification utility not installed (ubuntu: apt-get install -y mp3val)"
else
for fname in "$1"/*.mp3; do
filename="$(basename "$fname")"
if mp3val -f -nb "$fname" > /dev/null; then
echo "Verified Track: $filename"
fi
done
fi fi
fi }
}
conversion () { detectsinglefilealbums () {
converttrackcount=$(find "$1"/ -name "*.flac" | wc -l) if find "$1" -type f -iregex ".*/.*\.\(flac\|mp3\|m4a\|alac\|ogg\|opus\)" -size +${MaxFileSize} | read; then
targetformat="$ConversionFormat" echo "ERROR: Non split album detected" && exit 1
bitrate="$ConversionBitrate" fi
if [ "${ConversionFormat}" = OPUS ]; then }
options="-acodec libopus -ab ${bitrate}k -application audio -vbr off"
extension="opus" verify () {
targetbitrate="${bitrate}k" if find "$1" -iname "*.flac" | read; then
fi verifytrackcount=$(find "$1"/ -iname "*.flac" | wc -l)
if [ "${ConversionFormat}" = AAC ]; then echo "Verifying: $verifytrackcount Tracks"
options="-acodec aac -ab ${bitrate}k -movflags faststart" if ! [ -x "$(command -v flac)" ]; then
extension="m4a" echo "ERROR: FLAC verification utility not installed (ubuntu: apt-get install -y flac)"
targetbitrate="${bitrate}k" else
fi for fname in "$1"/*.flac; do
if [ "${ConversionFormat}" = MP3 ]; then filename="$(basename "$fname")"
options="-acodec libmp3lame -ab ${bitrate}k" if flac -t --totally-silent "$fname"; then
extension="mp3" echo "Verified Track: $filename"
targetbitrate="${bitrate}k" else
fi echo "ERROR: Track Verification Failed: \"$filename\""
if [ "${ConversionFormat}" = ALAC ]; then rm -rf "$1"/*
options="-acodec alac -movflags faststart" sleep 0.1
extension="m4a" exit 1
targetbitrate="lossless" fi
fi done
if [ "${ConversionFormat}" = FLAC ]; then fi
options="-acodec flac" fi
extension="flac" if find "$1" -iname "*.mp3" | read; then
targetbitrate="lossless" verifytrackcount=$(find "$1"/ -iname "*.mp3" | wc -l)
fi echo ""
if [ -x "$(command -v ffmpeg)" ]; then echo "Verifying: $verifytrackcount Tracks"
if ! [ -x "$(command -v mp3val)" ]; then
echo "MP3VAL verification utility not installed (ubuntu: apt-get install -y mp3val)"
else
for fname in "$1"/*.mp3; do
filename="$(basename "$fname")"
if mp3val -f -nb "$fname" > /dev/null; then
echo "Verified Track: $filename"
fi
done
fi
fi
}
conversion () {
converttrackcount=$(find "$1"/ -name "*.flac" | wc -l)
targetformat="$ConversionFormat"
bitrate="$ConversionBitrate"
if [ "${ConversionFormat}" = OPUS ]; then
options="-acodec libopus -ab ${bitrate}k -application audio -vbr off"
extension="opus"
targetbitrate="${bitrate}k"
fi
if [ "${ConversionFormat}" = AAC ]; then
options="-acodec aac -ab ${bitrate}k -movflags faststart"
extension="m4a"
targetbitrate="${bitrate}k"
fi
if [ "${ConversionFormat}" = MP3 ]; then
options="-acodec libmp3lame -ab ${bitrate}k"
extension="mp3"
targetbitrate="${bitrate}k"
fi
if [ "${ConversionFormat}" = ALAC ]; then
options="-acodec alac -movflags faststart"
extension="m4a"
targetbitrate="lossless"
fi
if [ "${ConversionFormat}" = FLAC ]; then if [ "${ConversionFormat}" = FLAC ]; then
sleep 0.1 options="-acodec flac"
elif find "$1"/ -name "*.flac" | read; then extension="flac"
echo "Converting: $converttrackcount Tracks (Target Format: $targetformat (${targetbitrate}))" targetbitrate="lossless"
for fname in "$1"/*.flac; do fi
filename="$(basename "${fname%.flac}")" if [ -x "$(command -v ffmpeg)" ]; then
if ffmpeg -loglevel warning -hide_banner -nostats -i "$fname" -n -vn $options "${fname%.flac}.temp.$extension"; then if [ "${ConversionFormat}" = FLAC ]; then
echo "Converted: $filename" sleep 0.1
if [ -f "${fname%.flac}.temp.$extension" ]; then elif find "$1"/ -name "*.flac" | read; then
rm "$fname" echo "Converting: $converttrackcount Tracks (Target Format: $targetformat (${targetbitrate}))"
for fname in "$1"/*.flac; do
filename="$(basename "${fname%.flac}")"
if ffmpeg -loglevel warning -hide_banner -nostats -i "$fname" -n -vn $options "${fname%.flac}.temp.$extension"; then
echo "Converted: $filename"
if [ -f "${fname%.flac}.temp.$extension" ]; then
rm "$fname"
sleep 0.1
mv "${fname%.flac}.temp.$extension" "${fname%.flac}.$extension"
fi
else
echo "Conversion failed: $filename, performing cleanup..."
rm -rf "$1"/*
sleep 0.1 sleep 0.1
mv "${fname%.flac}.temp.$extension" "${fname%.flac}.$extension" exit 1
fi fi
else done
echo "Conversion failed: $filename, performing cleanup..." fi
rm -rf "$1"/* else
sleep 0.1 echo "ERROR: ffmpeg not installed, please install ffmpeg to use this conversion feature"
exit 1 sleep 5
fi
done
fi fi
else }
echo "ERROR: ffmpeg not installed, please install ffmpeg to use this conversion feature"
sleep 5
fi
}
replaygain () { replaygain () {
if ! [ -x "$(command -v flac)" ]; then if ! [ -x "$(command -v flac)" ]; then
echo "ERROR: METAFLAC replaygain utility not installed (ubuntu: apt-get install -y flac)" echo "ERROR: METAFLAC replaygain utility not installed (ubuntu: apt-get install -y flac)"
elif find "$1" -iname "*.flac" | read; then elif find "$1" -iname "*.flac" | read; then
replaygaintrackcount=$(find "$1"/ -iname "*.flac" | wc -l) replaygaintrackcount=$(find "$1"/ -iname "*.flac" | wc -l)
echo "Replaygain: Calculating $replaygaintrackcount Tracks" echo "Replaygain: Calculating $replaygaintrackcount Tracks"
find "$1" -iname "*.flac" -exec metaflac --add-replay-gain "{}" + && echo "Replaygain: $replaygaintrackcount Tracks Tagged" find "$1" -iname "*.flac" -exec metaflac --add-replay-gain "{}" + && echo "Replaygain: $replaygaintrackcount Tracks Tagged"
fi fi
} }
beets () { #============START SCRIPT============
trackcount=$(find "$1" -type f -iregex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" | wc -l)
echo "Matching $trackcount tracks with Beets"
touch "$1/beets-match" settings "$1"
sleep 0.1 clean "$1"
duplicatefilecleanup "$1"
detectsinglefilealbums "$1"
if find "$1" -type f -iregex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" | read; then if [ "${AudioVerification}" = TRUE ]; then
beet -c "/config/scripts/configs/beets-config.yaml" -l "$1/beets-library.blb" -d "$1" import -q "$1" verify "$1"
if find "$1" -type f -iregex ".*/.*\.\(flac\|opus\|m4a\|mp3\)" -newer "$1/beets-match" | read; then
echo "SUCCESS: Matched with beets!"
else
if [ "$RequireBeetsMatch" = true ]; then
echo "ERROR: RequireBeetsMatch enabled, performing cleanup"
rm -rf "$1"/*
sleep 0.1
exit 1
else
echo "ERROR: Unable to match using beets, fallback to lidarr import matching..."
fi
fi
fi fi
if [ -f "$1/beets-match" ]; then conversion "$1"
rm "$1/beets-match"
sleep 0.1
fi
if [ -f "$1/beets-library.blb" ]; then if [ "${ReplaygainTagging}" = TRUE ]; then
rm "$1/beets-library.blb" replaygain "$1"
sleep 0.1
fi fi
}
#============START SCRIPT============ echo ""
echo "Post Processing Complete!" && exit 0
settings "$1" #============END SCRIPT============
clean "$1" }
duplicatefilecleanup "$1"
detectsinglefilealbums "$1"
if [ "${AudioVerification}" = TRUE ]; then
verify "$1"
fi
conversion "$1"
if [ "${ReplaygainTagging}" = TRUE ]; then Main "$@" | tee -a /config/scripts/logs/audio-pp.log
replaygain "$1" chmod 666 /config/scripts/logs/audio-pp.log
fi chown abc:abc /config/scripts/logs/audio-pp.log
echo "" exit $?
echo "Post Processing Complete!" && exit 0
#============END SCRIPT============

Loading…
Cancel
Save