shell-scripts/rename-tracknumber

245 lines
10 KiB
Bash
Executable file

#!/bin/bash
############################################################
# Rename TRACKNUMBER #
# #
# Rename all of the files in a folder based on track #
# number metadata. Currently, there is only support for #
# flac files with the `.flac` extention at the end of the #
# name. #
# #
# Change History #
# 03/11/2023 Out Of Ideas Original code. This is a #
# template for creating new #
# Bash shell script #
# Add new history entries as #
# needed. #
# #
# 02/01/2024 Out Of Ideas The program worked pretty #
# well in the first test. One #
# of the files was named #
# incorrectly. The program #
# only includes support for #
# two-diget track numbers. A #
# goal for the future, is to #
# use a for loop instead of if #
# statements to remove the #
# limit of digits. It is a #
# concern, that the script #
# may malfunction with #
# filenames that include #
# special characters. If this #
# is a problem, then instead #
# of double-quotes, `print` #
# will be used to escape #
# special characters. Also so #
# for, flac is the only #
# filetype with support. More #
# filetypes may be added in #
# future versions. #
# #
# 02/01/2024 Out Of Ideas File renaming issue fixed. #
# #
# 02/01/2024 Out Of Ideas MP3 functionality has been #
# added; not tested. #
# #
############################################################
############################################################
############################################################
# #
# This software is hereby released into the public domain. #
# #
# Anyone is free to copy, modify, publish, use, compile, #
# sell, or distribute this software, either in source code #
# form or as a compiled binary, for any purpose, with or #
# without modification. There are no restrictions or #
# requirements for the use of this software. #
# #
# This software is provided "as is," without any warranty, #
# express or implied, including but not limited to the #
# warranties of merchantability, fitness for a particular #
# purpose, or non-infringement. In no event shall the #
# authors be liable for any claim, damages, or other #
# liability, whether in an action of contract, tort, or #
# otherwise, arising from, out of, or in connection with #
# this software or the use or other dealings in this #
# software. #
# #
# To the fullest extent possible under law, the authors #
# have waived all copyright and patent rights to this #
# software. You are free to use this software without #
# any restrictions on copyright or patent rights. #
# #
# This Public Domain declaration, including the waiver of #
# copyright and patent rights, applies to the full extent #
# permitted by applicable laws and regulations. #
# #
############################################################
############################################################
############################################################
# Help #
############################################################
Help()
{
# Display Help
echo 'Rename all of the files in a folder based on track number metadata. Currently, there is only support for flac files with the `.flac` extention at the end of the name.'
echo
echo "Syntax: Rename TRACKNUMBER [-h|p|v|V]"
echo "options:"
echo "h Print this Help."
echo "p Print the Public Domain declaration."
echo "v Verbose mode."
echo "V Print version and exit."
echo
}
############################################################
# Public Domain Declaration #
############################################################
PUBLIC_DOMAIN_DECLARATION='
This software is hereby released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, with or without modification. There are no restrictions or requirements for the use of this software.
This software is provided "as is," without any warranty, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, or non-infringement. In no event shall the authors be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with this software or the use or other dealings in this software.
To the fullest extent possible under law, the authors have waived all copyright and patent rights to this software. You are free to use this software without any restrictions on copyright or patent rights.
This Public Domain declaration, including the waiver of
copyright and patent rights, applies to the full extent
permitted by applicable laws and regulations.
'
############################################################
# Version #
############################################################
VERSION="Rename TRACKNUMBER v2.0"
############################################################
# Check for root #
############################################################
CheckRoot()
{
# If we are not running as root, we exit the program.
if [ `id -u` != 0 ]
then
echo "ERROR: You must be root user to run this program"
exit
fi
}
############################################################
# Check for user #
############################################################
CheckUser()
{
# If we are running as root, we exit the program.
if [ `id -u` == 0 ]
then
echo "ERROR: You must NOT be root user to run this program"
exit
fi
}
############################################################
# Check for dependencies #
############################################################
CheckMetaflac()
{
# If 'metaflac' command is not found, we exit the program
if ! type metaflac &> /dev/null; then
echo "Error: 'metaflac' command not found. Please install metaflac."
exit 1
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\" \"$TRACKNUMBER - $file\""
fi
mv "$file" "$TRACKNUMBER - $file"
fi
}
############################################################
# Main Program #
############################################################
############################################################
# Sanity checks #
############################################################
# Are we running as root?
#CheckRoot
CheckUser
#Is metaflac command found?
CheckMetaflac
############################################################
# Initialize variables #
############################################################
option=""
Msg=""
# Unset variables
unset VERBOSE
unset MUSIC
############################################################
# Process the input options. Add options as needed. #
############################################################
# Get the options
while getopts ":hpvV" option;
do
case $option in
h) # display Help
Help
exit;;
p) # display $PUBLIC_DOMAIN_DECLARATION
echo $PUBLIC_DOMAIN_DECLARATION
exit;;
v) # enable $VERBOSE
VERBOSE=true;;
V) # display $VERSION
echo $VERSION
exit;;
\?) # incorrect option
echo "Error: Invalid option"
exit;;
esac
done
# Display a message before the program starts
#echo "$Msg"
# Loop through all .flac and .mp3 files
for file in *.flac *.mp3; do
# Ensure only songs are renamed.
if [[ $file == *.flac ]]; then
TRACKNUMBER=$(metaflac --show-tag=TRACKNUMBER "$file" | head -n 1 | sed -r 's/^TRACKNUMBER=([0-9]+).*$/\1/')
elif [[ $file == *.mp3 ]]; then
TRACKNUMBER=$(ffprobe "$file" 2>&1 | grep 'track' | awk '{print $3}' | sed "s/\/[0-9]*//")
else
# Skip non-music files
continue
fi
if [ $VERBOSE ]; then
echo "TRACKNUMBER = $TRACKNUMBER"
fi
# Rename $file
RenameFile
done