YouTube Media/Audio Download using Python - pafy


In this article, we will see how to extract details about Youtube videos and download them in different formats using the pafy module. Head over to the link for official documentation.

Install the pafy module using the following command

pip install pafy

If you run the above command, it will generate the following results on the successful installation of the module pafy.

Collecting pafy
Using cached
https://files.pythonhosted.org/packages/b0/e8/3516f761558525b00d3eaf73744eed5c267db
20650b7b660674547e3e506/pafy-0.5.4-py2.py3-none-any.whl
Installing collected packages: pafy
Successfully installed pafy-0.5.4

Check whether you can import the pafy module or not by running the following command.

import pafy

If you found no errors then it's done. Otherwise install the following module to solve the problem.

pip install youtube-dl

If you run the above command, it will generate the following results on the successful installation of the module youtube-dl.

Collecting youtube-dl
Using cached
https://files.pythonhosted.org/packages/b1/ec/fe552181d6bd05a9e5b6b51f6f7ea4fed9f12
1ce595d788217e59318e47c/youtube_dl-2019.7.30-py2.py3-none-any.whl
Installing collected packages: youtube-dl
Successfully installed youtube-dl-2019.7.30

Youtube Video Details

Steps to extract the details of a videos using the link.

  • Import the module pafy

  • Store a link of the video in a variable.

  • Invoke the pafy.new(url) method and store the result in a variable.

  • Get all the information about the video using above variable.

Let's see one example.

## importing the module
import pafy
## url of the video
url = "https://www.youtube.com/watch?v=cr3-J5wDLsM"
## calling the new method of pafy
result = pafy.new(url)
## getting details like title, rating, viewcount, author, length, likes, etc..,
print(f"Title: {result.title}")
print(f"Viewcount {result.viewcount}")
print(f"Author: {result.author}")
print(f"Video Length: {result.length}")
print(f"Likes: {result.likes}")
print(f"Dislikes: {result.dislikes}")
print(f"Description: {result.description}")

If you run the above program, you will get the following results.

Title: Indexing Overview
Viewcount 862
Author: Tutorials Point (India) Pvt. Ltd.
Video Length: 167
Likes: 6
Dislikes: 1
Description: Indexing Overview
Watch more Videos at https://www.tutorialspoint.com/videotutorials/index.htm
Lecture By: Mr. Arnab Chakraborty, Tutorials Point India Private Limited

Downloading Video In Best Quality

  • Import the module pafy

  • Store a link of the video in a variable.

  • Invoke the pafy.new(url) method and store the result in a variable.

  • Get the best quality of video using the getbest method using the above previous variable and store in a variable.

  • Invoke the download method on the previous variable.

See the below example.

## importing the module
import pafy
## url of the video
url = "https://www.youtube.com/watch?v=cr3-J5wDLsM"
## calling the new method of pafy
result = pafy.new(url)
## getting the best quality of video from the 'result' using the getbest()
best_quality_video = result.getbest()
## you can print it to see the quality of the video
print(best_quality_video)
## download it using the download()
best_quality_video.download()

If you run the above program, you will get the following results.

normal:mp4@1280x720
26,638,008 Bytes [100.00%] received. Rate: [ 820 KB/s]. ETA: [0 secs]

You can able to download any type of video using the getbest() methods bypassing the preftype like 3gp, mp4, WebM, etc.., See the below syntax and try it on your own.

## previous steps are same
best_quality_video = result.getbest(preftype = "mp4")
## next steps are same

Downloading Audio In Best Quality

Follow the same process as we did to download the video. Invoke the getbestaudio() instead of getbest() and then download it using the download() method. First, try it yourself. If you found it difficult to see the following code.## importing the module

import pafy
## url of the video
url = "https://www.youtube.com/watch?v=cr3-J5wDLsM"
## calling the new method of pafy
result = pafy.new(url)
## getting the best quality of video from the 'result' using the getbest()
best_quality_audio = result.getbestaudio()
## you can print it to see the quality of the video
print(best_quality_audio)
## download it using the download()
best_quality_audio.download()

If you run the above program, you will get the following results.

audio:m4a@128k
27,518 Bytes [100.00%] received. Rate: [ 306 KB/s]. ETA: [0 secs]
'Indexing Overview.m4a'

Updated on: 02-Jul-2020

819 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements