How To Download Youtube Videos Using Python
The easiest fashion to download YouTube videos using Python
And how to employ a custom class to excerpt frames as images
In one of my first articles on Medium, I showed how to train a Convolutional Neural Network to classify images coming from erstwhile GameBoy games — Mario and Wario. Afterwards over a year, I wanted to revisit one aspect of the process — downloading videos (and potentially audio) from YouTube videos and extracting frames as images. We tin utilize such images for various motorcar learning projects.
Setup
In my previous article, I used a library called pytube
to download the videos. However, subsequently some changes introduced by YouTube, it is non really usable anymore — any attempt to download videos results in KeyError: 'url_encoded_fmt_stream_map'
. Likewise, the library is non actively maintained anymore.
That is why in this article I suggest using pytube3
, which is a fork of the original pytube
library and has the error stock-still (it only works with Python 3). All the functionalities of the original library are preserved and we actually still use import pytube
to import the library (even though nosotros install it using pip install pytube3
).
Below I nowadays the listing of all the imports required for this article:
from pytube import YouTube # misc
import os
import shutil
import math
import datetime # plots
import matplotlib.pyplot as plt
%matplotlib inline # image operation
import cv2
Downloading a video
In this part, I nowadays how to download a YouTube video using Python. I volition employ a classic video game from GameBoy — Mega Man: Dr. Wily'southward Revenge. The commencement stride is to create an instance of the YouTube
grade using the link to the video nosotros want to download.
video = YouTube('https://www.youtube.com/watch?v=NqC_1GuY3dw')
Using this object, we tin download the video/audio, and audit some of the properties of the video itself. Some of the interesting methods nosotros can apply are:
-
length
— length of the video in seconds -
rating
— the rating of the video -
views
— the number of views
The next step is to inspect the available streams using the streams
method. Nosotros can chain information technology with the all
method to meet all the available streams.
video.streams.all()
Running that line of code returns the following list of media formats:
For a more in-depth description of the options regarding the media formats and working with streams, I refer to pytube
's documentation available here.
Let's narrow down all the available streams to mp4 files using the filter method:
video.streams.filter(file_extension = "mp4").all()
Running the lawmaking results in the following selection:
In this case, we will use the first available option, the i in 360p (resolution). To download the video, we outset indicate which one we want to download by using the itag
and then download the file using the download
method. The full code for downloading the video is as follows:
video.streams.get_by_itag(eighteen).download()
In the download
method, nosotros can too specify the destination path for the video. The default value is the current directory.
Extract frames from the video
I created a special form called FrameExtractor
to — as the name pretty much reveals it — extract individual frames from the video and save them equally images. The class is defined as follows:
While instantiating the object of the FrameExtractor
class, nosotros need to provide the path to the video we want to piece of work with. In the __init__
method, we likewise extract some characteristics of the video such every bit the total number of frames and the frames per second (FPS). In general, the class provides functionality to extract every 10-th frame from the video, as the difference betwixt any ii neighboring frames will be minimal. We provide a few convenience methods too. All the methods are described below:
-
get_video_duration
— prints the duration of the video -
get_n_images
— prints the number of images that will be extracted given we extract every x-th frame (indicated byevery_x_frame
) -
extract_frames
— this is the main method of the class, which is used to extract the images. The bare minimum is providing the value forevery_x_frame
and the proper noun of the image (the numbers indicating the sequence will be added automatically at the terminate of the name). By default, the images will be saved in the current directory. We can too provide a path to the desired directory (dest_path
), and if it does not exist, it will be created for us. We can also specify the format of the prototype file, the default is JPG.
At present it is fourth dimension to actually use the class. We start by instantiating the object of the FrameExtractor
class:
iron = FrameExtractor('Game Boy Longplay [009] Mega Human being Dr Wilys Revenge.mp4')
Adjacent, we investigate the length of the video:
atomic number 26.get_video_duration()
# Duration: 0:39:48.333333
Every bit an example, let's assume nosotros want to extract every 1000th frame. To calculate the number of images extracted using this setting, nosotros run:
fe.get_n_images(every_x_frame=1000)
# Extracting every g (nd/rd/thursday) frame would result in 71 images.
Equally the final step, we extract the images:
iron.extract_frames(every_x_frame=m,
img_name='megaman',
dest_path='megaman_images') # Created the following directory: megaman_images
The indicated directory did not be prior to using the extract_frames
method, so information technology was automatically created and we saw a printed statement confirming this.
Finally, we ascertain a short function for viewing the downloaded images:
def show_image(path):
image = cv2.imread(path)
plt.imshow(prototype)
plt.bear witness() show_image('megaman_images/megaman_61.jpg')
Running the lawmaking results in displaying the post-obit image:
Conclusions
In this article, I described how to download videos from YouTube using the pytube3
library and coded a custom grade used for extracting frames equally images from the downloaded videos. A potential modification to the class is to account for skipping the first n seconds of the video, as the beginning oftentimes contains title screens, company logos, etc. The same could be done about the end of the video. However, for the time being, nosotros tin can also business relationship for that by manually deleting the images that are of no use to usa.
Yous tin discover the code used for this article on my GitHub. Equally ever, whatever effective feedback is welcome. Y'all can reach out to me on Twitter or in the comments.
Liked the article? Get a Medium member to continue learning by reading without limits. If you use this link to become a member, you lot volition support me at no extra cost to you. Thank you in accelerate and see yous around!
I recently published a book on using Python for solving practical tasks in the fiscal domain. If you are interested, I posted an article introducing the contents of the book. You can get the book on Amazon or Packt's website.
Source: https://towardsdatascience.com/the-easiest-way-to-download-youtube-videos-using-python-2640958318ab
Posted by: sotobrong2000.blogspot.com
0 Response to "How To Download Youtube Videos Using Python"
Post a Comment