Python YouTube Downloader Script
This website contains lots of Python developer resources.There are many Python source code available that you can directly implement in your website applications. In this article, you will learn about a YouTube Python downloader script that helps you to download YouTube videos.
YouTube is the most famous video sharing software on the world. It encourages clients to upload, view, and share videos. Huge amounts of individuals chose YouTube video making as full work, as they make and upload recordings and getting income. The recordings are viewed on the web and YouTube didn't give any download choice to offline users. On the off chance that you need to get to the document of the YouTube video, the video should be downloaded. Here, we have mentioned the step by step process to create YouTube downloader script using Python.
Python provides pytube module that helps in downloading YouTube videos. So, open your terminal window and run the following command to install this module using pip tool -
pip install pytube
If the above command is not working or return error on execution, uninstall it and run the following command -
pip install pytube3

Once the pytube module is installed, create a python file and import the module at the top of the script -
from pytube import YouTube
Next, for accessing the features of this module, we need to initialize the class and load the URL as an argument in the YouTube() function -
pytube.YouTube(url)
Pytube Simple Example
Here, we have taken a very simple example to download a YouTube video. The download() function accepts different parameters to set the video file location and name -
import pytube
url = 'https://www.youtube.com/watch?v=acvdFEOy-EY'
youtube = pytube.YouTube(url)
video = youtube.streams.first()
video.download('/video', 'phpinterview')
Pytube get video information
This library provides attributes to get video information, like we can get video id, video title, and so on -
import pytube
url = 'https://www.youtube.com/watch?v=acvdFEOy-EY'
youtube = pytube.YouTube(url)
print(youtube.title) # Title
print(youtube.video_id) # Id
print(youtube.age_restricted) # Age
The above code returns the following output -

Pytube Streams Format
The YouTube object opens different streams from the YouTube video URL. The given example returns all the stream information of the video -
import pytube
url = 'https://www.youtube.com/watch?v=EY1SKJQ2rTU&t=8s'
youtube = pytube.YouTube(url)
stream = youtube.streams.all()
for i in stream:
print(i)
It returns the following output -
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">
<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">
<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.64001e" progressive="False" type="video">
<Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9" progressive="False" type="video">
<Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d4016" progressive="False" type="video">
<Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9" progressive="False" type="video">
<Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d4014" progressive="False" type="video">
<Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9" progressive="False" type="video">
<Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9" progressive="False" type="video">
<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2" progressive="False" type="audio">
<Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus" progressive="False" type="audio">
The "progressive" stream contains the file having both audio and video and the "adaptive" steam contains either audio or video. The "mime_type", "res", and "fps" attributes can be used to filter the stream of the downloaded video.
Pytube Filter Streams
We can use the filter() method to filter only specific streams. It is helpful when we want to download all the different resolutions of YouTube videos-
import pytube
url = 'https://www.youtube.com/watch?v=EY1SKJQ2rTU&t=8s'
youtube = pytube.YouTube(url)
stream = youtube.streams.filter(progressive=True, file_extension='mp4')
for i in stream:
print(i)
It returns the following -
(env) c:\python37\Scripts\projects>youtube.py
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">
<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">
Pytube download high resolution video
Pytube provides get_highest_resolution() method to download high resolution video. In this given example, we have also used exception handling -
import pytube
url = 'https://www.youtube.com/watch?v=EY1SKJQ2rTU&t=8s'
try:
youtube = pytube.YouTube(url)
filters = youtube.streams.filter(progressive=True, file_extension='mp4')
# high resolution video
filters.get_highest_resolution().download()
print('Successfully downloaded')
except Exception as e:
print(e)
Related Articles
Python Spell Checker ProgramPython remove punctuation from string
How to convert Excel to CSV Python Pandas
How to read data from excel file using Python Pandas
How to read data from excel file in Python
Python read JSON from URL requests
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml