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

HOT PRODUCTS
no data
GET IN TOUCH WITH US
recommended articles
Related Blogs blog
Taurion: Tech Demo Released & Open Sourced  Test Now
Taurion: Tech Demo Released & Open Sourced Test Now
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, so this release is a pre-alpha tech demonstration. Elements of the full Taurion game are missing from it, but the elements that are there are more than sufficient to demonstrate the power of the XAYA platform. Just a few of the important elements of Taurion and true blockchain gaming that youll see in this tech demo include:Complex and fully decentralised MMOMovement inside a decentralised worldBasic combat (with health points and shield regeneration)ProspectingChat system utilizing XID (Xaya ID)MoreBlockchain games that run in smart contracts on Ethereum are computationally limited, for example to compute check mate in a game of chess would be too expensive. XAYA is designed in such a way that game worlds can be massively complex.Computational scalability is a major advancement for blockchain games precisely because it unleashes unlimited potential for games that would otherwise need to be trivial, such as pets that can mutate.XAYA also keeps the true-ness of decentralisation (no validator nodes) using a permissionless blockchain secured by PoW. Currently around 14% of Bitcoin miners and numerous GPU miners secure the XAYA blockchain.You can checkout the game mechanics and information for the full game release here can I test Taurion now?Strictly speaking, you wont be playing Taurion proper, but only a partial version, i.e. the tech demo.To get started with this tech demo, you will need:A XAYA wallet (download here)Some CHI (purchase CHI here). You can also ask in our Discord and someone may send you someThe Taurion tech demo the latest version can be found in our Discord in the Taurion channels. You can also ask for support (and beg for CHI)Open-SourceAs well as releasing the tech demo we have also opened up the code for the Taurion Game State Processor (GSP) under the GPLv3 license.What is a GSP?A GSP, or game state processor, is the part of a video game or dapp that contains all of the core game or application logic.The GSP works as a single executable which connects to the XAYA wallet/blockchain to compute the state of the game/dapp and provides a JSON-RPC interface to communicate with a front end (normally).Why is open source important?Its important for fully decentralised trustless blockchain games to have open source code so that the game is transparent. This means that the game code can be checked and therefore it can be provably fair.Provable fairness is crucial for blockchain games. It empowers the value mechanism for in-game assets. No trust is needed and never-before-seen degrees of perfect information are achieved. Free markets can work smoothly while in-game assets can achieve real world value through the price discovery mechanism of the free market.You can find the open source code repository in our GitHub here: opening the code we welcome people to:Contribute to the projectFork the project and make their own game (develop along with us as we add more features) Adjust the gameplay so its more suited to their own project Make a Fantasy or Medieval or Modern day setting/version Whatever they wishCreate their own front endsFor instructions to build you can check our tutorials to build the libxayagame library and then build the taurion_gsp (no extra dependencies needed) simply by:./autogen.sh./configuremakeThe front end is currently closed source due to Epics licensing agreement (i.e. the company behind Unreal Engine) but we are hoping to release parts of the code we can at a future time.Enjoy!Find out how you can get to Taurion at io/.Want to know more about the benefits of blockchain for gaming?Visit the conversation: ·RELATED QUESTIONi 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
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