Download Youtube Video Using Python

In this tutorial i will show you how to download YouTube video or audio with python using pafy library. Pafy is a Python library to download YouTube content and retrieve metadata.

Download Youtube Video Using Python

In this tutorial, I will show you how to download YouTube videos or audio with python using the pafy library.
Pafy is a Python library to download YouTube content and retrieve metadata.

Installation:

pip3 install pafy

Code:

import pafy
url="https://www.youtube.com/watch?v=S-n4ntTRIxY"
video=pafy.new(url)
streams=video.streams
foriin streams:
    print(i)
#get best resolution regardless of format
best=video.getbest()
print(best.resolution,best.extension)
#Download the video.
best.download()

Code Explanation:

1. The code starts by importing the pafy library.
2. Then it creates a new instance of the video class with the URL for YouTube's "S-n4ntTRIxY" video.
3. Next, it gets all of the streams from that video and prints them out to see what they are.
4. It then gets the best resolution for each stream and prints that information out as well.
5.  Finally, it downloads the file using its best resolution and extension.
6. The code attempts to download the video from YouTube.