Skip to content

Instantly share code, notes, and snippets.

@cmsj
Last active January 19, 2024 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmsj/0bb6dda62bca13a3326fadf1f18d66f6 to your computer and use it in GitHub Desktop.
Save cmsj/0bb6dda62bca13a3326fadf1f18d66f6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script will:
# * Download DJ Electric Samurai's Progressive Psytrance mixes from youtube
# * split them into individual MP3s
# * tag them with a reasonable amount of metadata
# * import them into Music.app (née iTunes)
#
# Dependencies:
# * yt-dlp
# * mid3v2 (from the mutagen project)
set -u
set -o pipefail
DATA=~/.djelectricsamurai.dat
OUTDIR=~/Music/Music/Media.localized/Automatically\ Add\ to\ Music.localized
echo "Fetching channel playlist..."
PLAYLIST="$(yt-dlp --flat-playlist --print "%(id)s||%(title)s" 'https://www.youtube.com/@DJElectricSamurai' | grep -i "||Progressive Psytrance" | sed -E -e 's/[pP]rogressive [Pp]sytrance //' -e 's/mix //' -e 's#/##g')"
echo "${PLAYLIST}" | while read -r line ; do
ID="$(echo "${line}" | sed -e 's/\(.*\)||.*/\1/g')"
NAME="$(echo "${line}" | sed -e 's/.*||\(.*\)/\1/g')"
grep -q "${ID}" "${DATA}"
if [ "$?" == "0" ]; then
# We processed this one already
echo " skipping ${ID}"
continue
fi
TMPDIR="$(mktemp -d)"
if [ "$?" != "0" ]; then
echo "ERROR: Unable to mktemp"
exit 1
fi
echo " working in ${TMPDIR}"
pushd "${TMPDIR}" >/dev/null
echo " fetching ${ID}..."
yt-dlp -f 'bestaudio[ext=m4a]' -x --audio-format mp3 --audio-quality 0 --split-chapters -o 'chapter:%(section_title)s.%(ext)s' --add-metadata --write-thumbnail --embed-metadata --convert-thumbnails jpg --exec rm https://www.youtube.com/watch?v=${ID}
COVART="$(ls *.jpg | head -1)"
TCNT=$(ls *.mp3 | wc -l)
for track in *.mp3 ; do
TNUM="$(echo "${track}" | sed -e 's/\.\..*//')"
TART="$(echo "${track}" | sed -e 's/.*\.\.\(.*\) - .*/\1/')"
TNAM="$(echo "${track}" | sed -e 's/.* - \(.*\)\..*/\1/')"
echo " writing ID3 for ${NAME} :: ${TNUM} :: ${TNAM} :: -> ${track}"
mid3v2 --delete-frames=CHAP -A "${NAME}" -t "${TNAM}" --TPE2 "DJ Electric Samurai" -T "${TNUM}/${TCNT}" -a "${TART}" -p "${COVART}" -g "Progressive Psytrance" "${track}"
done
DST="${OUTDIR}/${NAME}"
mkdir "${DST}"
rm *.jpg
mv * "${DST}"
echo "${ID}" >> "${DATA}"
echo " leaving ${TMPDIR}"
popd >/dev/null
rmdir "${TMPDIR}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment