Youtube Playlist - Downloader Python Script

def download_playlist(self, start_from: int = 1, max_videos: Optional[int] = None): """ Download entire playlist Args: start_from: Start downloading from this video index (1-based) max_videos: Maximum number of videos to download """ # Get playlist info info = self.get_playlist_info() self.stats["total"] = info["total_videos"] # Determine which videos to download video_urls = self.playlist.video_urls if start_from > 1: video_urls = video_urls[start_from - 1:] if max_videos: video_urls = video_urls[:max_videos] total_to_download = len(video_urls) print(f"Fore.CYAN🎬 Starting download of total_to_download videos...") # Download each video with progress bar with tqdm(total=total_to_download, desc="Overall Progress", unit="video") as pbar: for idx, video_url in enumerate(video_urls, start=start_from): success = self.download_video(video_url, idx, info["total_videos"]) if success: self.stats["successful"] += 1 else: self.stats["failed"] += 1 pbar.update(1) # Print summary self.print_summary()

def get_playlist_info(self) -> Dict: """Get playlist information""" try: self.playlist = Playlist(self.playlist_url) # Force refresh playlist to get video URLs self.playlist._video_regex = None self.playlist.parse_links() info = "title": self.playlist.title, "total_videos": len(self.playlist.video_urls), "videos": [] print(f"\nFore.CYAN📋 Playlist: self.playlist.title") print(f"Fore.CYAN📊 Total videos: len(self.playlist.video_urls)") return info except Exception as e: print(f"Fore.RED❌ Error accessing playlist: e") raise

class YouTubePlaylistDownloader: """Main class for downloading YouTube playlists"""

def download_video(self, video_url: str, index: int, total: int) -> bool: """ Download a single video Returns: bool: True if successful, False otherwise """ try: # Create YouTube object video = YouTube(video_url, on_progress_callback=self.on_progress) # Get video title title = self.sanitize_filename(video.title) print(f"\nFore.YELLOW[index/total] Fore.WHITEDownloading: title") # Check if file already exists if self.download_audio_only: output_path = self.output_dir / f"title.mp3" else: output_path = self.output_dir / f"title.mp4" if output_path.exists(): print(f"Fore.BLUE⏭️ File already exists, skipping...") self.stats["skipped"] += 1 return True # Get appropriate stream stream = self.get_video_stream(video) if not stream: print(f"Fore.RED❌ No suitable stream found") return False # Download if self.download_audio_only: # Download audio and convert to MP3 out_file = stream.download(output_path=self.output_dir) base, ext = os.path.splitext(out_file) new_file = base + '.mp3' os.rename(out_file, new_file) print(f"Fore.GREEN✅ Downloaded audio: title.mp3") else: stream.download(output_path=self.output_dir, filename=f"title.mp4") print(f"Fore.GREEN✅ Downloaded video: title.mp4") return True except VideoUnavailable: print(f"Fore.RED❌ Video unavailable: video_url") self.stats["failed_videos"].append("url": video_url, "error": "Video unavailable") return False except PytubeError as e: print(f"Fore.RED❌ Pytube error: e") self.stats["failed_videos"].append("url": video_url, "error": str(e)) return False except Exception as e: print(f"Fore.RED❌ Unexpected error: e") self.stats["failed_videos"].append("url": video_url, "error": str(e)) return False youtube playlist downloader python script

args = parser.parse_args()

try: # Create downloader instance downloader = YouTubePlaylistDownloader( playlist_url=args.playlist_url, output_dir=args.output, quality=args.quality, max_resolution=args.max_res, download_audio_only=args.audio_only ) # Start download downloader.download_playlist( start_from=args.start, max_videos=args.max_videos ) except KeyboardInterrupt: print(f"\nFore.YELLOW⚠️ Download interrupted by user") except Exception as e: print(f"Fore.RED❌ Fatal error: e") return 1

try: from pytube import Playlist, YouTube from pytube.exceptions import PytubeError, VideoUnavailable from tqdm import tqdm from colorama import init, Fore, Style init(autoreset=True) except ImportError as e: print("Missing required libraries. Install with: pip install pytube tqdm colorama") raise e start_from: int = 1

parser.add_argument("playlist_url", help="YouTube playlist URL") parser.add_argument("-o", "--output", default="downloads", help="Output directory (default: downloads)") parser.add_argument("-q", "--quality", choices=["highest", "lowest"], default="highest", help="Video quality (default: highest)") parser.add_argument("--max-res", default="1080p", help="Maximum resolution (e.g., 720p, 1080p) (default: 1080p)") parser.add_argument("--audio-only", action="store_true", help="Download only audio as MP3") parser.add_argument("--start", type=int, default=1, help="Start downloading from this video index (1-based)") parser.add_argument("--max-videos", type=int, default=None, help="Maximum number of videos to download")

def print_summary(self): """Print download summary""" print(f"\n'='*60") print(f"Fore.CYAN📊 DOWNLOAD SUMMARY") print(f"'='*60") print(f"Fore.GREEN✅ Successful: self.stats['successful']") print(f"Fore.YELLOW⏭️ Skipped: self.stats['skipped']") print(f"Fore.RED❌ Failed: self.stats['failed']") print(f"Fore.CYAN📁 Output directory: self.output_dir.absolute()") if self.stats["failed_videos"]: print(f"\nFore.REDFailed videos:") for failed in self.stats["failed_videos"]: print(f" - failed['url'] (failed['error'])") # Save failed URLs to file for retry if self.stats["failed_videos"]: failed_log = self.output_dir / "failed_downloads.txt" with open(failed_log, 'w') as f: for failed in self.stats["failed_videos"]: f.write(f"failed['url']\n") print(f"Fore.YELLOW💾 Failed URLs saved to: failed_log") def main(): """Main function with command-line interface""" parser = argparse.ArgumentParser( description="Download YouTube playlists with quality selection", formatter_class=argparse.RawDescriptionHelpFormatter, epilog=""" Examples: python playlist_downloader.py "https://youtube.com/playlist?list=..." Download only audio (MP3) python playlist_downloader.py "PLAYLIST_URL" --audio-only Download in 720p maximum resolution python playlist_downloader.py "PLAYLIST_URL" --max-res 720p Start from video 5, download only 10 videos python playlist_downloader.py "PLAYLIST_URL" --start 5 --max-videos 10 Download to custom directory python playlist_downloader.py "PLAYLIST_URL" --output "~/Music/Playlists" """ )

def on_progress(self, stream, chunk, bytes_remaining): """Progress callback for downloads""" total_size = stream.filesize bytes_downloaded = total_size - bytes_remaining percentage = (bytes_downloaded / total_size) * 100 # This will be handled by tqdm in the main download loop pass unit="video") as pbar: for idx

def get_video_stream(self, video: YouTube): """Get appropriate video stream based on quality settings""" try: if self.download_audio_only: # Get audio stream (highest bitrate) return video.streams.filter(only_audio=True).first() # Get video streams if self.quality == "lowest": stream = video.streams.get_lowest_resolution() else: # highest # Filter by max resolution if specified streams = video.streams.filter(progressive=True, file_extension='mp4') if self.max_resolution != "highest": # Convert '1080p' to '1080' for comparison max_res = int(self.max_resolution.replace('p', '')) streams = streams.filter(res=f"max_resp") if streams: stream = streams.last() # Highest resolution else: # Fallback to non-progressive (video only + audio) stream = video.streams.get_highest_resolution() return stream except Exception as e: print(f"Fore.YELLOW⚠️ Error getting stream: e") return None

return 0 if == " main ": exit(main()) Alternative: Using yt-dlp (Recommended for Production) #!/usr/bin/env python3 """ YouTube Playlist Downloader using yt-dlp More robust and actively maintained """ import subprocess import json import os from pathlib import Path

def sanitize_filename(self, filename: str) -> str: """Remove invalid characters from filename""" # Remove invalid characters for Windows/Linux/Mac invalid_chars = r'[<>:"/\\|?*]' filename = re.sub(invalid_chars, '_', filename) # Limit filename length if len(filename) > 200: filename = filename[:200] return filename.strip()

def __init__(self, playlist_url: str, output_dir: str = "downloads", quality: str = "highest", max_resolution: str = "1080p", download_audio_only: bool = False): """ Initialize the downloader Args: playlist_url: YouTube playlist URL output_dir: Directory to save downloads quality: 'highest', 'lowest', or 'audio' max_resolution: Maximum video resolution (e.g., '720p', '1080p') download_audio_only: If True, download only audio as MP3 """ self.playlist_url = playlist_url self.output_dir = Path(output_dir) self.quality = quality self.max_resolution = max_resolution self.download_audio_only = download_audio_only self.playlist = None self.stats = "total": 0, "successful": 0, "failed": 0, "skipped": 0, "failed_videos": [] # Create output directory if it doesn't exist self.output_dir.mkdir(parents=True, exist_ok=True) # If downloading audio only, create an audio subdirectory if self.download_audio_only: self.output_dir = self.output_dir / "audio" self.output_dir.mkdir(parents=True, exist_ok=True)