Skip to main content

Command Palette

Search for a command to run...

YouTube Video Downloader using Python

Published
3 min read
YouTube Video Downloader using Python
A

Software Engineer | Technical Blogger | YouTuber | Exploring and Refactoring! | connect with me on Twitter

In this Blog article, we will see how to download YouTube video. We will see the implementation in python.

Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!

You can refer to my YouTube video tutorial for better Understanding

Introduction

YouTube is a very popular video-sharing platform. Downloading a video from YouTube can be a tough job. Python can make the job easier for you.

What will be covered in this Blog

1. Pytube Introduction
2. How to fetch the Video Title
3. How to fetch the Thumbnail Image
4. Download a YouTube Video

What is Pytube?

pytube is a lightweight, Pythonic, dependency-free, library (and command-line utility) for downloading YouTube Videos.

Features

  • Support for Both Progressive & DASH Streams
  • Easily Register on_download_progress & on_download_complete callbacks
  • Command-line Interfaced Included
  • Caption Track Support
  • Outputs Caption Tracks to .srt format (SubRip Subtitle)
  • Ability to Capture Thumbnail URL.
  • Extensively Documented Source Code
  • No Third-Party Dependencies

If you wish to know more about it, you can refer to Pytube Documentation. Use this link to navigate to the documentation.

Now that you are aware of pytube basics, we can move forward to the coding section.

Time to code!

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

carbon (5).png

Installing Pytube

Open your terminal and run the following command

pip install pytube3

I would recommend you to use pytube3 library. I am using the same in this tutorial. Now that we have the package, we are ready to import it in our python script.

Fetching the Title

The first step is to import the YouTube class from the pytube module.

from pytube import YouTube
url = 'Your URL goes here'
my_video = YouTube(url)

Now get the URL, I am storing it in url. Next, I am calling the YouTube method on my_video and passing out URL.

print(my_video.title)

Now let's fetch the title of the YouTube Video Using the title method. Next, let's fetch the thumbnail image.

Fetching the Thumbnail Image

In order to fetch the Thumbnail Image of YouTube Video, we will use the thumbnail_url method.

print(my_video.thumbnail_url)

Print method will display the Thumbnail Image of the YouTube Video.

Downloading the YouTube Video

In order to download the YouTube video, we need to set the stream resolution first.

my_video = my_video.streams.get_highest_resolution()
or
my_video = my_video.streams.first()

You can choose the first stream resolution or you can go for one with the stream resolution.

If you wish to get all the stream resolution for the video and then choose the appropriate one then you can use the following command.

for stream in my_video.streams:
    print(stream)

You can use the filter() function to extract only specific streams. This is useful when you want to download all the different resolutions of the YouTube video.

Moving forward, now let's download the video! We will use the download method on the specific stream to download the YouTube video.

my_video.download()

Your YouTube Video will be downloaded in the same folder where your python script resides.

That's it! simple, isn't it?

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

Thank you for reading, I would love to connect with you at Twitter.

Do share your valuable feedback and suggestions!

You should definitely check out my other Blogs:

Resources:

  • https://python-pytube.readthedocs.io/en/latest/

See you in my next Blog article, Take care

D

Youtube is one of the most popular video platforms, and if you want to have more time to watch it, you could do something that I want to share with you. So recently I decided to buy youtube view time just because I would like to watch youtube network without restrictions. Then I have a lot of time to watch my favorite blogger on this video platform. I assume that you can also use this site.

M

Interesting post! 24/7 availability. Once you order an essay from https://expertpaperwriter.com/myperfectwords-review/ , we ensure that constant communication is maintained to keep you informed regarding the progress of your order. To make this possible, our support team is available round-the-clock for calls and emails from our customers. This makes it possible for you to contact us anytime and get a prompt reply concerning our services.

D

Hi Ayushi, Thanks for sharing this . Really great to know !!

2
A

Thank you!

1

More from this blog

A

Ayushi Rawat - The official blog of Ayushi Rawat

72 posts

Software Engineer | Technical Blogger | YouTuber | Exploring and Refactoring! | Connect with me on Twitter