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.
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 :
After that, copy the URL of the YouTube video and paste it :
Further, pass url in one of pafy function, that is YouTube :
Lets get the best reolution of video
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.
#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
##get best resolution of the video
###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