Skip to content

Instantly share code, notes, and snippets.

@theKAKAN
Last active December 24, 2023 17:25
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 theKAKAN/a8770aab56829edf4dfa4c5d6cea2354 to your computer and use it in GitHub Desktop.
Save theKAKAN/a8770aab56829edf4dfa4c5d6cea2354 to your computer and use it in GitHub Desktop.
Download music and save to m3u playlist using yt-dlp
#!/bin/bash
# 0-file means this is important for the script to run (script itself and sources list)
# 1-file means its important in a re-run (download log)
# 2-file would mean its for user's convenience (m3u playlist)
# 3-file would mean its for debugging/seeing if something went wrong
# Should work for all playlists
yt-dlp \
--verbose \
--download-archive "./1-downloaded.txt" \
--restrict-filenames --windows-filenames \
-o "./%(playlist)s/%(title)s-%(id)s.%(ext)s" \
-f 'bestaudio/best' \
--cookies-from-browser firefox \
--embed-thumbnail --embed-metadata \
-x --audio-format mp3 --audio-quality 320K \
--print-to-file "after_move:filepath" "./2-%(playlist)s.m3u" \
--batch-file "./0-sources.txt" \
2>&1 | tee "./3-output.log"
# The new songs are added to the end of the playlist, which is okay for one of
# them, but I mostly add them to the top. However, there's a newline after
# every download, so I can manually shift them as I need.
# The playlists file have full paths, which are not only useless, but also
# limit their portability. So, we remove that
CURDIR="$(pwd)/"
for file in ./*.m3u;
do
# From testing, this command seems to work fine, so need for backup
# Backup the file first
#cp "$file" "$file.bak"
# Process it
echo "$(sed -e "s#$CURDIR##" $file)" > "$file";
done
# No need for: --write-thumbnail \, since its embedded into the mp3 file
# Line 18: Hardcoded mp3, because %(ext)s would return the video's format
link_to_playlist1
link_to_playlist2
and_so_on
@theKAKAN
Copy link
Author

I save to 320kbps mp3 (highest quality for mp3) just because it's compatible with pretty much every music player out there. YouTube does 120kbps AAC for most things (I think?), so it doesn't really matter. You can use something like flac, but then you're just upscaling and ballooning your file sizes to gain nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment