Let's start this article with some facts about YouTube:
The main reason why I am talking about YouTube is that with huge amount of content available on YouTube, most of us might have been using third-party applications to download videos from YouTube to access them offline. And even though we use third-party applications or plug-ins we still end up downloading low quality videos and have to upgrade to so called "Premium Account" to download the videos in high quality.
So why not build your own application to download YouTube videos? How? Using pytube module of python.
Unlike some other famous libraries, pytube doesn't have any third-party dependencies. Pytube is a light-weight python library with a rich set of features for downloading YouTube videos developed by Nick Ficano. This open source project is very easy to use but has a lot of bugs(still a work in progress). Towards the end of this article, I will be providing some code snippets to rectify some of the errors that might occur while you use the library. Hope these errors get fixed by the next release.
Open command prompt(terminal) and type in the following command:
pip install pytube
pytube is not dependent on any other library, so you don't need to install any other libraries. It is available for almost all the latest versions of python (2.7, 3.4 and higher). To start working with pytube, create a python file and type in the following statements,
from pytube import YouTube
The above command means that from the library pytube we are using the "YouTube" module and from now on this
YouTube keyword will be acting as an Object. This object provides many methods and properties like length, title, rating etc. Note that in the word YouTube letters Y and T are in capital, if the case is not given properly eventually we will end up with a lot of errors.
There are in total five types of Objects available in the pytube library, they are:
In this article we will mostly work with the
YouTube object while briefly covering other objects as well.
To fetch information about the video that you want to download using the URL of the video, you need to first, create an instance with the YouTube video's URL on which you want to perform operations. Simply copy the URL of the Youtube video and pass it as an argument to the
Here is the code,
myVideo = YouTube("https://www.youtube.com/watch?v=OTCuykFHBeA")
Now with the
myVideo object created, let us learn how to retrieve some of the basic informations about the video:
Following are the properties(information about the YouTube video) of the
YouTube object which we can access.
NOTE: *Refers that you may get errors while using the property, I will be explaining how to fix those errors in the "Possible Errors and their fixes" section below.
title of the
YouTube object (
myVideo) returns the title of the video. (Please refer to the image above)
Length of a video is the total time in seconds, whenever the property
length is accessed using the
YouTube object it gives the length of the video. The value returned will always be in seconds.
3. Thumbnail Url
Thumbnail is an image(generally picked up from the video itself) which is the representational image or short descriptive image of the video. When this
thumbnail_url property is used on the
YouTube object it returns a URL for the thumbnail picture.
Every video on Youtube has some description which gives information about the video and some hyperlinks to the associated blogs or websites etc. This description can be viewed by using the
description property of the
YouTube object (
The YouTube object (
myVideo) has this property called
views which returns the number of times the video has been viewed. This property might not work correctly because of the changes in the Youtube API recently. So you have to change the code in the pytube library. All the changes that should be made are listed below in a separate section.
rating property will give us the average rating of the YouTube video being accessed. Just like the
rating property might also through some errors.
7. Age Restricted
Some videos on YouTube are age restricted(can be viewed when signed in), to know whether a particular video is age restricted or not we can use the property
age_restricted of the
YouTube object (
myVideo) which returns a
If the value is false then the video is not age restricted and if its true then the video is restricted.
8. Video Id
video_id returns the ID of the video, which is generally present at the end of the Youtube video URLs.
Here are some errors that I encountered while working with the pytube library.
1. The Regex Error
pytube.exceptions.RegexMatchError: regex pattern (yt\.akamaized\.net/\)\s*\|\|\s*.*?\s*c\s*&&\s*d\.set\([^,]+\s*,\s*(?P<sig>[a-zA-Z0-9$]+)\() had zero matches
signature = cipher.get_signature(js, stream['s'])
These errors are encountered when we try to create a
YouTube object with some youtube link and the link doesn't gets identified as a valid link. This happens because the regular expression used to identify YouTube links could not identify the URL as a valid one, that's why we need to change the code in the library.
The screenshot below shows that this error is thrown in the function
get_initial_function_name that is located in the cipher.py file.
To know where the library files are installed in your system, open command prompt and type the following command.
pip show pytube
You can use this command to locate any python package installed.
After executing that command you will get the location of the library. Open the folder, and edit the cipher.py file (Administrator permissions might be required). It's better to store a copy of the original file somewhere so that if we end up making some wrong changes we can always use the backup file.
Fixing Regular Expression Error
After opening the cipher.py file, do the following changes
original code (at line 39):
fixed code (replace at line 39) with the following code:
After this fix, you can use any YouTube link without getting any error(fingers crossed).
2. Error with the property Views
Like we mentioned before as well, due to some changes in the YouTube API you might get error while using the
Whenever we use the views property, this error occurs because of the old API call made to Youtube by pytube library. This error occurs in __main__.py file at line 290 in the pytube library.
As shown earlier, use the
pip show pytube command in command prompt and open the library directory and then the __main__.py file and make the following changes.
Go to the line number 290 where the error occurred, now replace that line with the following code
and thats it we are done.
3. Error with the Property
Currently there is no fix for this error because the Youtube API is not giving any information on the rating because it is deprecated.
Download the updated library files that are fixed from here: Library Files With errors Fixed or update the files yourself as explained above.
After Fixing all the errors, run the following program:
# Importing YouTube Module from pytube library from pytube import YouTube # Prompting user for Youtube Video link youtube_url = input("Please enter a YouTube link:") # Creating YouTube object with the link myVideo = YouTube(youtube_url) # Title of the Video print("Title: " + myVideo.title) # Length of the Video in Seconds print("Duration: " + myVideo.length) # URL of the Thumbnail of the video print("Thumbnail Link: " + myVideo.thumbnail_url) # Description of the Video print("Description: " + myVideo.description) # Total Views of the Video print("Views: " + myVideo.views) # Age Restricted Content print("Age Restricted: " + str(myVideo.age_restricted)) # ID of the Video print("Video ID: " + myVideo.video_id)
Now that we have created a simple program and are quite familiar with the basic functioning of the pytube library, its time to take a deep dive and understand how we can actually use this library to download and stream the videos.
pytubeLibrary: Working with Streams
Now let's talk about the
StreamQuery Object, pytube library provides two types of streams:
The main difference between these two streams is that Dynamic Adaptive Streaming over HTTP (DASH) is an adaptive bitrate streaming technique while in Progressive stream it doesn't adapt to the current network speed. YouTube uses DASH Stream and that is the reason why it automatically adjusts the quality of the video streamed to your device based on the network speed.
Whenever we use the
streams property on a
YouTube object it will return a
StreamQuery object, which you can use to find all the streams available for the video you have provided the link for.
from pytube import YouTube myVideo = YouTube("https://www.youtube.com/watch?v=oS8lASbvlpI") myVideoStreams = yt.streams print(myVideoStreams)
Output of above Code:
StreamQuery object, we can perform different operations and the most basic one is
streamQuery.all() which gives the information of all the available streams in the form of a list. See the code below:
Output of above Code:
[<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">, <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d4016">, <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">]
In the output, we can observe a list with several streams, but I am showing only a few of the list items. Observe that each stream is just like a tag in HTML with different key-value pairs. Each of these streams have different keys like itag, mime_type, res, fps, vcodec, acodec and abr.
Every stream has a different set of attributes based on which the quality in terms of resolution and codecs differ:
mime_typerefers to the way how a video/audio files are transferred from server to client and is the way of knowing the format of the file (mp4,3gp,WebM etc.,)
res(resolution) is the resolution of the video in the selected stream
fps(frames per second) defines how many frames are rendered per second. In general most of the videos have 30 FPS
refers to audio and video codec's. If the
vcodecis missing in the stream, it means that it is an audio file and has an extra property named
abr(average bitrate)of the audio.
first() is accessed on the
StreamQuery object (
myVideoStream), it will return the highest quality stream available for a given video which consists of the audio codec also.
Similarly, when we use
last() on the
StreamQuery object it will return the audio stream because it is the lowest quality available.
Here is the Syntax:
myHDStream = YouTube("https://www.youtube.com/watch?v=oS8lASbvlpI").streams.first() myAudioStream = YouTube("https://www.youtube.com/watch?v=oS8lASbvlpI").streams.last() print(myHDStream, myAudioStream)
This prints the details of the high definition stream and the audio stream.
Coming to the part where we download the video.
Whenever you get the details of a single stream you can download that stream to your current working directory using the
myStream = YouTube("https://www.youtube.com/watch?v=oS8lASbvlpI").streams.first() myStream.download()
If you want to download the file to a specific location or path on your system, you have to just specify the path where you want to store the file as the parameter to the
Providing Relative Path:
Providing Absolute Path:
Remember that whenever the download statement is executed, the program waits for the video/audio to download and it depends on the speed of your internet connection how long that will take.
As you have seen different keys in the stream like resolution, fps, codec etc. We can use these keys as filtering mechanism on the
StreamQuery object to retrieve the desired stream(s) and then retrieve single stream by using methods like
last() etc., and then download the video using that stream.
myVideoStream = YouTube("https://www.youtube.com/watch?v=oS8lASbvlpI").streams webmStreams = myVideoStream.filter(file_extension = "webm") print(webmStreams.first()) audioStream = myVideoStream.filter(type = "audio") print(audioStream.first()) audioStream.first().download("audios/") webmStreams.first().download("videos/")
The above code shows how to use the
filter() method, we can pass the file_extension parameter and retrieve streams for only the specified file format. Similarly, we can use
type parameter in the
filter() method, using which we can get either stream of type audio or of type video only. After using the
filter() method you can use any of the methods that will retrieve a single stream (
last() etc.,) then you can call the
download() method to download the file.
If you are looking for the code to download a video easily, here is the magical one-line code to download the video with the highest quality available to the current working directory.
One-Line Code To Download a Video:
Here's the link to the GitHub repository where you will find the code for the above programs and usage of different properties:
Please drop any innovative ideas/applications possible with pytube in the comment section below to help our readers gain more insights.
SHARE YOUR THOUGHTS WITH US!
What is Studytonight?
Made with by Abhishek Ahlawat
© 2019 Studytonight. All rights reserved.