Pafy – 获取 Stream 的 URL
在本文中,我们将看到如何在 pafy 中获取给定 youtube 视频流的 url。 Pafy 是一个Python库,用于下载 YouTube 内容和检索元数据。 Pafy 对象是包含有关给定视频的所有信息的对象。流基本上可用的视频分辨率可在 youtube 上找到。 URL 代表统一资源定位器,用于指定万维网上的地址。 URL 是连接到网络的任何资源(例如,超文本页面、图像和声音文件)的基本网络标识。该协议指定如何传输来自链接的信息。
我们可以借助new
方法获取 pafy 对象,借助allstreams
属性我们可以获取视频可用的所有流,下面是获取给定视频的 pafy 对象的命令
video = pafy.new(url)
streams = video.allstreams
视频 url 应该存在于 youtube 上,因为它会获取 youtube 上存在的那些视频的信息。 YouTube 是美国的在线视频分享平台。
为了做到这一点,我们将url
属性与视频的 pafy 对象一起使用
Syntax : stream.url
Argument : It takes no argument
Return : It returns string
下面是实现
# importing pafy
import pafy
# url of video
url = "https://www.youtube.com / watch?v = vG2PNdI8axo"
# getting video
video = pafy.new(url)
# getting all the available streams
streams = video.allstreams
# selecting one stream
stream = streams[1]
# getting url of stream
value = stream.url
# printing the value
print("URL : " + str(value))
输出 :
URL : https://r7---sn-ci5gup-pmj6.googlevideo.com/videoplayback?expire=1594860925&ei=HVEPX62pGozmvwTroYioDQ&ip=171.61.220.239&id=o-ABGKfYL2zfzM02gQs2UDuKQhEteEMoIC2xCH6K7IQj0v&itag=250&source=youtube&requiressl=yes&mh=rM&mm=31%2C29&mn=sn-ci5gup-pmj6%2Csn-ci5gup-qxae7&ms=au%2Crdu&mv=m&mvi=7&pcm2cms=yes&pl=19&initcwndbps=472500&vprv=1&mime=audio%2Fwebm&gir=yes&clen=554085&dur=65.521&lmt=1590825392886674&mt=1594839211&fvip=7&keepalive=yes&c=WEB&txp=5431432&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRAIgMhq983QOGWQZAA2eFGKQEyxY1n0xJC9DEZgTAtLiqXYCIC-rXtlUPsuJ9GZj5GwtRMosgFJfttC6BAoa41c6Gmsn&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIhAMS2ZVj37jMWTLAe1Z5pUh5hZgaScudgAP_xwr6wDbbBAiAEyxwPwYVIVNfYcoP7pg5QrV3y6CPlGzWvEVfsL27xbg%3D%3D&ratebypass=yes
另一个例子
# importing pafy
import pafy
# url of video
url = "https://www.youtube.com / watch?v = i6rhnSoK_gc"
# getting video
video = pafy.new(url)
# getting all the available streams
streams = video.allstreams
# selecting one stream
stream = streams[4]
# getting url of stream
value = stream.url
# printing the value
print("URL : " + str(value))
输出 :
URL : https://r3---sn-ci5gup-pmjk.googlevideo.com/videoplayback?expire=1594860934&ei=JVEPX6P4OeLW4-EPsaWvgAU&ip=171.61.220.239&id=o-AP4eJDZeRovXfTcJC7nirnR2mFWhuUym4TbwvLgP8j16&itag=278&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278&source=youtube&requiressl=yes&mh=x9&mm=31%2C29&mn=sn-ci5gup-pmjk%2Csn-ci5gup-qxae7&ms=au%2Crdu&mv=m&mvi=3&pl=19&pcm2=no&initcwndbps=591250&vprv=1&mime=video%2Fwebm&gir=yes&clen=8125774&dur=701.933&lmt=1594573565579581&mt=1594839211&fvip=3&keepalive=yes&c=WEB&txp=5535432&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cpcm2%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRgIhAINLqM5ms2bTAZdWrMmejz_11HqKnzC-tsE1WOomPA6dAiEAkuGowLis8kUTYCE0Yf4CtIs4ftni_EY3WwCrYxpMIHk%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgdO1854F8W8wb6YjYROdQ0uDtCd8ef6pGUIQ3Qe-kZVMCICmtOj19ekFBmzcxrWMlM2FwDt3kW8AbVQw_nJKwWWyR&ratebypass=yes