How to download youtube videos using python
Requirements
·
pip install pytube3
pytube is a lightweight, Pythonic, dependency-free, library (and command-line
utility) for downloading YouTube Videos.
Pytube provide us the facility to download the youtube videos effortlessly.
Steps
1⇒firstly we have to import the Youtube class from the pytube module.
from pytube import YouTube
2⇒we will give the link or url of the video which we want to download.
url="https://www.youtube.com/watch?v=Y9VgmhxtJFk"
3⇒Now we will create Youtube class object as youtube
youtube = YouTube(url)
4⇒ To print the title of the video
print(youtube.title)
⇒ To see the thumbnail of the video.
print(youtube.thumbnail_url)
5⇒To see all the media format available for the video.
print(youtube.streams.all())
6⇒If you want to choose the first stream to download.
stream=youtube.streams.first()
⇒you can also choose the video by its itag number
stream=youtube.streams.get_by_itag('18')
7⇒To download the video
stream.download()
Your video will get downloaded.
code
from pytube import YouTube
url="https://www.youtube.com/watch?v=Y9VgmhxtJFk"
youtube = YouTube(url)
print(youtube.title)
print(youtube.thumbnail_url)
a=youtube.streams.all()
for i in a:
print(i)
#if you want both sound and video choose the one which have both audio video codec
stream = youtube.streams.get_by_itag('18')
print(stream)
#video will be downloaded to the current folder
stream.download()
#to download in a specific folder stream.download("/path")
Note=If you want both audio and video choose the format which have both audio and
video codec available
example--
<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">
Comments
Post a Comment