Download YouTube Videos and Playlist Using Python — With Example
As you all know, YouTube is a video sharing service where users can watch, like, share, comment and upload their own videos.
You can watch videos with the few clicks, but downloading videos can be difficult. You can download it by using video download manager, available online, but most of them are paid. YouTube added a feature to download a video in its download folder but still you do not have an option to download it on your local device.
In this tutorial, we will download videos using python language. As you may know the versatility of python in terms of its modules and libraries, we will use pytube library to write python code to download videos from the YouTube.
Prerequisites
Below are the basic requirements to proceed with this tutorial:
- Understanding of the Python Programming language
- You must have Python 3+ installed on your computer
- You must have installed the Python library “pytube”
- You should have a Python code editor such as Pycharm, VS Code, and etc.
Pytube Overview and installation
pytube is a lightweight, Pythonic, dependency-free, library (and command-line utility) for downloading YouTube Videos.
To install pytube, run the following command in your terminal:
pip install pytube
Downloading a Video
Downloading a video from YouTube with pytube is easy.
- Begin by importing the YouTube class:
from pytube import YouTube
2. Now, let’s try to download a video. For this example, let’s take any URL of the video. In this example, we take YouTube rewind video for 2015:
yt = YouTube('https://www.youtube.com/watch?v=G-og553jauM')
Now, we have a YouTube object called yt. The pytube API makes all information intuitive to access. Once you have a YouTube object set up, you’re ready to start looking at different media streams for the video.
Before going to the last step, let’s discuss about streams and StreamQuery:
Working with Streams and StreamQuery
Before we can dive in, we need to review a new-ish streaming technique adopted by YouTube. It assumes that you have already created a YouTube object in your code called “yt”.
DASH vs Progressive Streams
yt.streams
[<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.640028
˓→" progressive="False" type="video">,
...
<Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus" progressive=
˓→"False" type="audio">,
<Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus" progressive=
˓→"False" type="audio">]
You may notice that some streams listed have both a video codec and audio codec, while others have just video or just audio, this is a result of YouTube supporting a streaming technique called Dynamic Adaptive Streaming over HTTP (DASH).
In the context of pytube, the implications are for the highest quality streams; you now need to download both the audio and video tracks and then post-process them with software like FFmpeg to merge them.
The legacy streams that contain the audio and video in a single file (referred to as “progressive download”) are still available, but only for resolutions 720p and below.
3. As we have YouTube object set up and have looked at different media streams for the video, we can download and save that using download() method.
stream = yt.streams.get_by_itag(22)
stream.download()
The download method has a number of different useful arguments, which are documented in the API reference: pytube.Stream.download()
Putting things together
Below is the complete python code to download videos from the YouTube.
from pytube import YouTube
def Download(link):
yt = YouTube(link) # To get the Youtube Object
youtubeObject = yt.streams.get_highest_resolution() # Get highest resolution stream that is a progressive video.
try:
youtubeObject.download() # Write the media stream to disk.
except:
print("An error has occurred")
print("Download is completed successfully")
link = input("Enter the URL of the video: ")
Download(link)
The youtubeObject = yt.streams.get_highest_resolution()
command will automatically download the highest resolution available (i.e. 720p in this case).
When you run this program, you will get a prompt to enter URL of the video. After you enter the link, video will start downloading.
Output:
As it can be seen that, video is downloaded in your current directory. You can choose different directory location as well. You can do it by passing arguments to .download() method.
This article is concluded here.
To Download Playlist:
Moreover, if you want to download playlist, it can be done also. Here is code snippet to download playlist:
from pytube import YouTube
from pytube import Playlist
def Download(link):
playlist = Playlist(link)
#print (playlist)
youtubeobj = playlist.videos
#print (youtubeobj)
for links in youtubeobj:
link_obj = links.streams.get_highest_resolution()
try:
link_obj.download()
except:
print("An error has occurred")
print("Download is completed successfully")
link = input("Enter URL of the video: ")
Download(link)