Commit 819454ed authored by Cédric Marie's avatar Cédric Marie
Browse files

Add tag-sieve script



This script cleans the tags of audio files (FLAC and MP3) in current
directory. It keeps only the useful information.
Signed-off-by: default avatarCédric Marie <cedric.marie@openmailbox.org>
parent 33c5a00f
#!/bin/sh
# tag-sieve - Clean audio tags
# This file is part of Painted Shell - Shell configuration and scripts.
#
# Written in 2014 by Cédric Marie <cedric.marie@openmailbox.org>
#
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication along
# with this software.
# If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
print_usage()
{
command=$(basename $0)
printf "Usage: $command [OPTION] [COMMAND] [OPTION]...\n"
printf "Apply COMMAND to audio files in current directory.\n\n"
printf "Options:\n"
printf " -h display this message\n"
printf "Commands:\n"
printf " show show tags\n"
printf " rmblocks remove seek table, picture, and padding blocks (FLAC files only)\n"
printf " clean set only following tags (and remove padding):\n"
printf " - ARTIST (from first track if not multi-artist)\n"
printf " - TITLE\n"
printf " - ALBUM (from first track)\n"
printf " - DATE (from first track)\n"
printf " - TRACKNUMBER\n"
printf " - TRACKTOTAL (from first track if not multi-disc)\n"
printf " - DISCNUMBER (if multi-disc)\n"
printf " - DISCTOTAL (from first track if multi-disc)\n"
printf " - COMMENT (from first track, if present)\n"
printf " - LICENSE (from first track, if present)\n"
printf " Options for \"clean\" command:\n"
printf " -d keep full date (default: keep only the year)\n"
printf " -a multi-artist (read artist in each track)\n"
}
print_header()
{
printf "\n\033[1;32m$1\033[0m\n"
}
confirm_to_continue()
{
printf "Do you want to continue (y/N)? "
read answer
if [ "$answer" != "y" ]; then
exit 0
fi
}
while getopts h OPT
do
case $OPT in
h) print_usage; exit 0;;
*) print_usage; exit 1;;
esac
done
# Find audio file type
flac_files=`ls *.flac 2> /dev/null`
if [ "$flac_files" = "" ]; then
mp3_files=`ls *.mp3 2> /dev/null`
if [ "$mp3_files" = "" ]; then
printf "Could not find either FLAC or MP3 files in current directory\n"
exit 1
fi
fi
command=$1
# -----------------------------------------------------------------------------
# show
# -----------------------------------------------------------------------------
if [ "$command" = "show" ]; then
# FLAC files
if [ "$flac_files" != "" ]; then
ls -1 *.flac |
{
while read file
do
printf "$file\n"
printf "\033[7m"
metaflac --list "$file"
printf "\033[0m\n"
done
}
fi
exit 0
fi
# -----------------------------------------------------------------------------
# rmblocks
# -----------------------------------------------------------------------------
if [ "$command" = "rmblocks" ]; then
# FLAC files
if [ "$flac_files" != "" ]; then
# Set read-write permission
chmod 644 *.flac
# Remove seek table and padding
metaflac --remove --block-type=SEEKTABLE *.flac
metaflac --remove --block-type=PICTURE *.flac
metaflac --dont-use-padding --remove --block-type=PADDING *.flac
# Set read-only permission
chmod 444 *.flac
fi
exit 0
fi
# -----------------------------------------------------------------------------
# clean
# -----------------------------------------------------------------------------
if [ "$command" = "clean" ]; then
keep_date=0
multi_artist=0
shift 1
while getopts da OPT
do
case $OPT in
d) keep_date=1;;
a) multi_artist=1;;
*) print_usage; exit 1;;
esac
done
# FLAC files
if [ "$flac_files" != "" ]; then
tag_artist=
tag_album=
tag_date=
tag_tracknumber=
tag_tracktotal=
tag_discnumber=
tag_disctotal=
tag_comment=
tag_license=
multi_disc=0
tags_filename=/tmp/tag-sieve-clean.tags
file=`ls -1 *.flac | grep -m 1 flac`
print_header "Get album information from the first track ($file)"
metaflac --export-tags-to=$tags_filename "$file"
tag_artist=`grep "^ARTIST=" $tags_filename | cut -d= -f2 | tail -n 1`
tag_album=`grep "^ALBUM=" $tags_filename | cut -d= -f2 | tail -n 1`
tag_date=`grep "^DATE=" $tags_filename | cut -d= -f2 | tail -n 1`
if [ "$keep_date" = "0" ]; then
tag_date=${tag_date%%-*}
fi
tag_tracktotal=`grep "^TRACKTOTAL=" $tags_filename | cut -d= -f2 | tail -n 1`
tag_discnumber=`grep "^DISCNUMBER=" $tags_filename | cut -d= -f2 | tail -n 1`
if [ "$tag_discnumber" != "" ]; then
tag_disctotal=`grep "^DISCTOTAL=" $tags_filename | cut -d= -f2 | tail -n 1`
fi
tag_comment=`grep "^COMMENT=" $tags_filename | cut -d= -f2 | tail -n 1`
tag_license=`grep "^LICENSE=" $tags_filename | cut -d= -f2 | tail -n 1`
if [ "$tag_discnumber" != "" -a "$tag_disctotal" = "" ]; then
printf "DISCTOTAL is missing, please enter a value for this tag: "
read tag_disctotal
fi
printf "\033[7m"
if [ "$multi_artist" = "0" ]; then
printf "ARTIST=$tag_artist\n"
fi
printf "ALBUM=$tag_album\n"
printf "DATE=$tag_date\n"
if [ "$tag_discnumber" = "" ]; then
printf "TRACKTOTAL=$tag_tracktotal\n"
else
multi_disc=1
printf "DISCTOTAL=$tag_disctotal\n"
fi
if [ "$tag_comment" != "" ]; then
printf "COMMENT=$tag_comment\n"
fi
if [ "$tag_license" != "" ]; then
printf "LICENSE=$tag_license\n"
fi
printf "\033[0m"
confirm_to_continue
print_header "Set tags in all tracks"
# Set read-write permission
chmod 644 *.flac
ls -1 *.flac |
{
while read file
do
metaflac --export-tags-to=$tags_filename "$file"
if [ "$multi_artist" = "1" ]; then
tag_artist=`grep "^ARTIST=" $tags_filename | cut -d= -f2 | tail -n 1`
fi
tag_title=`grep "^TITLE=" $tags_filename | cut -d= -f2 | tail -n 1`
tag_tracknumber=`grep "^TRACKNUMBER=" $tags_filename | cut -d= -f2 | tail -n 1`
if [ "$multi_disc" = "1" ]; then
tag_tracktotal=`grep "^TRACKTOTAL=" $tags_filename | cut -d= -f2 | tail -n 1`
tag_discnumber=`grep "^DISCNUMBER=" $tags_filename | cut -d= -f2 | tail -n 1`
fi
printf "ARTIST=$tag_artist\n" > $tags_filename
printf "TITLE=$tag_title\n" >> $tags_filename
printf "ALBUM=$tag_album\n" >> $tags_filename
printf "DATE=$tag_date\n" >> $tags_filename
printf "TRACKNUMBER=$tag_tracknumber\n" >> $tags_filename
printf "TRACKTOTAL=$tag_tracktotal\n" >> $tags_filename
if [ "$multi_disc" = "1" ]; then
printf "DISCNUMBER=$tag_discnumber\n" >> $tags_filename
printf "DISCTOTAL=$tag_disctotal\n" >> $tags_filename
fi
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))
done
}
rm -f $tags_filename
# Set read-only permission
chmod 444 *.flac
fi
exit 0
fi
# -----------------------------------------------------------------------------
# unknown command
# -----------------------------------------------------------------------------
print_usage
exit 1
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