欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取腾讯视频的真实地址

程序员文章站 2022-04-16 21:29:38
...

最近在开发项目中涉及到视频的播放,但是这一块就涉及到流量问题,访问量一高服务器就顶不住了,这个时候就需要做优化处理,最好的方式就是利用第三方的存储方式,这里就使用了腾讯视频。

腾讯视频的视频是可以解析出真实地址(其它的没尝试不清楚),在开发小程序中还可以使用腾讯视频插件来播放视频。

这里我就使用小程序来演示视频地址的解析。

  • 封装请求视频的方法

    创建videoPlay.js,代码都写在这里。同时还会对wx.request封装成异步

  • 请求的封装

    这里需要腾讯视频的vid,由这个vid去获取视频的信息
    这是腾讯视频的网页地址https://v.qq.com/x/page/e0354z3cqjp.html,而在地址中的e0354z3cqjp就是vid

let getUrl = (vid) => {
	//请求腾讯视频信息的接口
  let url = `https://vv.video.qq.com/getinfo?vids=${vid}&platform=101001&charge=0&otype=json`;
  //请求封装成promise
  return new Promise((resolve, reject) => {
    wx.request({
      url,
      success: (res) => {
      //处理返回的数据
        let obj = JSON.parse(res.data.slice(13, res.data.length - 1))
        //把重要的信息结构出来
        let {
          fn,
          fvkey,
          ul
        } = obj.vl.vi[0]
        //拼接成视频地址返回
        let url = ul.ui[0].url + fn + '?vkey=' + fvkey;
        resolve(url)       
      }
    })
  })
}
export {
  getUrl
}
QZOutputJson={"dltype":1,"exem":0,"fl":{"cnt":2,"fi":[{"id":100701,"name":"msd","lmt":0,"sb":1,"cname":"标清;(270P)","br":29,"profile":2,"drm":0,"video":1,"audio":1,"fs":5911082,"super":0,"hdr10enh":0,"sname":"标清","resolution":"270P","recommend":0,"sl":1},{"id":2,"name":"mp4","lmt":0,"sb":1,"cname":"高清;(480P)","br":34,"profile":1,"drm":0,"video":1,"audio":1,"fs":12222495,"super":0,"hdr10enh":0,"sname":"高清","resolution":"480P","recommend":0,"sl":0}]},"hs":0,"ip":"14.23.99.50","ls":0,"preview":195,"s":"o","sfl":{"cnt":0},"tm":1601445251,"vl":{"cnt":1,"vi":[{"br":29,"ch":0,"cl":{"fc":0,"keyid":"e0354z3cqjp.100701"},"ct":21600,"drm":0,"dsb":0,"fmd5":"7a7f8d4d4b02d59543a6d0e37f0f014a","fn":"e0354z3cqjp.m701.mp4","fs":5911082,"fst":5,"fvkey":"402E482C4986778E2B8D82C4FA48D4984CA1E4C7434D39BAB25E201B24C1DFEDFD978A9638407D01DF2676BBD3F624C4CB0A8FAF4D233CD83DF6DB4F835E894E2CE6F3A539AD583A638EE0A9F521AD50D0B70F4E97FEDAA8E7100E3C04ED98830C5FAFD36868C587C261DDB928231EBB","head":0,"hevc":0,"iflag":0,"level":0,"lnk":"e0354z3cqjp","logo":1,"mst":8,"pl":null,"share":1,"sp":0,"st":2,"tail":0,"td":"195.46","ti":"Play with Fire","tie":0,"type":3,"ul":{"ui":[{"url":"http://ugcsjy.qq.com/uwMROfz2r5zCIaQXGdGnC2dfhzk_jigLCkkLvI0zErHWpl3G/","vt":176,"dtc":0,"dt":2},{"url":"http://113.96.83.172/om.tc.qq.com/AO-sWPJTBL4Mtd78_GjAHqilAwCgJXWOmyuehNGcc4ro/uwMROfz2r5zCIaQXGdGmXGdfhzkIYteRnEl_4W291jPY-IUT/","vt":219,"dtc":0,"dt":2},{"url":"http://ugccsy.qq.com/uwMROfz2r5zCIaQXGdGmVWdfhzn62vwaMYD5bY9sbyuUtq_A/","vt":126,"dtc":0,"dt":2},{"url":"http://video.dispatch.tc.qq.com/uwMROfz2r5zCIaQXGdGmVWdfhzn62vwaMYD5bY9sbyuUtq_A/","vt":0,"dtc":0,"dt":2}]},"vh":272,"vid":"e0354z3cqjp","videotype":0,"vr":0,"vst":2,"vw":480,"wh":1.7647059,"wl":{"wi":[]},"uptime":1481288537,"fvideo":0,"cached":1,"fvpint":0,"swhdcp":0,"sshot":3,"mshot":0}]}};

返回的数据是字符串,所以要对数据进行处理,把不要的字符串删掉QZOutputJson=还有最后的分号,这样截取出的就是json格式的数据,可以使用json.parse转为对象。

onLoad: function (options) {
    getUrl('e0354z3cqjp').then(res=>{
      console.log(res); 
    })
  },

这里需要的参数就是fn,fvkey,url
获取腾讯视频的真实地址
然后进行地址拼接url + fn + ‘?vkey=’ + fvkey就解析出来。

如果要使用腾讯视频插件的话就更简单了。

  • 绑定腾讯插件
    获取腾讯视频的真实地址

  • 项目配置

//app.json添加一个字段
"plugins":{
    "tencentvideo":{
      "version":"1.3.16",		//插件版本
      "provider":"wxa75efa648b60994b"	//插件appid
    }
  }
  • 使用插件
{
  "usingComponents": {
    "txv-video": "plugin://tencentvideo/video"	//引用组件
  },
  "navigationBarTitleText":"视频播放"
}
<txv-video vid="e0354z3cqjp" playerid="txv1"  bindplay="play"></txv-video>

填写vid后腾讯插件会自动给你解析,playerid就相当于每个播放器的唯一id。

相关标签: 小程序