获取腾讯视频真实地址(涉及清晰度选择)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Szu_IT_Man/article/details/80449751

前言

  • 由于需要在小程序插入腾讯视频,于是在网上找了诸多方法,但基本都只能抓到低清版本的;偶然发现大神能抓到超清版(720p)的,但由于自己的视频并不都含有超清的版本,因而只能退而求其次使用480p的;这对于移动端(包括全屏)的清晰度已经足够了。毕竟也不是专门做视频的小程序。

  • 环境:linux

  • 语言:python
  • 本文原创地址:https://blog.csdn.net/Szu_IT_Man/article/details/80449751

步骤

主要api

腾讯视频的vids

  • 当你打开一个腾讯视频的时候,比如https://v.qq.com/x/page/x0164ytbgov.html,vids=x0164ytbgov
    这个将作为参数让我们去访问api.

访问getinfo

QZOutputJson=
{
    "dltype":1,
    "exem":0,
    "fl":{
            "cnt":2,
            "fi":[
                    {"id":100701,"name":"msd","lmt":0,"sb":1,"cname":"标清;(270P)","br":28,"profile":2,"drm":0,"video":1,"audio":1,"fs":16546732,"sl":1},
                    {"id":2,"name":"mp4","lmt":0,"sb":1,"cname":"高清;(480P)","br":33,"profile":1,"drm":0,"video":1,"audio":1,"fs":34433483,"sl":0}
                ]
        },
    "hs":0,"ip":"120.36.254.197","ls":0,"preview":555,"s":"o","sfl":{"cnt":0},"tm":1527217153,

    "vl":{
            "cnt":1,
            "vi":[
                    {
                        "br":28,
                        "ch":0,
                        "cl":{"fc":0,"keyid":"x0164ytbgov.100701"},
                        "ct":21600,
                        "drm":0,
                        "dsb":0,
                        "fmd5":"99fb72a48f2a392ec88b5c17bac4ff81",
                        "fn":"x0164ytbgov.m701.mp4",
                        "fs":16546732,
                        "fst":5,
                        "fvkey":"1E327E4FE26E8B8222C226B0873DBF94C094934198E382AE2910361C29E5396237CCF814A49976A5A485F2E806CBC4C21602406034413F9896BF0972F7678D8263845FD123BA9B3604419489CD2BF4254EFD5F87D62A6F88FFF2A4E38F57C9898DD1F7C38C1F548C",
                        "head":0,
                        "hevc":0,
                        "iflag":0,
                        "level":0,
                        "lnk":"x0164ytbgov",
                        "logo":1,
                        "mst":8,
                        "pl":null,
                        "share":1,
                        "sp":0,
                        "st":2,
                        "tail":0,
                        "td":"555.00",
                        "ti":"World Builder (high quality)",
                        "tie":0,
                        "type":3,
                        "ul":{
                                "ui":[
                                        {
                                            "url":"http://ugcws.video.gtimg.com/",
                                            "vt":106,
                                            "dtc":0,
                                            "dt":2
                                        },
                                        {"url":"http://122.228.238.157/vhot2.qqvideo.tc.qq.com/AcSbzObMTmKX0kJqo8qTGhY0OAwzkw14O81CvShBXrlI/","vt":200,"dtc":0,"dt":2},
                                        {"url":"http://ugcdl.video.gtimg.com/","vt":116,"dtc":0,"dt":2},
                                        {"url":"http://video.dispatch.tc.qq.com/","vt":0,"dtc":0,"dt":2}
                                    ]
                            },
                        "vh":256,
                        "vid":"x0164ytbgov",
                        "videotype":0,
                        "vr":0,
                        "vst":2,
                        "vw":480,
                        "wh":1.875,
                        "wl":{"wi":[]}
                    }
                ]
        }
};

拼接低清版本的视频地址

  • 到这一步,我们就可以获取低清版本的视频源地址了。
  • 我们把获取到字符串去掉QZOutputJson=和最后的分号,变成正式的json
realVideoURLRequestPath="http://vv.video.qq.com/getinfo?vids=x0164ytbgov&platform=101001&charge=0&otype=json&defn=shd"
videoInfo = requests.get(realVideoURLRequestPath)
videoInfo_json = videoInfo.text[len('QZOutputJson='):-1]
tempStr = json.loads(videoInfo_json)
  • 这样我们就可以获取到
tempStr['vl']['vi'][0]['ul']['ui'][0]['url']
+tempStr['vl']['vi'][0]['fn']
+"?vkey="
+tempStr['vl']['vi'][0]['fvkey']

拼接高清视频地址

  • 因为我们这个视最清晰只有高清,所以json中的fi字段
"fi":[
        {"id":100701,"name":"msd","lmt":0,"sb":1,"cname":"标清;(270P)","br":28,"profile":2,"drm":0,"video":1,"audio":1,"fs":16546732,"sl":1},
        {"id":2,"name":"mp4","lmt":0,"sb":1,"cname":"高清;(480P)","br":33,"profile":1,"drm":0,"video":1,"audio":1,"fs":34433483,"sl":0}
    ]
  • 如果有超清清的话,一般会多一个id为10701的数组元素,如:
{
    "id":10701,
    "name":"shd",
    "lmt":0,
    "sb":1,
    "cname":"超清;(720P)",
    "br":39,
    "profile":1,
    "drm":0,
    "video":1,
    "audio":1,
    "fs":41560039,
    "sl":0
}
  • 这时,我们就需要用到 http://vv.video.qq.com/getkey api
  • 首先我们先构造filename=[vids]+.mp4 即 filename=x0164ytbgov.mp4
  • 然后访问:

http://vv.video.qq.com/getkey?format=2
&otype=json&vt=150&vid=x0164ytbgov&ran=0\%2E9477521511726081\\&charge=0&filename=x0164ytbgov.mp4&platform=11

  • 返回json2
 QZOutputJson=
{
    "br":62042.312,
    "ct":21600,
    "filename":"x0164ytbgov.mp4",
    "ip":"120.36.254.197",
    "key":"A76112B8282FBD47AE5408A46CBE73D6C3B087688864DAE1C5076726E901E76673A9884A141F398F08A0EB6E9A2C684DA23F92D9A6D0B2787CB108EF75FCD8D7A631A2D40AF2C020392BEE038A662EF1434BF157CAC87218E87A26B65375D06C",
    "keyid":"x0164ytbgov.2",
    "level":0,
    "levelvalid":1,
    "s":"o",
    "sp":0,
    "sr":0
};

拼接超清视频地址

  • 前面说过了额,如果有超清清的话,一般会多一个id为10701的数组元素,如:
{
    "id":10701,
    "name":"shd",
    "lmt":0,
    "sb":1,
    "cname":"超清;(720P)",
    "br":39,
    "profile":1,
    "drm":0,
    "video":1,
    "audio":1,
    "fs":41560039,
    "sl":0
}
  • 这时我们构造的filename=x0164ytbgov.p701.1.mp4
  • 此时访问getkey的api就要改为:

http://vv.video.qq.com/getkey?format=10701
&otype=json&vt=150&vid=x0164ytbgov&ran=0\%2E9477521511726081\\&charge=0&filename=x0164ytbgov.p701.1.mp4&platform=11

  • 聪明的你可能发现了:format={id}
  • 到最后构成真实视频的url:
    tempStr[‘vl’][‘vi’][0][‘ul’][‘ui’][0][‘url’]+filename+?vkey=key (key为json2的key,filename=x0164ytbgov.p701.1.mp4)

  • 由于测试用的视频地址无超清源,所以超清这块的测试需要使用的超清视频做测试。提示已测试可行。


参考

感谢参考文章中的大神。
本文原创地址:https://blog.csdn.net/Szu_IT_Man/article/details/80449751

展开阅读全文

没有更多推荐了,返回首页