📅  最后修改于: 2023-12-03 14:45:30.877000             🧑  作者: Mango
You can install
pip install pafy
Here are some features of
To use new
function from the pafy
module and pass the YouTube video URL or video ID as an argument:
import pafy
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
video = pafy.new(url)
Use the title
, description
, author
, thumbnail
, duration
, views
, and rating
properties to retrieve the video metadata:
print("Title:", video.title)
print("Description:", video.description)
print("Author:", video.author)
print("Thumbnail:", video.thumb)
print("Duration:", video.duration)
print("Views:", video.viewcount)
print("Rating:", video.rating)
Use the streams
property to get the available video streams and their URLs. You can then select the stream you want based on its resolution and download it or stream it:
print(video.streams)
best = video.getbest()
print(best.url)
best.download()
You can use the download
method to download the video or audio to your local directory:
bestaudio = video.getbestaudio()
bestaudio.download()
If you only want to stream the audio, you can use the getbestaudio
method to get the best available audio stream and then use the audio_stream
property to stream it:
bestaudio = video.getbestaudio()
audiostream = bestaudio.audio_stream
audiostream.play()