Git Repos / dotfiles / bin / flac_to_mp3.sh
Last update to this file was on 2024-01-02 at 05:12.
Show flac_to_mp3.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.
#
#- flac_to_mp3.sh 2015/09/01 cev
#- flac_to_mp3.sh 2021/07/23 cev
## Usage: flac_to_mp3.sh [-hV] [-c file] [-d directory] [-o file] <input file>
##
## Convert a flac file to an mp3 file, preserving tags.
##
## -c Cover image
## -d Output base directory
## -h Show help
## -o Output file name
## -r Apply replaygain to output file
## -V Print version info
# This hasn't been touched in a while and may or may not work. Haven't
# needed it since I stopped transcoding to mp3 in scs4tool.sh.
[ -x $HOME/.local/bin/cev_print.sh ] && . $HOME/.local/bin/cev_print.sh
self="${0##*'/'}"
cwd=""
coverfile=""
outdir=""
flacfile=""
mp3file=""
rgain=""
wavfile=""
ttags=""
tpic=""
cleanup() {
if [ "$ttags" -a -r "$ttags" ]; then rm "$ttags"; fi
if [ "$tpic" -a -r "$tpic" ]; then rm "$tpic"; fi
if [ "${tpic}.jpg" -a -r "${tpic}.jpg" ]; then rm "${tpic}.jpg"; fi
if [ "$wavfile" -a -r "$wavfile" ]; then rm "$wavfile"; fi
}
die_filenotfound() {
print_filenotfound "$*"
exit 1
}
die_nofile() {
print_nofile
exit 1
}
cleartags() {
A=""; TPE2=""; a=""; TBPM=""; c=""; y=""; g=""; TKEY=""; t=""; tt=""; T=""
rgag=""; rgap=""; rgtg=""; rgtp=""
export A TPE2 a TBPM c y g TKEY t tt T rgag rgap rgtg rgtp
}
readcover() {
pic="$(metaflac --list --block-type=PICTURE "$flacfile")"
if [ -n "$pic" ]; then
tpic=$(mktemp ${TMPDIR:-/tmp}/${self}.pic.XXXXXXXX)
metaflac --export-picture-to="$tpic.jpg" "$flacfile"
coverfile="$tpic.jpg"
fi
}
readtags() {
metaflac --no-utf8-convert --export-tags-to="$ttags" "$flacfile"
while read i; do
k=$(printf "%s" "$i" | cut -d '=' -f 1)
# convert ':' into '_' because ':' will mess up mp3 comments.
# shows up most often in URLs; example: 'visit http://'... comments
v=$(printf "%s" "$i" | cut -d '=' -f 2- | tr ':' '_')
case $k in
album|ALBUM) A="$v" ;;
albumartist|ALBUMARTIST) TPE2="$v" ;;
artist|ARTIST) a="$v" ;;
comment|COMMENT) c="$v" ;;
bpm|BPM) TBPM="$v" ;;
date|DATE) y="$v" ;;
genre|GENRE) g="$v" ;;
initialkey|INITIALKEY) TKEY="$v" ;;
title|TITLE) t="$v" ;;
tracktotal|TRACKTOTAL) tt="$v" ;;
tracknumber|TRACKNUMBER) T="$v" ;;
replaygain_album_gain|REPLAYGAIN_ALBUM_GAIN) rgag="$v" ;;
replaygain_album_peak|REPLAYGAIN_ALBUM_PEAK) rgap="$v" ;;
replaygain_track_gain|REPLAYGAIN_TRACK_GAIN) rgtg="$v" ;;
replaygain_track_peak|REPLAYGAIN_TRACK_PEAK) rgtp="$v" ;;
esac
done < $ttags
}
writecover() {
if [ -f "$coverfile" ]; then
eyeD3 --add-image="$coverfile:FRONT_COVER:" "$mp3file" 1>/dev/null 2>&1
fi
}
writetags() {
file="$mp3file"
[ "$A" ] && mid3v2 -A "$A" "$file"
[ "$TPE2" ] && mid3v2 --TPE2 "$TPE2" "$file"
[ "$a" ] && mid3v2 -a "$a" "$file"
[ "$TBPM" ] && mid3v2 --TBPM "$TBPM" "$file"
[ "$c" ] && mid3v2 -c "$c" "$file"
[ "$y" ] && mid3v2 -y "$y" "$file"
[ "$g" ] && mid3v2 -g "$g" "$file"
[ "$TKEY" ] && mid3v2 --TKEY "$TKEY" "$file"
[ "$t" ] && mid3v2 -t "$t" "$file"
if [ "$T" ]; then
if [ "$tt" ]; then mid3v2 -T "$T/$tt" "$file"
else mid3v2 -T "$T" "$file"
fi
fi
[ "$rgag" ] && mid3v2 --TXXX "replaygain_album_gain:$rgag" "$file"
[ "$rgap" ] && mid3v2 --TXXX "replaygain_album_peak:$rgap" "$file"
[ "$rgtg" ] && mid3v2 --TXXX "replaygain_track_gain:$rgtg" "$file"
[ "$rgtp" ] && mid3v2 --TXXX "replaygain_track_peak:$rgtp" "$file"
}
flac_decode() {
if [ "$rgain" = "Y" ]; then
flac --totally-silent \
--apply-replaygain-which-is-not-lossless=0t \
-o "$wavfile" -d "$flacfile"
else
flac --totally-silent -o "$wavfile" -d "$flacfile"
fi
}
mp3_encode() {
lame --preset insane --add-id3v2 -S "$wavfile" "$mp3file" 1>/dev/null 2>&1
}
while getopts "c:d:ho:rV?" option; do
case $option in
c) coverfile="$OPTARG" ;;
d) outdir="$OPTARG" ;;
h) print_help && exit ;;
o) mp3file="$OPTARG" ;;
r) rgain="Y" ;;
V) print_version && exit ;;
"?") print_help && exit ;;
esac
shift $((OPTIND - 1))
done
[ $# -lt 1 ] && die_nofile
[ ! -f "$@" ] && die_filenotfound "$*"
trap cleanup EXIT
ttags=$(mktemp ${TMPDIR:-/tmp}/${self}.tags.XXXXXXXX)
[ ! "$outdir" ] && outdir="./"
base=$(basename "$@" flac)
flacfile="$@"
[ ! "$mp3file" ] && mp3file="${base}mp3"
wavfile="${TMPDIR:-/tmp}/${base}wav"
print_self
printf "%breading cover from %b%.45s%b\n" \
"$_norm" "$_high" "$flacfile" "$_clear"
[ ! "$coverfile" ] && readcover
print_self
printf "%breading tags from %b%.46s%b\n" "$_norm" "$_high" "$flacfile" "$_clear"
readtags
print_self
printf "%bdecoding %b%.55s%b\n" "$_norm" "$_high" "$flacfile" "$_clear"
flac_decode
print_self
printf "%bencoding %b%.55s%b\n" "$_norm" "$_high" "$mp3file" "$_clear"
mp3_encode
print_self
printf "%bwriting cover to %b%.47s%b\n" "$_norm" "$_high" "$mp3file" "$_clear"
writecover
print_self
printf "%bwriting tags to %b%.48s%b\n" "$_norm" "$_high" "$mp3file" "$_clear"
writetags
print_self
printf "%bfinished converting %b%.44s%b\n" \
"$_norm" "$_high" "$flacfile" "$_clear"
Return to the top of this page or return to the overview of this repo.
Log flac_to_mp3.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 | +189 |
Return to the top of this page or return to the overview of this repo.