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

About Diwas

🚀 I'm Diwas Pandey, a Computer Engineer with an unyielding passion for Artificial Intelligence, currently pursuing a Master's in Computer Science at Washington State University, USA. As a dedicated blogger at AIHUBPROJECTS.COM, I share insights into the cutting-edge developments in AI, and as a Freelancer, I leverage my technical expertise to craft innovative solutions. Join me in bridging the gap between technology and healthcare as we shape a brighter future together! 🌍🤖🔬

View all posts by Diwas →

25 Comments on “Download Youtube Video using python”

  1. I’ve been browsing online more than 2 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. In my view, if all
    site owners and bloggers made good content as you did, the internet
    will be a lot more useful than ever before.

  2. Thanks a bunch for sharing this with all of us you actually know what you’re talking about!
    Bookmarked. Kindly also talk over with my site =).

    We could have a link alternate contract among us

Leave a Reply

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