Downloading YouTube video using Python to a certain directory
In this tutorial, using Python, we will see how you can download a video from YouTube and store it in any directory of your choice.
Pytube library in Python to download Youtube videos
For downloading a video from YouTube using Python, we will need a library called pytube. You can install this library easily using the pip command.
pip install pytube
Once you install the pytube library, import it on your IDE.
Now, we are ready to download videos from YouTube. First, create a variable url in which we will store the URL of the YouTube video we want to download.
url = 'https://www.youtube.com/watch?v=jNQXAC9IVRw' youtube = pytube.YouTube(url)
So, as we can see, after giving the url variable as an argument to the pytube.YouTube() function, the function downloads the video from YouTube. We assign this downloaded video to another variable youtube. Now, we need to store the video in a particular directory, say Downloads.
video = youtube.streams.first() video.download(r'C:\Users\anish\Downloads')
Here, we first have to choose any one format for the stream. Stream contains various parameters like resolution, fps, vcodec etc. Here we have chosen the first stream format and assigned it to a variable video. Now, we use the .download() function and give the path of the directory in which we want to store the video.
As a result, after going to the Downloads folder on your PC, you will be able to see the downloaded video.
Also read: Create a GUI downloader App using Python