#!/bin/bash # Includes #----------------------------------------------------------------------------------------------------------------------------------- . /usr/global/lib/music.bash # System Constants #----------------------------------------------------------------------------------------------------------------------------------- AC_NAME="album_convert" AC_VERSION="$Name: album_convert-1_0_0-1 $" # Print Version #----------------------------------------------------------------------------------------------------------------------------------- function version_print() { echo ${AC_VERSION} | sed -e 's/.*: //' -e 's/-/ /' -e 's/_/\./g' -e 's/\$$//' } # Prin Help #----------------------------------------------------------------------------------------------------------------------------------- function help_print() { echo "${AC_NAME} [-h | --help] | [-V | --version] | [-T | --test] [-v | --verbose] [-r | --resample] [-d | --downsampled_tag] []" } # Arg Parse #----------------------------------------------------------------------------------------------------------------------------------- function args_parse() { mode="default" verbose="false" resample="false" dstag="flase" source_dir="" target_dir="" tmp_args=$(getopt -o ThVvrd --long test,help,version,verbose,resample,downsampled_tag -n "${AC_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="exit"; help_print; shift;; -V|--version) mode="exit"; version_print; shift;; # Global options -v|--verbose) verbose="true"; shift;; -r|--resample) resample="true"; shift;; -d|--downsampled_tag) dstag="true"; shift;; # --) shift; break;; *) echo "args_parse internal error [$1] !"; exit 1;; esac done if [[ "${mode}" != "exit" ]] then if [[ "${#}" != "2" ]] then if [[ "${#}" -lt "2" ]] then echo "Not enough args!" else echo "Too many args!" fi mode="exit" help_print else source_dir="$1" target_dir="$2" fi fi } # Main #----------------------------------------------------------------------------------------------------------------------------------- args_parse "$@" if [[ "${mode}" == "exit" ]] then exit 0 else if [[ "${mode}" == "test" ]] then dry_run=true else dry_run=false fi fi if [[ "${resample}" == "true" ]] then resample_flags="-af \"aresample=out_sample_fmt=s16:out_sample_rate=44100\"" else resample_flags="" fi echo "mode: [${mode}] verbose: [${verbose}] resample : [${resample}] downsampled_tag : [${dstag}] source_dir: [${source_dir}] target_dir: [${target_dir}]" 1>&2 if [[ ! -d "${target_dir}" ]] then echo "Target directory doesn't exit !" exit -1 fi for file in "${source_dir}"/*@(.flac|.ogg|.mp3|.m4a|.ape|.wav) do id="$(basename "${file}" | sed -e "s/-.*//")" target_file="${target_dir}/${prefix}$(basename "${file}" | sed -e 's/\.[^.]*$//').flac" cmd="ffmpeg -i \"${file}\" -compression_level 12 ${resample_flags} \"${target_file}\"" exec_cmd "${cmd}" done if [[ ${resample} == "true" ]] then for file in "${target_dir}"/*.flac do if [[ "${dstag}" == "true" ]] then tag="DESCRIPTION" value="$(tag_read "${file}" "${tag}")" if [[ "$( echo "${value}" | grep "Downsampled")" == "" ]] then value="${value}\nDownsampled" tag_delete "${file}" "${tag}" tag_write "${file}" "${tag}" "${value}" fi fi for tag in "ALBUM" "TITLE" do value="$(tag_read "${file}" "${tag}")" new_value="$(echo ${value} | sed -e 's/ - HD$//')" if [[ "${value}" != "${new_value}" ]] then tag_delete "${file}" "${tag}" tag_write "${file}" "${tag}" "${new_value}" fi done cmd="mv \"${file}\" \"${file/-hd.flac/.flac}\"" exec_cmd "${cmd}" done fi