Are you tired of dealing with ad-riddled websites or paid software just to save a few educational videos or music playlists offline?
Python offers a clean, free, and powerful alternative. In this post, we will explore how to build a robust YouTube Playlist Downloader using Python in less than 20 lines of code. youtube playlist free downloader python script
python yt_playlist_dl.py "PLAYLIST_URL" --quality 720 --output ./my_videos 🐍 Automate Your Media Library: How to Download
#!/usr/bin/env python3
"""
Simple YouTube playlist downloader using yt-dlp.
Saves videos to ./output and shows progress.
"""
import os
import sys
from pathlib import Path
from yt_dlp import YoutubeDL
from tqdm import tqdm
# --- Config ---
OUTPUT_DIR = Path("output")
VIDEO_FORMAT = "bestvideo+bestaudio/best" # change to "bestaudio/best" for audio-only
CONCURRENT_DOWNLOADS = 1 # increase if you want parallel downloads (requires more care)
# --- Helpers ---
def ensure_output_dir():
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
class TqdmHook:
def __init__(self):
self.pbar = None
def progress_hook(self, d):
if d.get("status") == "downloading":
total = d.get("total_bytes") or d.get("total_bytes_estimate")
downloaded = d.get("downloaded_bytes", 0)
if total:
if not self.pbar:
self.pbar = tqdm(total=total, unit="B", unit_scale=True, desc=d.get("filename") or "download")
self.pbar.total = total
self.pbar.update(downloaded - self.pbar.n)
else:
# unknown total: create spinner-like progress
if not self.pbar:
self.pbar = tqdm(unit="B", unit_scale=True, desc=d.get("filename") or "download")
self.pbar.update(downloaded - self.pbar.n)
elif d.get("status") in ("finished", "error"):
if self.pbar:
self.pbar.close()
self.pbar = None
# --- Main ---
def download_playlist(playlist_url):
ensure_output_dir()
progress = TqdmHook()
ydl_opts =
"format": VIDEO_FORMAT,
"outtmpl": str(OUTPUT_DIR / "%(playlist_index)02d - %(title)s.%(ext)s"),
"noplaylist": False,
"ignoreerrors": True,
"progress_hooks": [progress.progress_hook],
"quiet": True,
"no_warnings": True,
# "concurrent_fragment_downloads": 4, # optional, for certain extractors
with YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(playlist_url, download=False)
except Exception as e:
print(f"Error extracting playlist info: e", file=sys.stderr)
return
entries = info.get("entries") or [info]
total_videos = sum(1 for e in entries if e) # some entries may be None if ignored
print(f"Found total_videos videos in playlist. Starting downloads...")
# download videos one by one to keep progress bars neat
for entry in entries:
if not entry:
continue
# build single video URL or id
video_url = entry.get("webpage_url") or entry.get("id")
try:
ydl.download([video_url])
except Exception as e:
print(f"Failed to download video_url: e", file=sys.stderr)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python playlist_downloader.py PLAYLIST_URL")
sys.exit(1)
playlist_link = sys.argv[1]
download_playlist(playlist_link)
Downloading videos from YouTube can be useful for offline study, archiving lectures, or creating an offline playlist when you have limited connectivity. This tutorial shows how to build a simple, free YouTube playlist downloader in Python using open-source tools. It covers installation, a clear script, options for quality and output, and tips for legal/ethical use. The script (playlist_downloader
Note: Only download videos you have the right to save (your own content, Creative Commons videos, or where you have permission). Respect YouTube’s Terms of Service and copyright law.