Change language

YouTube Media / Audio Download using Python | pafy

|

Pafy Features
(i) Get metadata like number of views, duration, rating, author, thumbnail, keywords. 
(ii) Download video or audio with the required resolution / bitrate / format / file size
(iii) Command line tool (ytdl) to download directly from the command line
(iv) Download video only (no sound) m4v or webm format
(v) Download audio only (no video) ogg or m4a format
(vi) Works with Python 2.6+ and 3.3+
(vii) Optionally depends on youtube-dl (recommended; more stable)

Install

 virtualenv venv pip install pafy 

Library imported

 import pafy 

Example1:
Get metadata like number of views, duration, rating, author, description.

import pafy

 
# Video URL

url = " https://www.youtube.com/watch?v=Ns4LCeeOFS4&t=320s "

 
# instant creation

video = pafy.new (url)

  
# print title

print (video.title)

 
# print rating

print (video.rating)

 
# print viewcount

print (vi deo.viewcount)

 
# author’s stamp and video length

print (video.author, video.length)

 
# print duration, likes, dislikes and description

print (video.duration, video.likes, video.dislikes, video.description)

Output:

 Dynamic Programming | Set 3 (Longest Increasing Subsequence) | Python.Engineering 4.30275249481 57739 Python.Engineering 396 00:06:36 180 38 Explanation for the article: http://python.engineering/dynamic-programming-set-3-longest-increasing-subsequence/ 

Example2:
Download video with the best resolution regardless of extension

Output:

 normal: 3gp @ 176x144 normal: 3gp @ 320x180 normal: webm @ 640x360 normal: mp4 @ 640x360 normal: mp4 @ 1280x720 1280x720 mp4 25, 707 , 969 Bytes [100.00%] received. Rate: [506 KB / s]. ETA: [0 secs] 

Example3:
Download a video of a specific format (say .3gp)

import pafy

 

url = " https://www.youtube.com/watch?v=eACohWVwTOc "

video = pafy.new (url)

  

streams = video.streams

for i in streams:

print (i)

 
# get the best resolution regardless of format

best = video.getbest ()

  

print (best.resolution, best.extension)

 
# Download video
best.download ()

import pafy

 

url = " https://www.youtube.com/watch?v=eACohWVwTOc "

video = pafy.new (url)

 

streams = video.streams

for i in streams:

  print (i)

  
# get the best resolution of a certain format
# set the format from (mp4, webm, flv or 3gp)

best = video. getbest (preftype = "3gp" )

 
best.download ()

Output:

 normal: 3gp @ 176x144 normal: 3gp @ 320x180 normal: webm @ 640x360 normal: mp4 @ 640x360 normal: mp4 @ 1280x720 6,049, 643 Bytes [100.00%] received. Rate: [241 KB / s]. ETA: [0 secs] 

Example4:
Download audio in a specific format.

import pafy 

 

url = " https://www.youtube.com/watch?v = eACohWVwTOc "

video = pafy.new (url)

 

audiostreams = video.audiostreams

for i in audiostreams:

  print (a.bitrate, i.extension, i.get_filesize ())

 

audiostreams [ 3 ]. download ()

Output:

 160k webm 1365668 160k webm 1811815 160k m4a 3470205 160k webm 3301003 160k webm 3588746 

Example5:
Download best audio

import pafy 

  

url = " https://www.youtube.com/watch?v=eACohWVwTOc "

video = pafy.new (url)

  

bestaudio = video.getbestaudio ()

bestaudio.download ()

Output:

   

Links:
https://pypi.python.org/pypi/pafy

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically