pafy

raw JSON →
0.5.5 verified Fri May 01 auth: no python maintenance

pafy is a Python library to retrieve YouTube content and metadata, such as video titles, descriptions, thumbnails, and download URLs. Current version is 0.5.5, with no set release cadence; the project appears to be in maintenance mode with infrequent updates.

pip install pafy
error AttributeError: module 'pafy' has no attribute 'new'
cause Importing pafy incorrectly (e.g., 'import pafy.new' instead of 'from pafy import new').
fix
Use 'import pafy' then 'pafy.new(...)' or 'from pafy import new'.
error pafy.util.GdataError: No data returned
cause YouTube API key missing or invalid, or videos are age-restricted/private.
fix
Ensure video is public and not restricted. Set API key via pafy.set_api_key('YOUR_KEY').
error IOError: [Errno 2] No such file or directory: 'youtube-dl'
cause youtube-dl executable not installed or not in PATH.
fix
Install youtube-dl: pip install youtube-dl and ensure it's in PATH.
deprecated pafy uses youtube-dl as its backend, which is no longer actively maintained and may break with YouTube changes. Consider using yt-dlp as a replacement.
fix Switch to yt-dlp with pafy: pip install yt-dlp and set pafy.backend = 'youtube-dl' (though yt-dlp is not natively supported). Better to use yt-dlp directly.
gotcha pafy.new blocks on network calls; calling it in a GUI or async context may freeze the application.
fix Run pafy.new in a thread or use an async-compatible library instead.
breaking As of pafy 0.5.5, the default backend relies on youtube-dl, which may frequently break due to YouTube API changes. Ensure youtube-dl is up to date.
fix Update youtube-dl frequently: pip install --upgrade youtube-dl. Or switch to yt-dlp based forks.
pip install pafy youtube-dl

Fetch metadata from a YouTube video using pafy and print title, views, duration, and best stream URL.

import pafy

url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
video = pafy.new(url)
print(video.title)
print(video.viewcount)
print(video.duration)
stream = video.getbest()
print(stream.url)