Python YouTube Downloader Script
This website contains lots of Python development resources. There is a lot of 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 download YouTube videos.
YouTube is the most popular video-sharing software in the world. It encourages clients to upload, view, and share videos. Huge numbers of individuals choose YouTube video making as a full-time job as they make and upload recordings and get 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 from the YouTube video, the video should be downloaded. Here, we have mentioned the step-by-step process of creating a YouTube downloader script using Python.
Install PyTube module
Python provides a pytube module that helps in downloading YouTube videos. So, open your terminal window and run the following command to install this module using the pip tool.
pip install pytube
If the above command is not working or returns an 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, to access the features of this module, we need to initialise the class and load the URL as an argument in the YouTube() function.
pytube.YouTube(url)
Python youtube downloader
Here is a very simple example to download a YouTube video. First, we import the pytube library and the object of the YouTube module by passing the link as the parameter. 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')
A file downloading takes some time as a very large amount of data is being downloaded from the web.
Pytube get video information
The pytube library provides attributes to get video information, such as a video id, a 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 Python program 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" stream 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)
Output of the above code:
(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 the get_highest_resolution() method to download high-resolution videos. 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)
Pytube download multiple videos
In the given Python program, we have downloaded multiple videos using the pytube module.
from pytube import YouTube
SAVE_PATH = "C:/"
#link of the video to be downloaded
download_link=["https://www.youtube.com/watch?v=acvdFEOy-EY",
"https://www.youtube.com/watch?v=Io_HjpygtH0"
]
for i in download_link:
try:
yt = YouTube(i)
except:
print("Connection Error")
#filter files with "mp4" extension
mp4files = yt.filter('mp4')
# passed extension and resolution in the get() function
download_videos = yt.get(mp4files[-1].extension,mp4files[-1].resolution)
try:
download_videos.download(SAVE_PATH)
except:
print("Error in downloading!")
print('Video downloaded!')
Related Articles
Detect Specific Color From Image using Python OpenCVPython program to print all even numbers between 1 to 100
Python OpenCV Histogram of Color Image
Remove last element from list Python
Python Tkinter Combobox Event Binding
Capture a video in Python OpenCV and save
Python Spell Checker Program
Python 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