Skip to content

Instantly share code, notes, and snippets.

@Ssenseii
Created August 7, 2025 23:09
Show Gist options
  • Select an option

  • Save Ssenseii/9bf5b2b0059d0f369fc9208e10ba46c1 to your computer and use it in GitHub Desktop.

Select an option

Save Ssenseii/9bf5b2b0059d0f369fc9208e10ba46c1 to your computer and use it in GitHub Desktop.
Download your spotify tracks using your spotify data and python (requires python, ffmpeg, ytd-lp)
import json
import subprocess
import time
import os
import math
# ===== CONFIG =====
JSON_FILE = "tracks.json"
OUTPUT_DIR = "music"
AUDIO_FORMAT = "mp3"
SLEEP_BETWEEN = 5 # seconds between downloads
AVERAGE_DOWNLOAD_TIME = 20 # estimated seconds per track (adjust if needed)
# ==================
# Create music folder if not exists
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Load track list
with open(JSON_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
total_tracks = len(data["tracks"])
# Estimate total time
estimated_total_seconds = (AVERAGE_DOWNLOAD_TIME + SLEEP_BETWEEN) * total_tracks
estimated_minutes = math.ceil(estimated_total_seconds / 60)
print(f"\n🎡 Found {total_tracks} tracks in {JSON_FILE}")
print(f"πŸ“‚ Output folder: {OUTPUT_DIR}")
print(f"⏳ Estimated total time: ~{estimated_minutes} minutes "
f"({AVERAGE_DOWNLOAD_TIME + SLEEP_BETWEEN} sec per track)")
print("=" * 50, "\n")
# Loop through tracks
for i, entry in enumerate(data["tracks"], start=1):
artist = entry['artist'].strip()
track = entry['track'].strip()
query = f"{artist} - {track}"
filename = f"{artist} - {track}".replace("/", "-") # avoid folder issues
print(f"[{i}/{total_tracks}] 🎢 Starting download: {query}")
# yt-dlp command
cmd = [
"yt-dlp",
f"ytsearch1:{query}",
"-x",
"--audio-format", AUDIO_FORMAT,
"-o", os.path.join(OUTPUT_DIR, f"{filename}.%(ext)s")
]
print(f"πŸ” Searching & downloading from YouTube...")
print(f"πŸ’Ύ Saving as {filename}.{AUDIO_FORMAT} in {OUTPUT_DIR}/")
print("-" * 50)
# Run command and stream output directly to terminal
process = subprocess.Popen(cmd)
process.wait() # Wait until finished
if process.returncode == 0:
print(f"βœ… Downloaded successfully: {query}")
else:
print(f"❌ Failed to download: {query}")
print("-" * 50)
time.sleep(SLEEP_BETWEEN)
print("\nπŸŽ‰ All downloads completed!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment