Skip to content

Instantly share code, notes, and snippets.

@teklynk
Created January 4, 2026 21:14
Show Gist options
  • Select an option

  • Save teklynk/364751844f738a97ea556ab360978eba to your computer and use it in GitHub Desktop.

Select an option

Save teklynk/364751844f738a97ea556ab360978eba to your computer and use it in GitHub Desktop.
yt-dlp-music script that pulls album art, artist and track names. Pulls entire album using playlist url
#!/bin/bash
# Example Usage:
# ./yt-dlp-music.sh mp3 https://music.youtube.com/playlist?list=OLAK5uy_nsxFbHsI16bmvO4j8X3mrwMhsORFIOZ1w
output_dir="$HOME/Music"
# YT-DLP (YouTube Downloader)
sudo wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -O /usr/local/bin/yt-dlp
sudo chmod a+rx /usr/local/bin/yt-dlp
# Check if the required yt-dlp package is installed
command -v /usr/local/bin/yt-dlp >/dev/null 2>&1 || { echo "Error: yt-dlp is not installed. Please install it using your package manager." >&2; exit 1; }
# Format type: mp3, m4a, opus
format=$1
# Get the YouTube music URL from the first argument
url=$2
# Check if the FORMAT is provided
if [ -z "$format" ]; then
echo "Error: YouTube music FORMAT is required."
exit 1
fi
# Check if the URL is provided
if [ -z "$url" ]; then
echo "Error: YouTube music URL is required."
exit 1
fi
# Extract audio from YouTube video and save it
/usr/local/bin/yt-dlp -xciw -f "bestaudio/best" --cookies $HOME/scripts/music.youtube.com_cookies.txt --extract-audio --audio-format "$format" --audio-quality 0 --embed-thumbnail --embed-metadata "$url" -o "$output_dir/%(artist)s/%(album)s/%(title)s.%(ext)s"
# Loop through all .m4a files in the output directory
for m4a_file in "$output_dir"/*.m4a; do
# Check if the file exists
if [ -f "$m4a_file" ]; then
# Get the base name of the file without the extension
base_name=$(basename "$m4a_file" .m4a)
# Define the output MP3 file
mp3_file="$output_dir/$base_name.mp3"
# Convert the M4A file to MP3
ffmpeg -i "$m4a_file" "$mp3_file"
# Check if the conversion was successful
if [ $? -eq 0 ]; then
echo "Conversion successful: $m4a_file -> $mp3_file"
rm "$m4a_file"
echo "Removed original file: $m4a_file"
else
echo "Error: Conversion failed for $m4a_file"
fi
fi
done
# Loop through all .opus files in the output directory
for opus_file in "$output_dir"/*.opus; do
# Check if the file exists
if [ -f "$opus_file" ]; then
# Get the base name of the file without the extension
base_name=$(basename "$opus_file" .opus)
# Define the output MP3 file
mp3_file="$output_dir/$base_name.mp3"
# Convert the OPUS file to MP3
ffmpeg -i "$opus_file" "$mp3_file"
# Check if the conversion was successful
if [ $? -eq 0 ]; then
echo "Conversion successful: $opus_file -> $mp3_file"
rm "$opus_file"
echo "Removed original file: $opus_file"
else
echo "Error: Conversion failed for $opus_file"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment