Skip to content

Instantly share code, notes, and snippets.

@polypoyo
Last active April 7, 2022 02:16
Show Gist options
  • Save polypoyo/76acc2fc56bc3f6df1ec7732455d24c3 to your computer and use it in GitHub Desktop.
Save polypoyo/76acc2fc56bc3f6df1ec7732455d24c3 to your computer and use it in GitHub Desktop.
My media helper script
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# Defaults. Change using cli args
SRCDIR="Music/Rick Astley"
OUTDIR="Music/Rick Astley Converted"
# Sometimes change to "opus" if you use yt-dlp or youtube-dl
SRCEXT=mp3
OUTEXT=mp3
# Location of ffmpeg
ffm=/usr/bin/ffmpeg
# Assists in relative path weirdness
reset=$(pwd)
error() {
CODE=$1
shift
echo "Critical error:" "${@}"
exit "$CODE"
}
usage() {
cat <<EOF
MediaHelper: Mass-apply cover art via ffmpeg
-D: Dry run, don't convert anything
-i [dir]: Input directory
-o [dir]: Output directory
-h: Show this help
Experimental options:
-e [ext]: Set file ext
-E [ext]: Set input file ext (extra experimental)
EOF
exit "$1"
}
while getopts "Dhi:o:e:E:" o; do
case "${o}" in
D) ffm="echo Dry run: ffmpeg " ;;
i) SRCDIR=${OPTARG} ;;
o) OUTDIR=${OPTARG} ;;
e)
OUTEXT=${OPTARG}
SRCEXT=${OPTARG}
;;
E) SRCEXT=${OPTARG} ;;
h) usage 0 ;;
*) usage 2 ;;
esac
done
shift $((OPTIND - 1))
mkdir -p "$OUTDIR"
# This cd-dance helps relative paths work
cd "$SRCDIR" || error 2 "cd to SRCDIR fail"
for i in *."$SRCEXT"; do
y=${i%."$SRCEXT"}
cd "$reset" || error 2 "cd to reset fail"
$ffm \
-i "$SRCDIR/$i" \
-i "$SRCDIR/cover.png" \
-c copy \
-map 0 \
-map 1 \
"$OUTDIR/$y.$OUTEXT" || error 127 "FFMpeg exited badly!"
cd "$reset" || error 2 "cd to reset fail"
cd "$SRCDIR" || error 2 "cd to SRCDIR fail"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment