djcev.com

//

Git Repos / dotfiles / bin / tag_key.sh

Last update to this file was on 2024-01-02 at 05:12.

Show tag_key.sh

#!/bin/sh
#
# Copyright (c) 2015-2022, Cameron Vanderzanden. Available under the
# terms of the 2-clause BSD license. Please see the file "LICENSE" in
# the root dir of this repository for more information.
#
#- tag_key.sh 2016/01/05 cev, updated 2021/07/23 and 2022/01/29.
## Usage: tag_key.sh [-3fhwV] <file>
##
## Shell script frontend for Mark Hills' *nix port of Ibrahim Sha'ath's
## excellent "keyfinder" program.
##
## -3 Operate on mp3 files
## -f Operate on flac files
## -h Show help
## -w Write detected key to KEY tag and COMMENT tag.
## -V Print version info
#
# See http://www.ibrahimshaath.co.uk/keyfinder/ ,
# https://github.com/ibsh/is_KeyFinder , and www.pogo.org.uk/~mark/key-tools/
# for more information.
#
# TODO: Split writing to KEY and COMMENT to different options.

[ -x $HOME/.local/bin/cev_print.sh ] && . $HOME/.local/bin/cev_print.sh

self="${0##*'/'}"
keybin="$HOME/.local/bin/key"
filetype=""
fileext=""
write=""

if [ $self = "flac_key.sh" -o $self = "flac_key" ]; then
filetype="flac"
fileext="flac"
elif [ $self = "mp3_key.sh" -o $self = "mp3_key" ]; then
filetype="mp3"
fileext="mp3"
fi

die_filenotfound() {
print_filenotfound "$*"
exit 1
}

die_istagged() {
print_self error
printf "%b%s%b already tagged, key: %b%s%b\n" \
"$_high" "$*" "$_norm" "$_high" "$key" "$_clear" >&2
exit 1
}

die_nofile() {
print_nofile
exit 1
}

die_nokeyfound() {
print_self error
printf "%bcould not detect a key for file " "$_norm" >&2
printf "%b%s%b.%b\n" "$_high" "$*" "$_norm" "$_clear" >&2
exit 1
}

die_unknownfiletype() {
shortname="$(basename "$*")"
print_unknownfiletype "$shortname"
exit 1
}

is_flac() { test_mimetype.sh -t "audio/x-flac" "$@" ; }

is_mp3() {
if test_mimetype.sh -t "audio/mpeg" "$@"; then
return 0
else
# test file extension
tmpname="$(basename "$@" ".mp3")"
tmpname2="${@##$tmpname}"
if [ "$tmpname2" = ".mp3" ]; then
return 0
fi
fi
return 1
}

mp3_comment() {
comm="$(mid3v2 --list "$@" | grep COMM | sed -e 's/COMM=//' | sed -e 's/^=eng=//')"
}

flac_comment() {
comm="$(metaflac --show-tag=COMMENT "$@" | sed -e 's/COMMENT=//')"
}

append_comment() {
if [ -z "$comm" ]; then
comm="$@"
else
comm="$@ - $comm"
fi
}

is_tagged() {
if [ -n "$key" ] && ! $force; then
die_istagged "$*"
fi
}

wrap_key() {
# TODO error checking
key="$(sox -V1 "$@" -r 44100 -e float -c 1 -t raw - | "$keybin")"
[ -z "$key" ] && die_nokeyfound "$*"
}

flac_key() {
key="$(metaflac --show-tag=INITIALKEY "$@" | sed -e 's/INITIALKEY=//')"
is_tagged "$@"
wrap_key "$@"
flac_comment "$@"
append_comment "$key"
key="$(printf "%s" "$key" | cut -d' ' -f 1)"
print_key "$key" "$comm"
if [ "$write" ]; then
metaflac --remove-tag=INITIALKEY --set-tag="INITIALKEY=$key" "$@"
metaflac --remove-tag=COMMENT --set-tag="COMMENT=$comm" "$@"
fi
}

mp3_key() {
key="$(mid3v2 --list "$@" | grep TKEY | sed -e 's/TKEY=//')"
is_tagged "$@"
wrap_key "$@"
mp3_comment "$@"
append_comment "$key"
key="$(printf "%s" "$key" | cut -d' ' -f 1)"
print_key "$key" "$comm"
if [ "$write" ]; then
mid3v2 --TKEY "$key" "$@"
mid3v2 -c "$comm" "$@"
fi
}

print_key() {
print_self
printf "%bkey '%b%s%b', " "$_norm" "$_high" "$1" "$_norm"
printf "comment: %b%.49s%b\n" "$_high" "$2" "$_clear"
}

while getopts "3fhwV?" option; do
case $option in
3) filetype="mp3" && fileext="mp3" ;;
f) filetype="flac" && fileext="flac" ;;
h) print_help && exit ;;
w) write="yes" ;;
V) print_version && exit ;;
"?") print_help && exit ;;
esac
shift $((OPTIND - 1))
done

[ $# -lt 1 ] && die_nofile
[ ! -f "$*" ] && die_filenotfound "$*"

if [ "$filetype" ]; then
eval func=\${filetype}_key
elif is_flac "$*"; then
filetype="flac" && fileext="flac"
func="flac_key"
elif is_mp3 "$*"; then
filetype="mp3" && fileext="mp3"
func="mp3_key"
else
die_unknownfiletype "$*"
fi

$func "$*"
exit 0

Return to the top of this page or return to the overview of this repo.

Log tag_key.sh

Date Commit Message Author + -
2024-01-02 Change from FreeBSD to Linux, other minor changes cev +2 -2
2022-01-29 Adding first batch of audio file handling scripts. cev +177  

Return to the top of this page or return to the overview of this repo.