DEV Community

Cover image for Python Youtube Downloader.
Pratik Mishra
Pratik Mishra

Posted on

4 2

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 :)

đź‘‹ While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay