Pafy - 获取 Stream 的 https URL
在本文中,我们将了解如何在 pafy 中获取给定 youtube 视频流的安全 URL。 Pafy 是一个Python库,用于下载 YouTube 内容和检索元数据。 Pafy 对象是包含有关给定视频的所有信息的对象。流基本上可用的视频分辨率可在 youtube 上找到。 URL 代表统一资源定位器,用于指定万维网上的地址。 URL 是连接到网络的任何资源(例如,超文本页面、图像和声音文件)的基本网络标识。该协议指定如何传输来自链接的信息。 Https 是安全的 url。
我们可以借助new
方法获取 pafy 对象,借助allstreams
属性我们可以获取视频可用的所有流,下面是获取给定视频的 pafy 对象的命令
video = pafy.new(url)
streams = video.allstreams
视频 url 应该存在于 youtube 上,因为它会获取 youtube 上存在的那些视频的信息。 YouTube 是美国的在线视频分享平台。
为了做到这一点,我们将url_https
属性与视频的 pafy 流对象一起使用
Syntax : stream.url_https
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 https url of stream
value = stream.url_https
# printing the value
print("URL Htttps : " + str(value))
输出 :
URL Htttps : https://r7---sn-ci5gup-pmj6.googlevideo.com/videoplayback?expire=1594861438&ei=HlMPX-nrNayuz7sP_bGqkAo&ip=171.61.220.239&id=o-AJfFxmBn3FJ12CzPKBH36Nm9uavpac34Biht02INP8B9&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&pl=19&initcwndbps=491250&vprv=1&mime=audio%2Fwebm&gir=yes&clen=554085&dur=65.521&lmt=1590825392886674&mt=1594839691&fvip=7&keepalive=yes&c=WEB&txp=5431432&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRgIhANgTWTY-4ju_s8pnFzJy4gWHZ6QFUuG0jxpiNNzAB3j6AiEA5aykKDegON8rNiRVL6Frr4if8YmYiD5n-9eY0iaovDA%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRgIhAJH2KIEr4_SwK2k4TmhZiqJgHSHbpognkeTApr8jk3xFAiEAwuH5fERHVoT5bTCC7faXjxRXECMBf3umqpwEMCUdrbs%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 https url of stream
value = stream.url_https
# printing the value
print("URL Htttps : " + str(value))
输出 :
URL Htttps : https://r3---sn-ci5gup-pmjk.googlevideo.com/videoplayback?expire=1594861433&ei=GVMPX9yKBs-uwgPV2LHICA&ip=171.61.220.239&id=o-AMJ8u2W9CWH0eQFEr34g3qSXbDvUh5UJp_qaEwhdv4PH&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&pcm2cms=yes&pl=19&initcwndbps=585000&vprv=1&mime=video%2Fwebm&gir=yes&clen=8125774&dur=701.933&lmt=1594573565579581&mt=1594839691&fvip=3&keepalive=yes&c=WEB&txp=5535432&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRgIhAP6QlYxQyQnl7Z34mTkbO5PrMgpy-JhTRU_2z8kVtyxQAiEAxE2yYZYMTNGdMsTqyp_JX4P1ruo-Ceubi0zGYKisaqE%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRgIhAIY99OO_F3qWdoBbuKkVmK-j4moo6A_o6ToHV_0SjMe3AiEAm7w-BZIe_BPosyMCfyCINJLViYEKQwyPlW_L8UXchkU%3D&ratebypass=yes