Git Repos / dotfiles / bin / tag_rg.sh
Last update to this file was on 2024-01-02 at 05:12.
Show tag_rg.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/16 cev, updated 2021/07/23 and 2022/01/29
## Usage: tag_rg.sh [-3fhV] <file>
## 
##     Simple shim/frontend script to mp3gain and, somewhat unnecessarily,
##     metaflac --add-replay-gain. Adds replaygain_track_gain and replaygain
##     _album_gain id3 tags to an mp3, or the full replaygain complement
##     to a flac.
## 
##       -3     Operate on mp3 files
##       -f     Operate on flac files
##       -h     Show help
##       -V     Print version info
#
[ -x $HOME/.local/bin/cev_print.sh ] && . $HOME/.local/bin/cev_print.sh
self="${0##*'/'}"
filetype=""
fileext=""
rgfile=""
if [ $self = "flac_rg.sh" -o $self = "flac_rg" ]; then
    filetype="flac"
    fileext="flac"
elif [ $self = "mp3_rg.sh" -o $self = "mp3_rg" ]; then
    filetype="mp3"
    fileext="mp3"
fi
cleanup() {
    if [ -n "$rgfile" -a -e "$rgfile" ]; then rm $rgfile; fi
}
die_filenotfound() {
    print_filenotfound "$*"
    exit 1
}
die_nofile() {
    print_nofile
    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_rg_format_db() {
    printf "%.2f dB" "$mp3rgaindb"
}
# Not going to do any error checking and/or stderr redirection in here,
# because this is simple enough that I'd rather just see the error.
mp3_rg() {
    trap cleanup EXIT
    rgfile=$(mktemp ${TMPDIR:-/tmp}/${self}.1.XXXXXXXX)
    mp3gain -q -s s -k -o "$@" 1>$rgfile
    zIFS="$IFS"
    IFS="       "
    # TODO: which one of these fields is album_peak / track_peak ? and/or
    # how do we calculate those values?
    # File \t MP3 gain \t dB gain \t Max Amplitude \t Max global_gain \t Min g_g
    while read mp3file mp3rgain mp3rgaindb mp3maxamp mp3maxgg mp3mingg; do
        [ "$mp3file" = "File" ] && continue
        [ "$mp3file" = "\"Album\"" ] && break
        mid3v2 --delete-frames="TXXX" "$mp3file"
        mp3rgaindb=$(mp3_rg_format_db "$mp3rgaindb")
        mid3v2 --TXXX "replaygain_track_gain:$mp3rgaindb" "$mp3file"
    done < $rgfile
    IFS="$zIFS"
    if [ "$mp3file" = "\"Album\"" ]; then
        mp3rgaindb=$(mp3_rg_format_db "$mp3rgaindb")
        for i in "$@"; do
            mid3v2 --TXXX "replaygain_album_gain:$mp3rgaindb" "$i"
        done
    fi
    [ -f "$rgfile" ] && rm $rgfile
}
flac_rg() {
    # a thousand thanks to the flac team for making this sane / easy.
    # This script could have just been called "mp3_rg.sh", and I could have
    # certainly left this flac "capability" out; I've included it just so I
    # don't have to remember the option to metaflac. Not that it's hard to
    # remember...
    metaflac --add-replay-gain "$@"
}
while getopts "3fhV?" option; do
    case $option in
        3) filetype="mp3" && fileext="mp3" ;;
        f) filetype="flac" && fileext="flac" ;;
        h) print_help && exit ;;
        V) print_version && exit ;;
        "?") print_help && exit ;;
    esac
    shift $((OPTIND - 1))
done
[ $# -lt 1 ] && die_nofile
[ ! -e "$*" ] && die_filenotfound "$*"
if [ "$filetype" ]; then
    eval func=\${filetype}_rg    
elif is_flac "$@"; then
    filetype="flac" && fileext="flac"
    func="flac_rg"
elif is_mp3 "$@"; then
    filetype="mp3" && fileext="mp3"
    func="mp3_rg"
else
    die_unknownfiletype "$*"
fi
$func "$@"
exit 0
Return to the top of this page or return to the overview of this repo.
Log tag_rg.sh
| Date | Commit Message | Author | + | - | 
|---|---|---|---|---|
| 2024-01-02 | Change from FreeBSD to Linux, other minor changes | cev | +1 | -1 | 
| 2022-01-29 | Adding first batch of audio file handling scripts. | cev | +140 | 
Return to the top of this page or return to the overview of this repo.