misc-scripts/albumsetup

134 lines
4.5 KiB
Bash
Executable File

#!/bin/sh
set -euf
EXT="flac" # default
NL="
"
err() { echo "$*" >&2 ; }
die() { err "$*" && exit 1 ; }
clean() {
trap 'exit' INT HUP EXIT
rm -f "$PICTURE"
}
# wrapper to grab a specific tag from flac/opus/mp3
grabtag() {
case ${1##*.} in
flac) metaflac --show-tag="$2" "$1" | cut -d '=' -f 2- ;;
opus) opusinfo "$1" | awk -v RS='\n\t' -v FS='=' -v OFS='' "/^$2=/"'{$1=""; print $0}' ;;
mp3)
case "$2" in
ARTIST) id3 -q '%_a' "$1" ;;
ALBUM) id3 -q '%_A' "$1" ;;
TRACKNUMBER) id3 -q '%_###T' "$1" ;;
TITLE) id3 -q '%_t' "$1" ;;
ALBUMARTIST) id3 -2 -q "%|%{TPE2}||%{TXXX:ALBUM ARTIST}|?" "$1" ;;
*) printf 'unknown' ;;
esac
;;
*) die 'unknown tag used for mp3'
esac
}
# grab track info, make tracknumber 3-padded just in case for long albums
# (no album has > 999 tracks. I think.)
grabinfo() {
DURATION="$(ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$1")"
ARTIST="$(grabtag "$1" ARTIST)"
ALBUM="$(grabtag "$1" ALBUM)"
TITLE="$(grabtag "$1" TITLE)"
TRACKNUMBER="$(printf '%03d' "$(grabtag "$1" TRACKNUMBER)")"
}
ffconv() {
IFS=" "
ffmpeg ${VERBOSE--loglevel error} -y -loop 1 -framerate 4 -f image2 -i "$1" ${FULLALBUM:+-safe 0 -f concat} -i "$2" \
-t "${TOTALTIME:-$DURATION}" \
-pix_fmt yuv420p \
${acopy:--c:a libopus -b:a 256k} \
-r 4 \
-b:v 500k \
-c:v libvpx \
-cpu-used -5 \
-deadline realtime \
"$3"
err "Successfully converted $2"
}
SONG="" FULLALBUM="" AUTOPIC=""
OUTDIR="/tmp/albumsetup/${PWD##*/}"
while getopts :aovd:e:p:s: OPT; do
case "$OPT" in
a) AUTOPIC=1 ;; # autopic
o) FULLALBUM=1 ;;
v) VERBOSE="" ;; # can use ${var-rep} for this
d) cd "$OPTARG" && OUTDIR="${OUTDIR%/*}/${PWD##*/}" ;;
e) EXT="$OPTARG" ;;
p) tmpimg="$OPTARG" ;;
s) SONG="$OPTARG" && OUTDIR="${OUTDIR%/*}" ;; # individual song
*) die "albumsetup: invalid option: -$OPTARG" ;;
esac
done
shift "$((OPTIND - 1))"
[ ! -f "$tmpimg" ] && die "Image not specified! This is required even with AUTOPIC!"
[ "$AUTOPIC" ] && [ "$SONG" ] && die "AUTOPIC doesn't make sense in SONG mode!"
[ "$AUTOPIC" ] && [ "$FULLALBUM" ] && die "AUTOPIC doesn't make sense in FULLALBUM mode!"
mkdir -p "$OUTDIR"
# convert cover image. it has to be wider than 1:1 so youtube doesn't convert it to a short
# if image is wider than 1:1, just resize height. if not, pad with black
PICTURE="$OUTDIR/ALBUMSETUP_IMG.png"
magick convert "$tmpimg" -resize 1280x720 -background black -gravity center -extent 722x720 "$PICTURE"
trap 'clean' INT HUP EXIT
[ "$EXT" = "opus" ] && acopy="-c:a copy" # copy audio codec if opus since output codec is opus
# individual song
if [ "$SONG" ]; then
grabinfo "$SONG"
ffconv "$PICTURE" "$SONG" "$OUTDIR/SONG ${ARTIST%%/*} - ${TITLE%%/*}.webm" # substitution mods are to not create directories
# one continuous video with timestamps
elif [ "$FULLALBUM" ]; then
TOTALTIME=0 # keeping track of timestamps
IFS="$NL"
for f in $(fd -d 1 -e "$EXT"); do
grabinfo "$f"
# make timestamp
printf '%02d:%02d:%02d - %s\n' \
"$((${TOTALTIME%%.*} / 3600))" "$((${TOTALTIME%%.*} % 3600 / 60))" "$((${TOTALTIME%%.*} % 60))" \
"$ARTIST - $TITLE" >> "$OUTDIR/metadata.txt"
# add to total time, but this is as a float remember that
TOTALTIME="$(echo "$TOTALTIME + $DURATION" | bc)"
# build ffmpeg concat metadata
sf="$(printf '%s' "$f" | sed "s/'/'\\\\''/g")" # make safe filename for ffmpeg concat; replace all ' with '\''
echo "file '${PWD}/$sf'" >> "$OUTDIR/ffmpeg_tracklist.txt"
done
[ ! -f "$OUTDIR/ffmpeg_tracklist.txt" ] && die "No files found!"
ffconv "$PICTURE" "$OUTDIR/tracklist.txt" "$OUTDIR/$ARTIST - $ALBUM.webm"
rm "$OUTDIR/ffmpeg_tracklist.txt"
else # individual tracks for a full album
IFS="$NL"
for f in $(fd -d 1 -e "$EXT"); do
grabinfo "$f"
# metadata print
printf '%s %s - %s - %s\n' "${TRACKNUMBER:-000}" "${ARTIST:-unknown artist}" "${ALBUM:-unknown album}" "${TITLE:-unknown title}" >> "$OUTDIR/metadata.txt"
# try making auto pic
# if $PIC_AUTO doesn't exist (i.e. trackXXX.(jpg|png), use $PICTURE as fallback
[ "$AUTOPIC" ] && PIC_AUTO="$(fd -e jpg -e png "^track$TRACKNUMBER")"
err "Converting $f"
ffconv "${PIC_AUTO:-$PICTURE}" "$f" "$OUTDIR/$TRACKNUMBER ${ARTIST%%/*} - ${TITLE%%/*}.webm"
done
[ ! -f "$OUTDIR/metadata.txt" ] && die "No files found!"
# sort by correct track numbers then remove track numbers later
sort -o "$OUTDIR/metadata.txt.sorted" "$OUTDIR/metadata.txt"
while read -r line; do
echo "$line" | cut -d ' ' -f 2-
done <"$OUTDIR/metadata.txt.sorted" >"$OUTDIR/metadata.txt"
rm "$OUTDIR/metadata.txt.sorted"
fi