Download Youtube Video using python

Download Youtube Video using python

Hello everyone ! In this post we will help you to download any youtube video using python code.  Using Python, this task is very easy. Few lines of code will download the video from YouTube for you. At first we need to install pafy and youtube-dl packages using pip.

Pafy

In this article, we will see how to extract details about Youtube videos and download them in different formats using the pafy module. Run the following code in your terminal to install pafy.

pip install pafy

Run the command to install youtube-dl packages

pip install youtube-dl

First of all, we need to import pafy library :

import pafy

After that, copy the URL of the YouTube video and paste it :

url="https://www.youtube.com/watch?v=ozTvBsrf1as" 

Further, pass url in one of pafy function, that is YouTube :

video=pafy.new(url)

Lets get the best reolution of video

best=video.getbest()

To download the video in specified location, we need to provide the link of the locaton to store the video.

filename = best.download(filepath="/home/diwas/Desktop/") 

Congartulations, the video has been successfully downloaded in the ‘Desktop’ folder. However, it may take some time to download. Lets check the full code.

Full code

import pafy
#enter the url of the video you want to download
url="https://www.youtube.com/watch?v=ozTvBsrf1as" # link of video url you want to download
video=pafy.new(url)
##get best resolution of the video
best=video.getbest()
###best resolution in required format[give your req format in "preftype"]
best = video.getbest(preftype="mp4")

#if you want to specify a particular path for the video
filename = best.download(filepath="/home/diwas/Desktop/") # change the location as per your need .

## credit :- aihubprojects.com

7 Comments on “Download Youtube Video using python”

Leave a Reply

Your email address will not be published. Required fields are marked *