π₯ I Built a Professional YouTube Downloader with Python
Ever wanted to download YouTube videos or extract audio for offline listening? I created a comprehensive, production-ready YouTube downloader that handles everything from single videos to entire playlists!
π What Makes This Special?
Unlike basic downloaders, this tool is built for real-world use with features you actually need:
- π¬ Single Video Downloads - Quick and easy
- π Playlist Support - Download entire playlists with organized structure
- π Bulk Downloads - Process multiple URLs from a text file
- π΅ Multiple Formats - MP4, MP3, M4A with quality control
- π― Quality Options - From 144p to 4K for videos, 128k to 320k for audio
- π₯οΈ Cross-Platform - Works on Windows, macOS, and Linux
- π¨ Interactive CLI - User-friendly interface with colored output
π Quick Start
# Clone the repository
git clone https://github.com/dusmamud/youtube-downloader.git
cd youtube-downloader
# Install dependencies
pip install -r requirements.txt
# Download a video
python youtube_downloader.py "https://www.youtube.com/watch?v=VIDEO_ID"
That's it! You're ready to go.
π‘ Real-World Usage Examples
Download Audio as MP3
Perfect for music or podcasts:
python youtube_downloader.py "VIDEO_URL" -f audio -o mp3 -q 320k
Download Entire Playlist
Great for educational content:
python youtube_downloader.py "PLAYLIST_URL" -p
Bulk Download from File
Create a urls.txt file:
https://www.youtube.com/watch?v=VIDEO_ID_1
https://www.youtube.com/watch?v=VIDEO_ID_2
https://www.youtube.com/playlist?list=PLAYLIST_ID
Then run:
python youtube_downloader.py -b urls.txt -q 720p
Get Video Info Without Downloading
python youtube_downloader.py "VIDEO_URL" -i
Output:
πΉ Video Information:
Title: Amazing Video Title
Duration: 03:33
Uploader: Channel Name
Views: 1.7B
Upload Date: 20091025
ποΈ Architecture & Design
Project Structure
youtube-downloader/
βββ youtube_downloader.py # Main CLI interface
βββ src/
β βββ downloader.py # Core downloader class
β βββ utils.py # Utility functions
βββ tests/ # Unit tests
βββ docs/ # Comprehensive documentation
βββ requirements.txt # Dependencies
Key Technologies
yt-dlp: The powerhouse behind the downloads. It's a fork of youtube-dl with better performance and more features.
colorama: Cross-platform colored terminal output for better UX.
FFmpeg: For audio conversion (MP3/M4A).
Core Features Implementation
1. Modular Design
class YouTubeDownloader:
def __init__(self, output_dir: str = "downloads"):
self.output_dir = Path(output_dir)
self.video_qualities = {...}
self.audio_qualities = {...}
def download_single(self, url, format_type, quality, output_format):
# Single video download logic
def download_playlist(self, playlist_url, ...):
# Playlist download logic
def bulk_download(self, urls, ...):
# Bulk download logic
2. Quality Management
video_qualities = {
'144p': 'worst[height<=144]',
'720p': 'best[height<=720]',
'1080p': 'best[height<=1080]',
'4K': 'best[height<=2160]',
'best': 'best'
}
audio_qualities = {
'128k': 'bestaudio[abr<=128]',
'320k': 'bestaudio[abr<=320]',
'best': 'bestaudio/best'
}
3. Error Handling
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
print(f"{Fore.GREEN}β Successfully downloaded!")
return True
except Exception as e:
print(f"{Fore.RED}β Error: {str(e)}")
return False
π¨ User Experience Design
Interactive Mode
Run without arguments for a guided experience:
python youtube_downloader.py
The tool presents a friendly menu:
π― Interactive Mode
Enter YouTube URL (or 'quit' to exit):
URL: https://youtube.com/watch?v=...
π Select download type:
1. π₯ Single video/audio
2. π Entire playlist
3. βΉοΈ Get video info only
4. π Enter new URL
Choice (1-4):
Colored Output
Using colorama for better visual feedback:
- π’ Green for success
- π΄ Red for errors
- π‘ Yellow for warnings
- π΅ Cyan for information
π Testing & Quality Assurance
Automated Testing
# Run comprehensive test suite
python test_project.py
# Run unit tests
python -m pytest tests/ -v
Test Coverage
- β Dependencies check
- β File structure validation
- β Module imports
- β Basic functionality
- β Unit tests (9/9 passed)
Overall Score: 82.6% PASS π
π§ Advanced Features
Custom Output Directory
python youtube_downloader.py "VIDEO_URL" -d "~/MyVideos"
Format Conversion
Automatic conversion to MP3 or M4A:
python youtube_downloader.py "VIDEO_URL" -f audio -o mp3
Playlist Organization
Playlists are automatically organized:
downloads/
βββ playlist_downloads/
βββ 01 - First Video.mp4
βββ 02 - Second Video.mp4
βββ 03 - Third Video.mp4
π Documentation
The project includes comprehensive documentation:
- Installation Guide - Platform-specific setup instructions
- Usage Guide - Detailed examples and use cases
- Troubleshooting Guide - Common issues and solutions
- Contributing Guide - How to contribute to the project
- Test Guide - How to test the project
π€ Contributing
Contributions are welcome! The project follows standard open-source practices:
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests
- Submit a pull request
π― Use Cases
For Students
- Download educational videos for offline study
- Save lecture series and tutorials
- Extract audio from language learning videos
For Content Creators
- Backup your own content
- Download reference materials
- Archive important videos
For Music Lovers
- Convert music videos to MP3
- Download entire playlists
- Create offline music libraries
For Researchers
- Archive video content for analysis
- Download documentary series
- Save interview recordings
βοΈ Legal & Ethical Considerations
Important: This tool is for educational and personal use only. Please:
- Respect YouTube's Terms of Service
- Honor copyright laws
- Only download content you have permission to download
- Support content creators through official channels
π Future Enhancements
Planned features for upcoming releases:
- π Download progress bars with ETA
- π Resume interrupted downloads
- π± Mobile-optimized quality presets
- ποΈ Advanced filtering options
- π Authentication for private videos
- π Proxy support
- π¦ GUI version
- π Plugin system
π Performance
The tool is optimized for:
- Speed: Parallel processing for bulk downloads
- Reliability: Comprehensive error handling
- Efficiency: Minimal memory footprint
- Compatibility: Works with Python 3.7+
π What I Learned
Building this project taught me:
- API Integration: Working with yt-dlp's extensive API
- CLI Design: Creating intuitive command-line interfaces
- Error Handling: Graceful failure and user feedback
- Testing: Writing comprehensive test suites
- Documentation: Creating user-friendly docs
- Cross-Platform Development: Ensuring compatibility
π Links
- GitHub Repository: dusmamud/youtube-downloader
- Documentation: Full Docs
- Issues: Report Bugs
π¬ Feedback
I'd love to hear your thoughts! Have you built something similar? What features would you add? Drop a comment below! π
If you found this useful, please β star the repository on GitHub and share it with others who might benefit from it!
Happy Downloading! π
Top comments (4)
Very nicely formatted and presented article. I've been checking out the actual application, and I had some questions as to why anyone would need something beyond
yt-dlp, which I've been using for quite some time now, and I must say, you've done a fine job with this product.I've also scoured over your Git repo's docs and again, very sensible and comprehensive layout - if most other developers put such effort into their documentation they wouldn't have an endless flow of open issues that consist of users simply asking basic questions.
For most n00bs, the interactive mode is prolly the way they're gonna go... initially, and then they'll start venturing out into more precision, since they're already on the CLI.
The product works as advertised and quickly too, I like snappy. Everyone does. Let's dive into your roadmap though, shall we?
π Download progress bars with ETA
Yup - I'm wondering why it wasn't already implemented, and early on in the project?
π Resume interrupted downloads
This has always been a popular item with any software for some reason, but years of experiencing various forms and levels of success/failure/frustration and wasted time trying (i.e., some FTP servers historically support resume and others don't - no mention of which ones do however), have let me to a point (with the exception of torrents) where the times I try to resume anything that broke is far and few between - again, it's exceedingly rare that I'll try to resume a failed download for any reason. I find it's faster just to start over and get it done.
That's just me though. Some people will try four and five times because they should be able to resume - in the meantime, I've completed my d/l and moved on to the next task at hand.
π± Mobile-optimized quality presets
WhatEv. people should use Termux or NixOnDroid and just run this from the shell on their Android IMNSHO. Alternatively, you might wanna try your hand at pushing out an app based on this product to F-Droid - I wouldn't even bother with the Google Play Store.
Further, if you do produce an Android App port of your product, I would highly encourage you to do so via your Git repo using a "Custom F-Droid Repo" - that way, people have three choices:
FairEmail, my favorite MUA, and IMO way better than K9 Mail, is setup to do automatic updates directly from Github if you install it from there; you're prompted to download the release package APK and when that finishes you have to press to install it. With an F-Droid custom repo or regular F-Droid repo that will/can happen automatically for stable releases. I encourage you to look and see how he does that for his users :)
There's also Obtanium which is quite popular too.
ποΈ Advanced filtering options
As long as you don't complicate what you already have, and leave this for the truly demanding powerusers - again, don't complicate things for the n00bs ;)
π Authentication for private videos
I used to use a sandboxed freeware youtube downloader on Windows (back when I used to use Wndows), and they actually had this option where you could set up logins and it worked great - until it didn't, and then you got another update and it worked great, until it didn't, Etc.
But you already know that going down that road means that this particular aspect of usage for your product is going to be playing whack-a-mole with YT. The nice thing however, is that this will have no impact on people who aren't actually using it for d/l'ing this particular class of videos anyway - So Go for it.
π Proxy support
This neither means here nor there to me. I use WireGuard in different geographical places for different things, but if you think it's worth the trouble for people who won't take the initiative to set up their own tunnels in the first place, and with respect to Android, you should be aware that things like the DuckDuckGo browser have a localhost loop proxy that may interfere with such things (it can be disabled per app, and generally is for YouTube and a few other things anyway) to thwart trackers.
If you do this, I'd leave it to last - after you build a GUI - anyone working on the CLI already knows how to do these things for themselves, IMO.
π¦ GUI version
Oh, yeah baby! Did I mention that at first, I didn't see why I should be using what you've produced from yt-dlp until I played with your program? Well, this would have removed that question in the first place - a Good GUI (option) is usually a really good thing, and I urge you to make this your next push here - people will say really good things about you if you do ;)
π Plugin system
The repo size appears to be about 70k as it is. This is a design choice for you to make, but it may be a lot of work for something that overly complicates the ability for people to use features that might just be better off in core - but you decide.
Summary
I think you've done a very fine job, and I also thought that someone should take the time to spend a little more time than just to leave a one-liner to say so for what you released just two days ago.
I hope to see more from you in the future, and remember how impressed I was (and others will be) for not cutting corners on the documentation, giving it the due attention that it deserves.
Best of luck and I hope that helps!
P.S. If you do go ahead with the GUI I'll be happy to create a SlackBuild and be the package maintainer of your d/l'er for Slackware Linux :)
Nice article, I would definitely try this out in my spare time! Thanks
Nicely written here as well as in your repo.
Really Helpful article, thanxx for this π