YouTube Media/Audio Download using Python – pafy

05 Nov, 2021
This tutorial will help you download youtube video or audio with python using pafy library. Pafy library is used to retrieve YouTube content and metadata.
Features of Pafy(i) Retrieve metadata such as viewcount, duration, rating, author, thumbnail, keywords.(ii) Download video or audio at requested resolution / bitrate / format / filesize(iii) Command line tool (ytdl) for downloading directly from the command line(iv) Download video only (no audio) in m4v or webm format(v) Download audio only (no video) in ogg or m4a format(vi) Works with Python 2.6+ and 3.3+(vii) Optionally depends on youtube-dl (recommended; more stable)
Installation
virtualenv venv
pip install pafy

Library Imported
 import pafy
Example1:Retrieve metadata such as viewcount, duration, rating, author, description.
import pafy  # url of videourl = "https://www.youtube.com/watch?v=Ns4LCeeOFS4&t=320s"  # instant createdvideo = pafy.new(url)  # print titleprint(video.title)  # print ratingprint(video.rating)  # print viewcountprint(video.viewcount)  # print author & video lengthprint(video.author, video.length)  # print duration, likes, dislikes & descriptionprint(video.duration, video.likes, video.dislikes, video.description)
Output:
Dynamic Programming | Set 3 (Longest Increasing Subsequence) | GeeksforGeeks
4.30275249481
57739
GeeksforGeeks 396
00:06:36 180 38 Explanation for the article: https://www.geeksforgeeks.org/dynamic-programming-set-3-longest-increasing-subsequence/

Example2:Download best resolution video regardless of extension
import pafy  url = "https://www.youtube.com/watch?v=eACohWVwTOc"video = pafy.new(url)  streams = video.streamsfor i in streams:    print(i)      # get best resolution regardless of formatbest = video.getbest()  print(best.resolution, best.extension)  # Download the videobest.download()
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 video of a particular format specified (let say .3gp)
import pafy  url = "https://www.youtube.com/watch?v=eACohWVwTOc"video = pafy.new(url)  streams = video.streamsfor i in streams:    print(i)      # get best resolution of a specific format# set format out of(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 a specific format audio.
import pafy   url = "https://www.youtube.com/watch?v =eACohWVwTOc"video = pafy.new(url)  audiostreams = video.audiostreamsfor i in audiostreams:    print(i.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 the bestaudio
import pafy   url = "https://www.youtube.com/watch?v=eACohWVwTOc"video = pafy.new(url)  bestaudio = video.getbestaudio()bestaudio.download()
Output:


References:https://pypi.python.org/pypi/pafy
python-utility
Python
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?