From fc93b4bae500adba377b5aa369f0de338efbcf8e Mon Sep 17 00:00:00 2001 From: Out Of Ideas Date: Wed, 3 Jan 2024 11:58:22 -0600 Subject: [PATCH] Add MP3 finctionality --- rename-tracknumber | 69 ++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/rename-tracknumber b/rename-tracknumber index 0f22f96..b7a5b94 100755 --- a/rename-tracknumber +++ b/rename-tracknumber @@ -39,6 +39,8 @@ # # # 02/01/2024 Out Of Ideas File renaming issue fixed. # # # +# 02/01/2024 Out Of Ideas MP3 functionality has been # +# added; not tested. # ############################################################ ############################################################ ############################################################ @@ -148,6 +150,28 @@ CheckMetaflac() fi } +RenameFile () +{ +# If the track number is less than 10, put a 0 before it in the new song title. +# Eg. "Song", with track number "1", is renamed to "01 - Song". +if [ ${#TRACKNUMBER} -gt 9 ] + then + + if [ $VERBOSE ] + then + echo "mv \"$file\" \"$TRACKNUMBER - $file\"" + fi + mv "$file" "$TRACKNUMBER - $file" + + else + if [ $VERBOSE ] + then + echo "mv \"$file\" \"0$TRACKNUMBER - $file\"" + fi + mv "$file" "0$TRACKNUMBER - $file" + fi +} + ############################################################ # Main Program # ############################################################ @@ -198,36 +222,33 @@ done # Display a message before the program starts #echo "$Msg" -read -p 'File extension: ' EXTENSION - for file in * do # Ensure only songs are renamed. - if [[ $file == *.$EXTENSION ]] + if [[ $file == *.flac ]] then TRACKNUMBER=$(metaflac --show-tag=TRACKNUMBER "$file" | head -n 1 | sed -r 's/^TRACKNUMBER=([0-9]+).*$/\1/') - if [ $VERBOSE ] - then - echo "TRACKNUMBER = $TRACKNUMBER" - fi - # If the track number is less than 10, put a 0 before it in the new song title. - # Eg. "Song", with track number "1", is renamed to "01 - Song". - if [ ${#TRACKNUMBER} -gt 9 ] - then - - if [ $VERBOSE ] - then - echo "mv \"$file\" \"$TRACKNUMBER - $file\"" - fi - mv "$file" "$TRACKNUMBER - $file" - else - if [ $VERBOSE ] - then - echo "mv \"$file\" \"0$TRACKNUMBER - $file\"" - fi - mv "$file" "0$TRACKNUMBER - $file" - fi + elif [[ $file == *.mp3 ]] + then + + TRACKNUMBER=$(ffprobe $file 2>&1 | grep 'track' | awk '{print $3}' | sed "s/\/[0-9]*//") + + else + + echo 'Error: No `.flac` or `.mp3` files found.' + exit + fi + + if [ $VERBOSE ] + then + echo "TRACKNUMBER = $TRACKNUMBER" + fi + + # Rename $file + RenameFile + done +