Commit c2549163 authored by Cédric Marie's avatar Cédric Marie

myaudiotags: Update filenames from title tags

Instead of updating the title tags from the filenames, provide a
command to set the title tag ("title" command), and update the
filenames from the title tags ("filenames" command).

The advantages are:
* You don't have to manually move (mv) the files to set the titles.
* If a title contains a '/' (and the filename must use a '-' instead),
  you don't have to change it in the title tag after the update. It is
  now automatically changed to '-' in the filename.

The "update" command (now called "numbers") doesn't update the title
tags anymore. It only updates the track/disc number/total tags.
Signed-off-by: default avatarCédric Marie <cedric@hjuvi.lautre.net>
parent 3278c562
......@@ -39,7 +39,10 @@ print_usage()
printf " set artist, album, and date tags in the first track\n"
printf " setc COMMENT\n"
printf " set comment tag in the first track\n"
printf " update update title and track/disc number/total tags from filenames\n\n"
printf " title NB TITLE\n"
printf " set title tag of track #NB\n"
printf " filenames update filenames from title tags\n"
printf " numbers update track/disc number/total tags from filenames\n\n"
printf "Commands applied to a single audio file:\n"
printf " clean-single FILE\n"
printf " set only following tags (and remove padding) in file FILE:\n"
......@@ -402,6 +405,118 @@ if [ "$command" = "clean-single" ]; then
exit 0
fi
# -----------------------------------------------------------------------------
# title
# -----------------------------------------------------------------------------
if [ "$command" = "title" ]; then
track_number=$2
tag_title=$3
if [ "$track_number" = "" ]; then
printf "NB parameter is missing\n"
exit 1
fi
if [ "$tag_title" = "" ]; then
printf "TITLE parameter is missing\n"
exit 1
fi
nb_files=`ls -1 ${track_number}.* | wc -l`
if [ "$nb_files" != "1" ]; then
printf "Failed to find track #$track_number\n"
exit 1
fi
file=`ls -1 ${track_number}.*`
printf "$file => \033[7m${tag_title}\033[0m\n"
if [ "$flac_files" != "" ]; then
# FLAC file
# Set read-write permission
chmod 644 "$file"
# Remove title tag before it is set again
metaflac --remove-tag=TITLE "$file"
# Set title tag
metaflac --set-tag="TITLE=$tag_title" "$file"
# Set read-only permission
chmod 444 "$file"
elif [ "$mp3_files" != "" ]; then
# MP3 file
# Set read-write permission
chmod 644 "$file"
# Set title tag
id3v2 --id3v2-only --song "$tag_title" "$file"
# Set read-only permission
chmod 444 "$file"
fi
exit 0
fi
# -----------------------------------------------------------------------------
# filenames
# -----------------------------------------------------------------------------
if [ "$command" = "filenames" ]; then
print_header "Set all filenames from title tags"
if [ "$flac_files" != "" ]; then
# FLAC files
ls -1 *.flac |
{
while read file
do
tag_title=`metaflac --show-tag=TITLE "$file" | tail -n 1`
tag_title=${tag_title#TITLE=}
# Replace '/' with '-' in the filename
filename_title=`echo $tag_title | sed 's!/!-!g'`
track_number=${file%%. *}
new_file="${track_number}. ${filename_title}.flac"
if [ "$new_file" != "$file" ]; then
mv -v "$file" "$new_file"
fi
if [ "$filename_title" = "$tag_title" ]; then
printf "$new_file\n"
else
printf "$new_file \033[0;33m('/' replaced with '-')\033[0m\n"
fi
printf "\033[7m${tag_title}\033[0m\n"
done
}
elif [ "$mp3_files" != "" ]; then
# MP3 files
ls -1 *.mp3 |
{
while read file
do
tag_title=`id3v2 --list "$file" | grep "^TIT2 "`
tag_title=${tag_title#*: }
# Replace '/' with '-' in the filename
filename_title=`echo $tag_title | sed 's!/!-!g'`
track_number=${file%%. *}
new_file="${track_number}. ${filename_title}.mp3"
if [ "$new_file" != "$file" ]; then
mv -v "$file" "$new_file"
fi
if [ "$filename_title" = "$tag_title" ]; then
printf "$new_file\n"
else
printf "$new_file \033[0;33m('/' replaced with '-')\033[0m\n"
fi
printf "\033[7m${tag_title}\033[0m\n"
done
}
fi
exit 0
fi
# -----------------------------------------------------------------------------
# seta
# -----------------------------------------------------------------------------
......@@ -561,16 +676,16 @@ if [ "$command" = "set-single" ]; then
fi
# -----------------------------------------------------------------------------
# update
# numbers
# -----------------------------------------------------------------------------
if [ "$command" = "update" ]; then
if [ "$command" = "numbers" ]; then
multi_disc=0
# FLAC files
if [ "$flac_files" != "" ]; then
# FLAC files
first_file=`ls -1 *.flac | grep -m 1 flac`
if [ "${first_file%%.*}" = "1" ]; then
......@@ -578,10 +693,10 @@ if [ "$command" = "update" ]; then
current_disc=
last_file=`ls -1r *.flac | grep -m 1 flac`
tag_disctotal=${last_file%%.*}
print_header "Set title and track/disc number/total in all tracks (from filenames)"
print_header "Set track/disc number/total in all tracks (from filenames)"
else
tag_tracktotal=`ls -1 *.flac | grep -c flac`
print_header "Set title and track number/total in all tracks (from filenames)"
print_header "Set track number/total in all tracks (from filenames)"
fi
# Set read-write permission
......@@ -592,10 +707,7 @@ if [ "$command" = "update" ]; then
while read file
do
# Remove the tags before they are set again
metaflac --remove-tag=TITLE --remove-tag=TRACKNUMBER --remove-tag=TRACKTOTAL --remove-tag=DISCNUMBER --remove-tag=DISCTOTAL "$file"
# Remove .flac and leading disc and track numbers
tag_title=`echo ${file%.flac} | sed 's/^[1-9]\.//' | sed 's/^[0-9]\+\. //'`
metaflac --set-tag="TITLE=$tag_title" "$file"
metaflac --remove-tag=TRACKNUMBER --remove-tag=TRACKTOTAL --remove-tag=DISCNUMBER --remove-tag=DISCTOTAL "$file"
# Remove .flac and leading disc number (if present)
tag_tracknumber=`echo ${file%.flac} | sed 's/^[1-9]\.//'`
# Get the track number without leading 0
......@@ -609,20 +721,19 @@ if [ "$command" = "update" ]; then
tag_tracktotal=`ls -1 ${tag_discnumber}.*.flac | grep -c flac`
fi
metaflac --set-tag="DISCNUMBER=$tag_discnumber" --set-tag="DISCTOTAL=$tag_disctotal" "$file"
printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m, disc \033[7m$tag_discnumber\033[0m/\033[7m$tag_disctotal\033[0m: "
printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m, disc \033[7m$tag_discnumber\033[0m/\033[7m$tag_disctotal\033[0m: $file\n"
else
printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m: "
printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m: $file\n"
fi
metaflac --set-tag="TRACKNUMBER=$tag_tracknumber" --set-tag="TRACKTOTAL=$tag_tracktotal" "$file"
printf "\033[7m$tag_title\033[0m ($file)\n"
done
}
# Set read-only permission
chmod 444 *.flac
# MP3 files
elif [ "$mp3_files" != "" ]; then
# MP3 files
first_file=`ls -1 *.mp3 | grep -m 1 mp3`
if [ "${first_file%%.*}" = "1" ]; then
......@@ -630,10 +741,10 @@ if [ "$command" = "update" ]; then
current_disc=
last_file=`ls -1r *.mp3 | grep -m 1 mp3`
tag_disctotal=${last_file%%.*}
print_header "Set title and track/disc number/total in all tracks (from filenames)"
print_header "Set track/disc number/total in all tracks (from filenames)"
else
tag_tracktotal=`ls -1 *.mp3 | grep -c mp3`
print_header "Set title and track number/total in all tracks (from filenames)"
print_header "Set track number/total in all tracks (from filenames)"
fi
# Set read-write permission
......@@ -643,10 +754,7 @@ if [ "$command" = "update" ]; then
{
while read file
do
# Remove .mp3 and leading disc and track numbers
tag_title=`echo ${file%.mp3} | sed 's/^[1-9]\.//' | sed 's/^[0-9]\+\. //'`
id3v2 --id3v2-only --song "$tag_title" "$file"
# Remove .mp3 and leading disc number
# Remove .mp3 and leading disc number (if present)
tag_tracknumber=`echo ${file%.mp3} | sed 's/^[1-9]\.//'`
# Get the track number without leading 0
tag_tracknumber=${tag_tracknumber%%.*}
......@@ -659,12 +767,11 @@ if [ "$command" = "update" ]; then
tag_tracktotal=`ls -1 ${tag_discnumber}.*.mp3 | grep -c mp3`
fi
id3v2 --id3v2-only --TPOS "$tag_discnumber/$tag_disctotal" "$file"
printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m, disc \033[7m$tag_discnumber\033[0m/\033[7m$tag_disctotal\033[0m: "
printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m, disc \033[7m$tag_discnumber\033[0m/\033[7m$tag_disctotal\033[0m: $file\n"
else
printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m: "
printf "track \033[7m$tag_tracknumber\033[0m/\033[7m$tag_tracktotal\033[0m: $file\n"
fi
id3v2 --id3v2-only --track "$tag_tracknumber/$tag_tracktotal" "$file"
printf "\033[7m$tag_title\033[0m ($file)\n"
done
}
# Set read-only permission
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment