loading

The perfect choice of one-stop service for diversification of architecture.

The Easiest Way to Download YouTube Videos Using Python

In one of my first articles on Medium, I showed how to train a Convolutional Neural Network to classify images coming from old GameBoy games Mario and Wario. After 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 can use such images for various machine learning projects.

SetupIn my previous article, I used a library called pytube to download the videos. However, after some changes introduced by YouTube, it is not really usable anymore any attempt to download videos results in KeyError: url_encoded_fmt_stream_map. Also, the library is not 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 fixed (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 we install it using pip install pytube3).Below I present the list of all the imports required for this article:from pytube import YouTube# miscimport osimport shutilimport mathimport datetime# plotsimport matplotlib.

pyplot as plt%matplotlib inline# image operationimport cv2Downloading a videoIn this part, I present how to download a YouTube video using Python. I will use a classic video game from GameBoy Mega Man: Dr. Wilys Revenge. The first step is to create an instance of the YouTube class using the link to the video we want to download.

video YouTube(' youtube.

com/watch?vNqC_1GuY3dw')Using this object, we can download the video/audio, and inspect some of the properties of the video itself. Some of the interesting methods we can use are:length length of the video in secondsrating the rating of the videoviews the number of viewsThe next step is to inspect the available streams using the streams method. We can chain it with the all method to see 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 pytubes documentation available here.Lets narrow down all the available streams to mp4 files using the filter method:video.streams.filter(file_extension "mp4").all()Running the code results in the following selection:In this case, we will use the first available option, the one in 360p (resolution). To download the video, we first 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(18).download()In the download method, we can also specify the destination path for the video. The default value is the current directory.Extract frames from the videoI created a special class called FrameExtractor to as the name pretty much reveals it extract individual frames from the video and save them as images. The class is defined as follows:While instantiating the object of the FrameExtractor class, we need to provide the path to the video we want to work with. In the __init__ method, we also extract some characteristics of the video such as the total number of frames and the frames per second (FPS). In general, the class provides functionality to extract every x-th frame from the video, as the difference between any two neighboring frames will be minimal. We provide a few convenience methods as well. All the methods are described below:get_video_duration prints the duration of the videoget_n_images prints the number of images that will be extracted given we extract every x-th frame (indicated by every_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 for every_x_frame and the name of the image (the numbers indicating the sequence will be added automatically at the end of the name). By default, the images will be saved in the current directory. We can also 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 image file, the default is JPG.Now it is time to actually use the class. We start by instantiating the object of the FrameExtractor class:fe FrameExtractor('Game Boy Longplay 009 Mega Man Dr Wilys Revenge.

mp4')Next, we investigate the length of the video:fe.get_video_duration()# Duration: 0:39:48.333333As an example, lets assume we want to extract every 1000th frame. To calculate the number of images extracted using this setting, we run:fe.

get_n_images(every_x_frame1000)# Extracting every 1000 (nd/rd/th) frame would result in 71 images.As the last step, we extract the images:fe.extract_frames(every_x_frame1000, img_name'megaman', dest_path'megaman_images')# Created the following directory: megaman_imagesThe indicated directory did not exist prior to using the extract_frames method, so it was automatically created and we saw a printed statement confirming this.

Finally, we define a short function for viewing the downloaded images:def show_image(path): image cv2.imread(path) plt.imshow(image) plt.show()show_image('megaman_images/megaman_61.jpg')Running the code results in displaying the following image:ConclusionsIn this article, I described how to download videos from YouTube using the pytube3 library and coded a custom class used for extracting frames as 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 often contains title screens, company logos, etc. The same could be done about the end of the video. However, for the time being, we can also account for that by manually deleting the images that are of no use to us.

You can find the code used for this article on my GitHub. As always, any constructive feedback is welcome. You can reach out to me on Twitter or in the comments.

I recently published a book on using Python for solving practical tasks in the financial domain. If you are interested, I posted an article introducing the contents of the book. You can get the book on Amazon or Packts website.

·RELATED QUESTION

i just download videos and they are avi files, so how do i put it on a disc and watch it on a dvd player?

You need dvd authoring software and a dvd burner/drive

The Easiest Way to Download YouTube Videos Using Python 1

GET IN TOUCH WITH Us
recommended articles
Related Blogs blog
Weve just released Taurion as a tech demo for you to download and test. You can get it on our Discord channel and get support there too.Taurion is not yet finished, ...
Zuchu ft. Mbosso AshuraWCB Wasafi Records newly signed diva, Zuchu teams up with label mate, Mbosso on her new single titled Ashura.The Mocco Genius -produced recor...
Although GPR has been widely used in hydrology, engineering, environment and other fields, many basic theoretical and technical problems have not been fundamentally ...
So I am thinking of a possible answer to my own question:Build my own journal note entry app linked to a wiki.Zim Wiki uses a file based system for wiki. Maybe I cou...
About 8 I think1. where can i get ncaa football 10 rosters with names?For the last two years I got mine from "Pastapadre". From what i can tell they are really prett...
The Haunting I vaguely remember some kind of eye injury in the movie.• Other Related Knowledge ofa cocktail glass— â€&...
rsync can be somewhat painful if you have a very large number of files - especially if your rsync version is lower than 3. On the other hand: if you use tar, you wou...
Blockchain Technology Explained: Powering BitcoinMicrosoft recently became the latest big name to officially associate with Bitcoin, the decentralized virtual curren...
PLEASE HELP ME CHOOSE A VIDEO CAMERA!?the Flip ultra HD is a really good HD portable camcorder, and it's fairly cheap. A lot of famous youtubers use it, such as timo...
In order to implement the tasks proposed in the outline of the national medium and long term science and technology development plan (2006-2020), the national key R ...
no data
Guangzhou
House Empire Construction&Furnishing Co.,Ltd
no data
Sign Up For The Newsletterus
Copyright © 2018 Guangzhou House Empire Construction&Furnishing Co.,Ltd. | All Rights Reserved Design by www.digahousing.com |Sitemap
Customer service
detect