Files
music_tools/bin/album_metadata_load
Arnaud G. GIBERT 7cbd0dc0c9 - Add VSL support,
- Support now rx3-base 1.2.0.
2026-09-13 12:14:25 +02:00

215 lines
6.1 KiB
Bash
Executable File

#!/bin/bash
#-----------------------------------------------------------------------------------------------------------------------------------
#
# Album MetaData Load
#
# Copyright (C) 2016-2026 Arnaud G. GIBERT
# mailto:arnaud@rx3.net
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see
# <https://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------------
# Includes
#-----------------------------------------------------------------------------------------------------------------------------------
: "${RX3_LIB_DIR:=/usr/lib/rx3}"
. "${RX3_LIB_DIR}/music_tools.bash"
#-----------------------------------------------------------------------------------------------------------------------------------
# Global Variables
#-----------------------------------------------------------------------------------------------------------------------------------
declare -g VERSION="1.1.3"
declare -g NAME="Album_MetaData_Load"
declare -g HELP="usage: [-h | --help] | [-V | --version] | [-T | --test] [-v | --verbose] [-c | --clean] <Source_Dir> <Target_Dir> <MetaData_File>"
declare -g CLEAN="FALSE"
declare -g SOURCE_DIR=""
declare -g TARGET_DIR=""
declare -g METADATA_FILE=""
declare -g EXIT_CODE=0
declare -g MODE="DEFAULT"
declare -g VERBOSE="FALSE"
declare -g DRY_RUN="FALSE"
#-----------------------------------------------------------------------------------------------------------------------------------
# Version Print
#-----------------------------------------------------------------------------------------------------------------------------------
aml_version_print()
{
version_print
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Help Print
#-----------------------------------------------------------------------------------------------------------------------------------
aml_help_print()
{
help_print
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Arg Parse
#-----------------------------------------------------------------------------------------------------------------------------------
aml_args_parse()
{
tmp_args=$(getopt -o ThVvc --long test,help,version,verbose,clean -n "${NAME}" -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "${tmp_args}"
while true
do
case "$1" in
# Options
# Mode switches
-T|--test) MODE="TEST"; shift;;
-h|--help) MODE="HELP"; shift;;
-V|--version) MODE="VERSION"; shift;;
# Global options
-v|--verbose) VERBOSE="TRUE"; shift;;
-c|--clean) CLEAN="TRUE"; shift;;
# End of options
--) shift; break;;
*) echo "args_parse internal error [$1] !"; exit 1;;
esac
done
if [[ "${MODE}" == "DEFAULT" ]]
then
if [[ "${#}" != "3" ]]
then
if [[ "${#}" -lt "3" ]]
then
echo_error "Not enough args!"
else
echo_error "Too many args!"
fi
MODE="HELP"
EXIT_CODE=1
else
SOURCE_DIR="$1"
TARGET_DIR="$2"
METADATA_FILE="$3"
fi
fi
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------------------------------------------------------------------
vsl_add
aml_args_parse "$@"
case "${MODE}" in
"EXIT")
exit ${EXIT_CODE}
;;
"HELP")
aml_help_print
exit ${EXIT_CODE}
;;
"TEST")
DRY_RUN="TRUE"
;;
"VERSION")
aml_version_print
exit ${EXIT_CODE}
;;
esac
echo_error "${NAME}: Mode: [${MODE}] Verbose: [${VERBOSE}] Clean: [${CLEAN}] Source_Dir: [${SOURCE_DIR}] Target_Dir: [${TARGET_DIR}] MetaData_File: [${METADATA_FILE}]"
mt_tagtab_alloc
echo_error "Reading MetaData..."
mt_tagtab_load "${METADATA_FILE}" "DEFAULT"
nb_track=${track_id}
echo_error "Nb track found: ${nb_track}"
track_id=1
find "${SOURCE_DIR}" -maxdepth 1 -name '*.flac' -o -name '*.mp3' | sort |
(
while read track_file
do
mt_tag_get title "${track_id}" "TITLE"
suffix="${track_file##*.}"
target_file="$(printf "%02d" ${track_id})-${title}.${suffix}"
target_file="$(mt_fix_file_name "${target_file}" "")"
target_file="${TARGET_DIR}/$(mt_fix_file_name "${target_file}" "")"
echo_error "Copying ${track_file} into ${target_file}"
cmd_exec cp "${track_file}" "${target_file}"
if [[ "${CLEAN}" == "TRUE" ]]
then
mt_tag_save "${target_file}" "${track_id}" "CLEAN"
else
mt_tag_save "${target_file}" "${track_id}" "NOCLEAN"
fi
track_id=$((${track_id} + 1))
done
)
exit ${EXIT_CODE}