Disclaimer: Downloading from YouTube/SoundCloud via scripts may violate ToS. This example uses legal, authorized sources only (Bandcamp API, YouTube Data API with permission).
import os, requests, yt_dlp # yt-dlp for public domain/CC content only
def download_bandcamp(album_url, out_dir="."):
"""Download purchased Bandcamp album via API (requires OAuth)."""
# Simplified: use bandcamp-downloader CLI instead
os.system(f"bandcamp-dl '{album_url}' --base-dir '{out_dir}'")
def download_youtube_audio(url, out_dir=".", format="bestaudio"):
"""Download audio from YouTube (only if CC/public domain or you own rights)."""
ydl_opts = {
'format': format,
'outtmpl': os.path.join(out_dir, '%(title)s.%(ext)s'),
'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3'}],
'noplaylist': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
# Usage (legal content only):
# download_youtube_audio('https://youtube.com/watch?v=CC_LICENSED_VIDEO')
# download_bandcamp('https://artist.bandcamp.com/album/album-name')
Install: pip install yt-dlp bandcamp-dl. Use responsibly.