- Add music_tools lib,

- Migrate album_metadata_load .
This commit is contained in:
2026-04-11 23:52:08 +02:00
parent 7abfcab917
commit 6f09c6c806
7 changed files with 833 additions and 85 deletions

View File

@@ -113,7 +113,7 @@ ac_args_parse()
#
--) shift; break;;
*) echo "args_parse internal error [$1] !"; exit 1;;
*) echo "args_parse internal error [$1] !"; exit 1;;
esac
done

View File

@@ -1,103 +1,138 @@
#!/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
#-----------------------------------------------------------------------------------------------------------------------------------
. /usr/global/lib/music.bash
: "${RX3_LIB_DIR:=/usr/lib/rx3}"
. "${RX3_LIB_DIR}/music_tools.bash"
# System Constants
#-----------------------------------------------------------------------------------------------------------------------------------
# Global Variables
#-----------------------------------------------------------------------------------------------------------------------------------
AML_NAME="album_metadata_load"
AML_VERSION="$Name: album_metadata_load-1_0_0-1 $"
declare -g VERSION="1.0.0"
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 MODE="DEFAULT"
declare -g VERBOSE="FALSE"
declare -g DRY_RUN="FALSE"
# Print Version
#-----------------------------------------------------------------------------------------------------------------------------------
# Version Print
#-----------------------------------------------------------------------------------------------------------------------------------
function version_print()
aml_version_print()
{
echo ${AML_VERSION} | sed -e 's/.*: //' -e 's/-/ /' -e 's/_/\./g' -e 's/\$$//'
version_print
}
# Prin Help
#-----------------------------------------------------------------------------------------------------------------------------------
# Help Print
#-----------------------------------------------------------------------------------------------------------------------------------
function help_print()
aml_help_print()
{
echo "${AML_NAME} [-h | --help] | [-V | --version] | [-T | --test] [-v | --verbose] [-c | --clean] <source_dir> <target_dir> <metadata_file>"
aml_version_print
help_print
}
#-----------------------------------------------------------------------------------------------------------------------------------
# Arg Parse
#-----------------------------------------------------------------------------------------------------------------------------------
function args_parse()
aml_args_parse()
{
mode="default"
verbose="false"
clean="false"
source_dir=""
target_dir=""
metadata_file=""
tmp_args=$(getopt -o ThVvc --long test,help,version,verbose,clean -n "${AML_NAME}" -- "$@")
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
while true
do
case "$1" in
# Options
# Mode switches
-T|--test) mode="test"; shift;;
-h|--help) mode="exit"; help_print; shift;;
-V|--version) mode="exit"; version_print; shift;;
-T|--test) MODE="TEST"; shift;;
-h|--help) MODE="EXIT"; aml_help_print; shift;;
-V|--version) MODE="EXIT"; aml_version_print; shift;;
# Global options
-v|--verbose) verbose="true"; shift;;
-c|--clean) clean="true"; shift;;
-v|--verbose) VERBOSE="TRUE"; shift;;
-c|--clean) CLEAN="TRUE"; shift;;
#
--) shift; break;;
*) echo "args_parse internal error [$1] !"; exit 1;;
#
--) shift; break;;
*) echo "args_parse internal error [$1] !"; exit 1;;
esac
done
if [[ "${mode}" != "exit" ]]
if [[ "${MODE}" != "EXIT" ]]
then
if [[ "${#}" != "3" ]]
if [[ "${#}" != "3" ]]
then
if [[ "${#}" -lt "3" ]]
then
echo "Not enough args!"
echo_error "Not enough args!"
else
echo "Too many args!"
echo_error "Too many args!"
fi
mode="exit"
help_print
MODE="EXIT"
aml_help_print
else
source_dir="$1"
target_dir="$2"
metadata_file="$3"
SOURCE_DIR="$1"
TARGET_DIR="$2"
METADATA_FILE="$3"
fi
fi
}
@@ -106,63 +141,59 @@ function args_parse()
#-----------------------------------------------------------------------------------------------------------------------------------
# Main
#-----------------------------------------------------------------------------------------------------------------------------------
args_parse "$@"
aml_args_parse "$@"
if [[ ${mode} == "exit" ]]
if [[ "${MODE}" == "EXIT" ]]
then
exit 0
else
if [[ ${mode} == "test" ]]
if [[ "${MODE}" == "TEST" ]]
then
dry_run=true
else
dry_run=false
DRY_RUN="TRUE"
fi
fi
echo_error "${NAME}: Mode: [${MODE}] Verbose: [${VERBOSE}] Clean: [${CLEAN}] Source_Dir: [${SOURCE_DIR}] Target_Dir: [${TARGET_DIR}] MetaData_File: [${METADATA_FILE}]"
echo "mode: [${mode}] verbose: [${verbose}] clean: [${clean}] source_dir: [${source_dir}] target_dir: [${target_dir}] metadata_file: [${metadata_file}]" 1>&2
mt_tagtab_alloc
tagtab_alloc
echo_error "Reading MetaData..."
echo "Reading MetaData..."
tagtab_load "${metadata_file}" "DEFAULT"
mt_tagtab_load "${METADATA_FILE}" "DEFAULT"
nb_track=${track_id}
echo "Nb track found: ${nb_track}"
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
# title="${tagtab["${track_id},TITLE"]}"
tag_get title "${track_id}" "TITLE"
suffix="${track_file##*.}"
target_file="$(printf "%02d" ${track_id})-${title}.${suffix}"
target_file="$(fix_file_name "${target_file}" "")"
target_file="${target_dir}/$(fix_file_name "${target_file}" "")"
echo "Copying ${track_file} into ${target_file}"
cmd="cp \"${track_file}\" \"${target_file}\""
exec_cmd "${cmd}"
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}" "")"
if [[ "${clean}" == "true" ]]
then
tag_save "${target_file}" "${track_id}" "CLEAN"
else
tag_save "${target_file}" "${track_id}" "NOCLEAN"
fi
echo_error "Copying ${track_file} into ${target_file}"
cmd_exec cp "${track_file}" "${target_file}"
track_id=$((${track_id} + 1))
done
)
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
)