How To Extract Captions From Youtube Pytube Client Id Code
Let's kickoff this article with some facts nearly YouTube:
- 100 -120 hours of video content is added to YouTube every 10 minutes.
- To picket all the bachelor YouTube videos, it will accept more than than one,00,000 years.
- More than 1.8 billion users sign into YouTube every month.
- Most of the programmers around the world use YouTube as the main source of learning, because of the huge noesis base of operations (Lots of educational videos).
The principal reason why I am talking about YouTube is that with huge amount of content available on YouTube, almost of us might have been using third-party applications to download videos from YouTube to admission them offline. And even though we use third-political party applications or plug-ins we still terminate up downloading low quality videos and have to upgrade to so chosen "Premium Account" to download the videos in high quality.
And then why not build your own application to download YouTube videos? How? Using pytube module of python.
Unlike another famous libraries, pytube doesn't have any 3rd-party dependencies. Pytube is a low-cal-weight python library with a rich gear up of features for downloading YouTube videos adult past Nick Ficano. This open source projection is very easy to employ simply has a lot of bugs(still a work in progress). Towards the finish of this article, I will be providing some code snippets to rectify some of the errors that might occur while you lot utilize the library. Hope these errors go stock-still by the next release.
Installing pytube
Library
Open command prompt(terminal) and blazon in the post-obit command:
pip install pytube
pytube is not dependent on any other library, and then you don't need to install any other libraries. It is available for almost all the latest versions of python (2.7, three.4 and higher). To start working with pytube, create a python file and type in the following statements,
from pytube import YouTube
The to a higher place command means that from the library pytube nosotros are using the "YouTube" module and from at present on this YouTube
keyword will exist interim equally an Object. This object provides many methods and backdrop like length, championship, rating etc. Note that in the give-and-take YouTube letters Y and T are in majuscule, if the case is not given properly eventually we will end up with a lot of errors.
There are in full five types of Objects available in the pytube library, they are:
-
YouTube
Object -
Stream
Object -
StreamQuery
Object -
Caption
Object -
CaptionQuery
Object
In this article we volition mostly work with the YouTube
object while briefly covering other objects equally well.
The YouTube
Object
To fetch information about the video that y'all desire 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. Merely copy the URL of the Youtube video and pass information technology every bit an argument to the YouTube()
object.
Here is the code,
myVideo = YouTube("https://www.youtube.com/watch?v=OTCuykFHBeA")
At present with the myVideo
object created, allow us learn how to retrieve some of the basic informations about the video:
Properties of the YouTube
Object
Following are the backdrop(information about the YouTube video) of the YouTube
object which we tin access.
- Title
- Length
- Thumbnail_URL
- Clarification
- Views*
- Rating*
- Age Restricted
- Video Id
Notation: *Refers that you lot may get errors while using the property, I will be explaining how to ready those errors in the "Possible Errors and their fixes" section beneath.
1. Championship
print(myVideo.title)
The property title
of the YouTube
object (myVideo
) returns the championship of the video. (Please refer to the image above)
2. Length
print(myVideo.length)
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 volition e'er be in seconds.
3. Thumbnail Url
print(myVideo.thumbnail_url)
Thumbnail is an image(more often than not picked up from the video itself) which is the representational image or brusk descriptive prototype of the video. When this thumbnail_url
property is used on the YouTube
object it returns a URL for the thumbnail picture.
4. Clarification
print(myVideo.description)
Every video on Youtube has some description which gives data near the video and some hyperlinks to the associated blogs or websites etc. This description can be viewed past using the clarification
property of the YouTube
object (myVideo
).
v. Views
impress(myVideo.views)
The YouTube object (myVideo
) has this property called views
which returns the number of times the video has been viewed. This holding might not work correctly because of the changes in the Youtube API recently. Then you have to modify the code in the pytube library. All the changes that should be made are listed below in a separate section.
half dozen. Rating
print(myVideo.rating)
rating
belongings will give usa the average rating of the YouTube video being accessed. Only similar the views
belongings, rating
belongings might also through some errors.
vii. Age Restricted
print(myVideo.age_restricted)
Some videos on YouTube are age restricted(tin be viewed when signed in), to know whether a detail video is age restricted or not we can use the property age_restricted
of the YouTube
object (myVideo
) which returns a Boolean
value.
If the value is imitation and then the video is not age restricted and if its true so the video is restricted.
8. Video Id
print(myVideo.video_id)
This holding video_id
returns the ID of the video, which is generally nowadays at the end of the Youtube video URLs.
Possible Errors And their Fixes
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*\|\|\southward*.*?\s*c\south*&&\south*d\.prepare\([^,]+\s*,\due south*(?P<sig>[a-zA-Z0-9$]+)\() had zero matches
Or,
signature = naught.get_signature(js, stream['due south'])
These errors are encountered when nosotros endeavor to create a YouTube
object with some youtube link and the link doesn't gets identified as a valid link. This happens considering the regular expression used to place YouTube links could not place the URL every bit a valid one, that'due south why nosotros 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 post-obit command.
pip testify pytube
Yous can use this control to locate whatever python package installed.
After executing that command you will get the location of the library. Open up the binder, and edit the nada.py file (Administrator permissions might exist required). Information technology's better to shop a copy of the original file somewhere so that if nosotros stop up making some wrong changes we can e'er use the backup file.
Fixing Regular Expression Error
After opening the nada.py file, do the following changes
original code (at line 39):
r'yt\.akamaized\.net/\)\s*\|\|\s*'
fixed code (supercede at line 39) with the following code:
r'\bc\s*&&\s*d\.set up\([^,]+\due south*,\s*\([^)]*\)\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
Later this fix, you can utilize whatsoever YouTube link without getting any fault(fingers crossed).
2. Error with the property Views
Similar we mentioned before also, due to some changes in the YouTube API you lot might go error while using the views
property.
Whenever we use the views property, this error occurs considering of the former API call made to Youtube by pytube library. This mistake occurs in __main__.py file at line 290 in the pytube library.
Fixing view_count
Error
Every bit shown earlier, use the pip evidence pytube
command in command prompt and open the library directory and so the __main__.py file and make the following changes.
Get to the line number 290 where the mistake occurred, at present supervene upon that line with the post-obit code
return self.player_config_args['player_response']['videoDetails']['viewCount']
and thats it we are done.
3. Mistake with the Holding rating
Currently there is no set for this mistake because the Youtube API is not giving whatever information on the rating considering it is deprecated.
Download the updated library files that are stock-still from here: Library Files With errors Stock-still or update the files yourself as explained above.
Program to get Data of YouTube Video using pytube
Later on 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("Championship: " + myVideo.championship) # 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.clarification) # Total Views of the Video impress("Views: " + myVideo.views) # Age Restricted Content print("Age Restricted: " + str(myVideo.age_restricted)) # ID of the Video print("Video ID: " + myVideo.video_id)
At present that nosotros have created a simple program and are quite familiar with the basic functioning of the pytube library, its fourth dimension to have a deep dive and understand how we can actually use this library to download and stream the videos.
pytube
Library: Working with Streams
At present let's talk about the StreamQuery
Object, pytube library provides two types of streams:
- DASH Stream
- Progressive Stream
The chief difference between these two streams is that Dynamic Adaptive Streaming over HTTP (Dash) is an adaptive bitrate streaming technique while in Progressive stream information technology 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.
Youtube way of streaming a Video
Whenever we utilize the streams
property on a YouTube
object it will return a StreamQuery
object, which you tin can use to notice all the streams available for the video yous have provided the link for.
from pytube import YouTube myVideo = YouTube("https://world wide web.youtube.com/watch?5=oS8lASbvlpI") myVideoStreams = yt.streams print(myVideoStreams)
Output of higher up Code:
pytube.query.StreamQuery
With this StreamQuery
object, nosotros can perform different operations and the most bones one is streamQuery.all()
which gives the data of all the available streams in the grade of a listing. Meet the code below:
impress(myVideoStreams.all())
Output of above Lawmaking:
[<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="sound/mp4" abr="128kbps" acodec="mp4a.40.2">]
In the output, nosotros can observe a listing with several streams, just I am showing only a few of the list items. Observe that each stream is only like a tag in HTML with dissimilar key-value pairs. Each of these streams have unlike keys similar 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_type
refers to the mode 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 2d) defines how many frames are rendered per second. In general nearly of the videos have xxx FPS -
acodec
vcodec
vcodec
is missing in the stream, information technology means that it is an sound file and has an extra property namedabr
(average bitrate)of the audio.
first()
and last()
on StreamQuery
When first()
is accessed on the StreamQuery
object (myVideoStream
), it volition return the highest quality stream bachelor for a given video which consists of the audio codec besides.
Similarly, when we use last()
on the StreamQuery
object information technology will return the audio stream considering it is the lowest quality available.
Hither is the Syntax:
YouTube('URL').streams.starting time() YouTube('URL').streams.last()
Usage Example:
myHDStream = YouTube("https://www.youtube.com/lookout?v=oS8lASbvlpI").streams.starting time() myAudioStream = YouTube("https://www.youtube.com/watch?v=oS8lASbvlpI").streams.last() impress(myHDStream, myAudioStream)
This prints the details of the high definition stream and the audio stream.
Downloading the YouTube Video
Coming to the part where we download the video.
Whenever yous become the details of a single stream you can download that stream to your electric current working directory using the download()
method.
myStream = YouTube("https://www.youtube.com/watch?v=oS8lASbvlpI").streams.first() myStream.download()
If yous desire to download the file to a specific location or path on your organisation, yous take to just specify the path where y'all want to store the file every bit the parameter to the download()
method.
Providing Relative Path:
myStream.download("Videos/")
Providing Accented Path:
myStream.download("D:/Videos/")
Recall that whenever the download argument is executed, the plan waits for the video/audio to download and information technology depends on the speed of your internet connection how long that will take.
Using Filters
Every bit you have seen unlike keys in the stream like resolution, fps, codec etc. We can utilize these keys as filtering machinery on the StreamQuery
object to retrieve the desired stream(south) and then retrieve single stream by using methods like first()
, terminal()
etc., and and then download the video using that stream.
myVideoStream = YouTube("https://www.youtube.com/spotter?five=oS8lASbvlpI").streams webmStreams = myVideoStream.filter(file_extension = "webm") print(webmStreams.outset()) audioStream = myVideoStream.filter(blazon = "sound") print(audioStream.kickoff()) audioStream.first().download("audios/") webmStreams.first().download("videos/")
The higher up code shows how to utilize the filter()
method, we tin can laissez passer the file_extension parameter and retrieve streams for only the specified file format. Similarly, we can use blazon
parameter in the filter()
method, using which we can get either stream of type sound or of blazon video only. Later on using the filter()
method you can utilise any of the methods that will retrieve a single stream (first()
, last()
etc.,) then you tin telephone call the download()
method to download the file.
If you are looking for the code to download a video hands, here is the magical 1-line code to download the video with the highest quality available to the current working directory.
One-Line Code To Download a Video:
YouTube("https://www.youtube.com/scout?v=oS8lASbvlpI").streams.first().download()
Here'southward the link to the GitHub repository where you will find the code for the in a higher place programs and usage of unlike backdrop:
Sample Programs for pytube
Applications of pytube
:
- Build a media player to stream YouTube videos.
- Automatically get the number of views of a YouTube video yous desire to monitor or a dashboard for your Youtube channel.
- Download multiple videos or playlist with ease.
- Download Subtitles of videos.
Please drop any innovative ideas/applications possible with pytube in the annotate section beneath to help our readers gain more insights.
Happy CODING!!
Yous may besides like:
- 11 YouTube Playlist Downloaders
- How to Download YouTube Videos?
- 21 Best Costless YouTube to MP3 Converter & Downloader Apps to use in 2021
- Converting YouTube videos to Mp3 Files
Source: https://www.studytonight.com/post/pytube-to-download-youtube-videos-with-python
Posted by: andersondadogiag.blogspot.com
0 Response to "How To Extract Captions From Youtube Pytube Client Id Code"
Post a Comment