Skip to content

YouTube Video Downloader

Abstract

YouTube Video Downloader is a Python project that downloads YouTube videos. The application features video extraction, format selection, and a CLI interface, demonstrating best practices in automation and media processing.

Prerequisites

  • Python 3.8 or above
  • A code editor or IDE
  • Basic understanding of web scraping and media processing
  • Required libraries: pytubepytube

Before you Start

Install Python and the required libraries:

Install dependencies
pip install pytube
Install dependencies
pip install pytube

Getting Started

Create a Project

  1. Create a folder named youtube-video-downloaderyoutube-video-downloader.
  2. Open the folder in your code editor or IDE.
  3. Create a file named youtube_video_downloader.pyyoutube_video_downloader.py.
  4. Copy the code below into your file.

Write the Code

⚙️ YouTube Video Downloader
YouTube Video Downloader
from pytube import YouTube
 
class YouTubeVideoDownloader:
    def __init__(self):
        pass
 
    def download(self, url):
        yt = YouTube(url)
        stream = yt.streams.get_highest_resolution()
        print(f"Downloading: {yt.title}")
        # stream.download()  # Uncomment to actually download
        print("Download complete.")
 
    def demo(self):
        self.download('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
 
if __name__ == "__main__":
    print("YouTube Video Downloader Demo")
    downloader = YouTubeVideoDownloader()
    # downloader.demo()  # Uncomment to run
 
YouTube Video Downloader
from pytube import YouTube
 
class YouTubeVideoDownloader:
    def __init__(self):
        pass
 
    def download(self, url):
        yt = YouTube(url)
        stream = yt.streams.get_highest_resolution()
        print(f"Downloading: {yt.title}")
        # stream.download()  # Uncomment to actually download
        print("Download complete.")
 
    def demo(self):
        self.download('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
 
if __name__ == "__main__":
    print("YouTube Video Downloader Demo")
    downloader = YouTubeVideoDownloader()
    # downloader.demo()  # Uncomment to run
 

Example Usage

Run video downloader
python youtube_video_downloader.py
Run video downloader
python youtube_video_downloader.py

Explanation

Key Features

  • Video Extraction: Downloads videos from YouTube.
  • Format Selection: Allows selection of video format.
  • Error Handling: Validates inputs and manages exceptions.
  • CLI Interface: Interactive command-line usage.

Code Breakdown

  1. Import Libraries and Setup Downloader
youtube_video_downloader.py
from pytube import YouTube
youtube_video_downloader.py
from pytube import YouTube
  1. Video Extraction and Format Selection Functions
youtube_video_downloader.py
def download_video(url, resolution='720p'):
    yt = YouTube(url)
    stream = yt.streams.filter(res=resolution, file_extension='mp4').first()
    if stream:
        stream.download()
        return f"Downloaded {yt.title}"
    return "Resolution not available."
youtube_video_downloader.py
def download_video(url, resolution='720p'):
    yt = YouTube(url)
    stream = yt.streams.filter(res=resolution, file_extension='mp4').first()
    if stream:
        stream.download()
        return f"Downloaded {yt.title}"
    return "Resolution not available."
  1. CLI Interface and Error Handling
youtube_video_downloader.py
def main():
    print("YouTube Video Downloader")
    while True:
        cmd = input('> ')
        if cmd == 'download':
            url = input("YouTube URL: ")
            resolution = input("Resolution (e.g., 720p): ")
            print(download_video(url, resolution))
        elif cmd == 'exit':
            break
        else:
            print("Unknown command. Type 'download' or 'exit'.")
 
if __name__ == "__main__":
    main()
youtube_video_downloader.py
def main():
    print("YouTube Video Downloader")
    while True:
        cmd = input('> ')
        if cmd == 'download':
            url = input("YouTube URL: ")
            resolution = input("Resolution (e.g., 720p): ")
            print(download_video(url, resolution))
        elif cmd == 'exit':
            break
        else:
            print("Unknown command. Type 'download' or 'exit'.")
 
if __name__ == "__main__":
    main()

Features

  • Video Downloading: Extraction and format selection
  • Modular Design: Separate functions for each task
  • Error Handling: Manages invalid inputs and exceptions
  • Production-Ready: Scalable and maintainable code

Next Steps

Enhance the project by:

  • Integrating with advanced video processing libraries
  • Supporting playlist downloads
  • Creating a GUI for downloading
  • Adding real-time progress
  • Unit testing for reliability

Educational Value

This project teaches:

  • Media Processing: Video downloading and format selection
  • Software Design: Modular, maintainable code
  • Error Handling: Writing robust Python code

Real-World Applications

  • Media Platforms
  • Automation Tools
  • AI Apps

Conclusion

YouTube Video Downloader demonstrates how to build a scalable and accurate video downloading tool using Python. With modular design and extensibility, this project can be adapted for real-world applications in media, automation, and more. For more advanced projects, visit Python Central Hub.

Was this page helpful?

Let us know how we did