Commit 67b075b8 authored by Cédric Marie's avatar Cédric Marie
Browse files

tag-sieve: Add commands for single files



When a directory contains several audio files that are not part of an
album, it is useful to set or clean tags in a single file.
Moreover, for these single files, some tags - album, track number, and
disc number - don't make sense and must not be set.

Two new commands are available:
- clean-single
- set-single
Signed-off-by: default avatarCédric Marie <cedric.marie@openmailbox.org>
parent 1edcd4ca
......@@ -16,11 +16,12 @@
print_usage()
{
command=$(basename $0)
printf "Usage: $command [OPTION] [COMMAND] [OPTION]...\n"
printf "Apply COMMAND to audio files in current directory.\n\n"
printf "Usage: $command [-h] [COMMAND] [OPTION]... [FILE]\n"
printf "Apply COMMAND to audio files in current directory, or to FILE\n"
printf "if COMMAND is clean-single or set-single.\n\n"
printf "Options:\n"
printf " -h display this message\n"
printf "Commands:\n"
printf " -h display this message\n\n"
printf "Commands applied to all audio files in current directory (album):\n"
printf " show show tags\n"
printf " rmblocks remove seek table, picture, cue sheet, and padding blocks\n"
printf " (FLAC files only)\n"
......@@ -41,7 +42,19 @@ print_usage()
printf " seta ARTIST ALBUM DATE\n"
printf " set artist, album, and date tags in the first track\n"
printf " upt update title tags from filenames\n"
printf " uptn update track number tags from filenames\n"
printf " uptn update track number 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"
printf " - ARTIST\n"
printf " - TITLE\n"
printf " - DATE\n"
printf " - COMMENT (if present)\n"
printf " - LICENSE (if present)\n"
printf " Options for \"clean-single\" command:\n"
printf " -d keep full date (default: keep only the year)\n"
printf " set-single FILE ARTIST TITLE DATE\n"
printf " set artist, title, and date tags in file FILE\n"
}
print_header()
......@@ -333,6 +346,105 @@ if [ "$command" = "clean" ]; then
exit 0
fi
# -----------------------------------------------------------------------------
# clean-single
# -----------------------------------------------------------------------------
if [ "$command" = "clean-single" ]; then
keep_date=0
shift 1
while getopts d OPT
do
case $OPT in
d) keep_date=1;;
*) print_usage; exit 1;;
esac
done
shift $(($OPTIND - 1))
file=$1
tags_filename=/tmp/tag-sieve-clean.tags
# FLAC file
if [ "${file%.flac}" != "$file" ]; then
# Get information
metaflac --export-tags-to=$tags_filename "$file"
tag_artist=`grep "^ARTIST=" $tags_filename | tail -n 1`
tag_artist=${tag_artist#ARTIST=}
tag_title=`grep "^TITLE=" $tags_filename | tail -n 1`
tag_title=${tag_title#TITLE=}
tag_date=`grep "^DATE=" $tags_filename | tail -n 1`
tag_date=${tag_date#DATE=}
if [ "$keep_date" = "0" ]; then
tag_date=${tag_date%%-*}
fi
tag_comment=`grep "^COMMENT=" $tags_filename | tail -n 1`
tag_comment=${tag_comment#COMMENT=}
tag_license=`grep "^LICENSE=" $tags_filename | tail -n 1`
tag_license=${tag_license#LICENSE=}
print_header "Set tags in this file"
# Set read-write permission
chmod 644 "$file"
# Set tags in this file
printf "ARTIST=$tag_artist\n" > $tags_filename
printf "TITLE=$tag_title\n" >> $tags_filename
printf "DATE=$tag_date\n" >> $tags_filename
if [ "$tag_comment" != "" ]; then
printf "COMMENT=$tag_comment\n" >> $tags_filename
fi
if [ "$tag_license" != "" ]; then
printf "LICENSE=$tag_license\n" >> $tags_filename
fi
old_size=`stat -c %s "$file"`
metaflac --remove-all-tags --dont-use-padding "$file"
metaflac --import-tags-from=$tags_filename "$file"
new_size=`stat -c %s "$file"`
printf "(saved %4d bytes) $file\n" $(($old_size - $new_size))
rm -f $tags_filename
# Set read-only permission
chmod 444 "$file"
# MP3 file
elif [ "${file%.mp3}" != "$file" ]; then
# Get information
id3v2 --list "$file" > $tags_filename
tag_artist=`grep "^TPE1 " $tags_filename`
tag_artist=${tag_artist#*: }
tag_title=`grep "^TIT2 " $tags_filename`
tag_title=${tag_title#*: }
tag_date=`grep "^TYER " $tags_filename`
tag_date=${tag_date#*: }
print_header "Set tags in this file"
# Set read-write permission
chmod 644 "$file"
id3v2 --list "$file" > $tags_filename
old_size=`stat -c %s "$file"`
id3v2 --delete-all "$file" > /dev/null
id3v2 --id3v2-only --artist "$tag_artist" "$file"
id3v2 --id3v2-only --song "$tag_title" "$file"
id3v2 --id3v2-only --year "$tag_date" "$file"
new_size=`stat -c %s "$file"`
printf "(saved %4d bytes) $file\n" $(($old_size - $new_size))
rm -f $tags_filename
# Set read-only permission
chmod 444 "$file"
fi
exit 0
fi
# -----------------------------------------------------------------------------
# seta
# -----------------------------------------------------------------------------
......@@ -384,9 +496,69 @@ if [ "$command" = "seta" ]; then
file=`ls -1 *.mp3 | grep -m 1 mp3`
# Set read-write permission
chmod 644 "$file"
id3v2 --artist "$tag_artist" "$file"
id3v2 --album "$tag_album" "$file"
id3v2 --year "$tag_date" "$file"
id3v2 --id3v2-only --artist "$tag_artist" "$file"
id3v2 --id3v2-only --album "$tag_album" "$file"
id3v2 --id3v2-only --year "$tag_date" "$file"
# Set read-only permission
chmod 444 "$file"
fi
exit 0
fi
# -----------------------------------------------------------------------------
# set-single
# -----------------------------------------------------------------------------
if [ "$command" = "set-single" ]; then
file=$2
tag_artist=$3
tag_title=$4
tag_date=$5
if [ "$tag_artist" = "" ]; then
printf "ARTIST parameter is missing\n"
exit 1
fi
if [ "$tag_title" = "" ]; then
printf "TITLE parameter is missing\n"
exit 1
fi
if [ "$tag_date" = "" ]; then
printf "DATE parameter is missing\n"
exit 1
fi
# Print album information
printf "\033[7m"
printf "ARTIST=$tag_artist\n"
printf "TITLE=$tag_title\n"
printf "DATE=$tag_date\n"
printf "\033[0m"
confirm_to_continue
print_header "Set artist, title, and date in this file"
# FLAC files
if [ "$flac_files" != "" ]; then
# Set read-write permission
chmod 644 "$file"
metaflac --set-tag="ARTIST=$tag_artist" "$file"
metaflac --set-tag="TITLE=$tag_title" "$file"
metaflac --set-tag="DATE=$tag_date" "$file"
# Set read-only permission
chmod 444 "$file"
# MP3 files
elif [ "$mp3_files" != "" ]; then
# Set read-write permission
chmod 644 "$file"
id3v2 --id3v2-only --artist "$tag_artist" "$file"
id3v2 --id3v2-only --song "$tag_title" "$file"
id3v2 --id3v2-only --year "$tag_date" "$file"
# Set read-only permission
chmod 444 "$file"
......@@ -461,6 +633,7 @@ if [ "$command" = "uptn" ]; then
if [ "${first_file%% - *}" = "1" ]; then
multi_disc=1
current_disc=
last_file=`ls -1r *.flac | grep -m 1 flac`
tag_disctotal=${last_file%% - *}
print_header "Set track/disc numbers from filename and track/disc total in all tracks"
......@@ -508,6 +681,7 @@ if [ "$command" = "uptn" ]; then
if [ "${first_file%% - *}" = "1" ]; then
multi_disc=1
current_disc=
last_file=`ls -1r *.mp3 | grep -m 1 mp3`
tag_disctotal=${last_file%% - *}
print_header "Set track/disc numbers from filename in all tracks"
......
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