Build a YouTube Video Downloader with Python in 5 min (Playlist Support)

Downloading YouTube videos for offline viewing or archiving can be done quickly with Python. Using the pytube library, you can create your own YouTube downloader that works for both single videos and entire playlists.

This tutorial will show you how to:

  • Download videos in high quality

  • Extract only audio if needed

  • Download entire playlists

  • Add a progress indicator


Step 1: Install Dependencies

bashCopyEditpip install pytube

Step 2: Download a Single Video

pythonCopyEditfrom pytube import YouTube

def download_video(url, path="."):
    yt = YouTube(url)
    stream = yt.streams.get_highest_resolution()
    stream.download(output_path=path)
    print(f"Downloaded: {yt.title}")

if __name__ == "__main__":
    video_url = input("Enter YouTube video URL: ")
    download_video(video_url, path="downloads")

Step 3: Download a Playlist

pythonCopyEditfrom pytube import Playlist

def download_playlist(url, path="."):
    playlist = Playlist(url)
    for video in playlist.videos:
        video.streams.get_highest_resolution().download(output_path=path)
        print(f"Downloaded: {video.title}")

if __name__ == "__main__":
    playlist_url = input("Enter playlist URL: ")
    download_playlist(playlist_url, path="downloads")

Step 4: Extract Only Audio

pythonCopyEditdef download_audio(url, path="."):
    yt = YouTube(url)
    audio_stream = yt.streams.filter(only_audio=True).first()
    audio_stream.download(output_path=path)
    print(f"Audio saved: {yt.title}")

Step 5: Tips for a Better Experience

  • Always check YouTube’s terms of service before downloading content.

  • Use progressive=True for videos with both audio and video in one file.

  • You can rename files automatically after download.

  • Combine with tkinter for a simple GUI-based downloader.


Example Usage

bashCopyEditpython yt_downloader.py
Enter YouTube video URL: https://www.youtube.com/watch?v=abc123
Downloaded: Sample Video Title

Conclusion

In less than 30 lines of Python code, you now have a personal YouTube downloader that can grab videos, audio, or entire playlists. With a bit of UI work, you could turn this into a desktop app for everyday use.

0
Subscribe to my newsletter

Read articles from Ashraful Islam Leon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashraful Islam Leon
Ashraful Islam Leon

Passionate Software Developer | Crafting clean code and elegant solutions