How to download YouTube videos in Python?

Here, we will see how we can download YouTube videos using pafy modules in Python?
Submitted by Abhinav Gangrade, on July 12, 2020

Modules used:

In this article, we will use pafy module.

pafy module:
pafy is a python library that is used to retrieve Youtube Content and metadata. We can collect video bit-rate, size, duration, rating, author and we can also convert them into audio and we can download the content in the audio format.

Note: We also have to download youtube_dl for the setup of pafy.

Download pafy and youtube_dl:

  • General Way to Download pafy: pip install pafy
  • General Way to Download youtube_dl: pip install youtube_dl
  • Pycharm Users: You can go to the project interpreter and can install these modules from there.

Some of the functions related to pafy:

  1.<url>): This function will take the URL of the youtube video and extract the whole metadata of that video like title, author, stream(audio and video).
  2. video.title: This will return us the title of the video.
  3. This will return us the name of the author of the video.
  4. v=video.streams: This will return us the video streams with the quality and size.
  5. .download(): This will download the video.


# importing the module
import pafy

# extracting the data
# enter the url into it"")

# print the title
# print the author
# print the duration
# print the rating
# Getting the stream
# this fumction return us list
for i in stream:
# getting audio streams
for i in audio_stream:

# get the best video

# get the best audio

# Now download the things
# if want to download best video
# for best audio
# now from the streams


SickFlip & Ritviz - Roshni feat. Seedhe Maut
normal:[email protected]
audio:[email protected]
audio:[email protected]
audio:[email protected]
audio:[email protected]
normal:[email protected]
audio:[email protected]

The downloaded will get saved in the same directory in which your Python file is.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.