DEV Community

Cover image for Python Youtube Downloader.
Pratik Mishra
Pratik Mishra

Posted on

Python Youtube Downloader.

So, today let's write a Python Script to download videos from our favourite video streaming platform: Youtube.
NOTE: Please note that only the videos which are available for download only those can be downloaded.

Let's Begin:
First Let's Import all the required Modules:

pip install youtube_dl 
Enter fullscreen mode Exit fullscreen mode

Importing the installed Module in Python:

import youtube_dl
Enter fullscreen mode Exit fullscreen mode

Now, let's make a list of all the youtube videos i.e their url's to download the videos also let's make a dictionary to specify all the options that we need for our video download as follows:

links = ['Link of your Video', 'Link of another video....',]
options = {

  "format" : "bestvideo+bestaudio"

}
Enter fullscreen mode Exit fullscreen mode

Here, in the options you can apply more filter's but I like to keep it simple by just specifying the format of the video.
The ' bestaudio+bestvideo' will download the best available format of that video file.

The next part is the main one:

with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download(links) 
Enter fullscreen mode Exit fullscreen mode

The youtube_dl.YoutubeDL.download() method will download all the videos from the links that you will provide to it with you desired options as declared earlier.
The Entire code is even less than 10 lines but using it you can easliy automate your task in Python.
Here is the entire code:

import youtube_dl 
links = ['https://www.youtube.com/watch?v=v4dROX2lRMw']
options = {

  "format" : "bestvideo+bestaudio"

}

with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download(links) 
Enter fullscreen mode Exit fullscreen mode

Hope this was useful :)

Top comments (0)