圆月山庄资源网 Design By www.vgjia.com
视频加密流程图:
后端获取保利威的视频播放授权token,提供接口api给前端
参考文档:http://dev.polyv.net/2019/videoproduct/v-api/v-api-play/create-playsafe-token/
在utils下创建polyv.py,编写token生成工具函数,path:utils/polyv.py
utils 是多个模块的公共函数库的文件夹里面存放自己开发的组件
from django.conf import settings import time import requests #pip install requests import hashlib class PolyvPlayer(object): def __init__(self,userId,secretkey,tokenUrl): """初始化,提供用户id和秘钥""" self.userId = userId self.secretKey = secretkey self.tokenUrl = tokenUrl def tomd5(self, value): """取md5值""" return hashlib.md5(value.encode()).hexdigest() # 获取视频数据的token def get_video_token(self, videoId, viewerIp, viewerId=None, viewerName='', extraParams='HTML5'): """ :param videoId: 视频id :param viewerId: 看视频用户id :param viewerIp: 看视频用户ip :param viewerName: 看视频用户昵称 :param extraParams: 扩展参数 :param sign: 加密的sign :return: 返回点播的视频的token """ ts = int(time.time() * 1000) # 时间戳 plain = { "userId": self.userId, 'videoId': videoId, 'ts': ts, 'viewerId': viewerId, 'viewerIp': viewerIp, 'viewerName': viewerName, } # 按照ASCKII升序 key + value + key + value... + value 拼接 plain_sorted = {} key_temp = sorted(plain) for key in key_temp: plain_sorted[key] = plain[key] plain_string = '' for k, v in plain_sorted.items(): plain_string += str(k) + str(v) # 首尾拼接上秘钥 sign_data = self.secretKey + plain_string + self.secretKey # 取sign_data的md5的大写 sign = self.tomd5(sign_data).upper() # 新的带有sign的字典 plain.update({'sign': sign}) # python 提供的发送http请求的模块 result = requests.post( url=self.tokenUrl, headers={"Content-type": "application/x-www-form-urlencoded"}, data=plain # 平台所需要携带的数据 ).json() # json.loads 把那拿到的数据序列化 token = {} if isinstance(result, str) else result.get("data", {}) # 如果保利威视频平台返回的的字符串 token={} 否则 if token == '': return result return token
在 项目开发时的本地配置 配置参数:
配置文件settings/dev.py,代码:
# 保利威视频加密服务 POLYV_CONFIG = { "userId":"62dc475e3f", "secretkey":"h6FiaEBRMU", "tokenUrl":"https://hls.videocc.net/service/v1/token", }
保利威文档地址:https://my.polyv.net/secure/setting/api
保利威api参考文档:http://dev.polyv.net/2019/videoproduct/v-api/v-api-play/create-playsafe-token/
urls.py,主路由代码:
path(r'polyv/',include('polyv.urls')),
在项目主应用文件夹下创建app
命令:
cd 主应用文件夹下
python3 …/…/manage.py startapp polyv
urls.py,子路由代码:
from django.urls import path,re_path from . import views urlpatterns = [ path('video/',views.Video.as_view(),) ]
polyv/views.py,视图代码:
from django.shortcuts import render # Create your views here. from rest_framework import status from lyapi.utils.polyv import PolyvPlayer from rest_framework.views import APIView from rest_framework.permissions import IsAuthenticated from django.conf import settings from rest_framework.response import Response class VideoView(APIView): # vid = '348e998797383060cb19620b1c600203_3' # permission_classes = [IsAuthenticated, ] #from rest_framework.permissions import IsAuthenticated 登录认证 def get(self,request): polyv_obj = PolyvPlayer(settings.POLYV_CONF['userid'],settings.POLYV_CONF['secretKey'],settings.POLYV_CONF['tokenUrl']) # 调用polyv文件下的polyv_obj类 # vid = 'cee1047a76927eb43774263cd93bb69f_c' # 存在保利威平台的视频ID # vid = '348e998797383060cb19620b1c600203_3' # 存在保利威平台的视频ID vid = request.query_params.get('vid') # 需要把保利威平台的视频ID存在数据库里或者直接或者在前段直接在的Params加 vid cee1047a76927eb43774263cd93bb69f_c viewerIp = request.META.get('REMOTE_ADDR') # 获取用户访问的IP地址 viewerId = request.user.id # 获取用户的id viewerName = request.user.username # 获取用户的账号名 token_dict = polyv_obj.get_video_token(vid,viewerIp,viewerId,viewerName) # 调用polyv文件下的polyv_obj类下的get_video_token方法 print(token_dict) if 'code' in list(token_dict) and token_dict['code'] != 200: # 返回请求失败的信息 return Response(token_dict, status=status.HTTP_403_FORBIDDEN) ''' 返回结果: { "code": 403, "status": "error", "message": "invalid userId or videoId.", "data": "" } ''' return Response(token_dict) # 返回请求成功的信息 ''' 返回结果: { "token": "43883858-92a3-4f25-a6e8-701d10d88cde-f2", "userId": "cee1047a76", "appId": null, "videoId": "cee1047a76927eb43774263cd93bb69f_c", "viewerIp": "127.0.0.1", "viewerId": "2", "viewerName": "root", "extraParams": null, "ttl": 600000, "createdTime": 1605614888570, "expiredTime": 1605615488570, "iswxa": 0, "disposable": false } '''
到这里后端的api接口就已经写好啦!
前段 vue界面 简写:
在src下的components下新建一个Player.vue
# Player.vue <template> <div class="player"> <div id="player"> </div> </div> </template> <script> export default { name:"Player", data () { return { } }, mounted() { //如果需要对标签进行一些加工处理,然后再放数据时,需要用mounted这个钩子函数,如果单纯的是获取数据,添加到数据属性中 // 那么用created方法 this.get_video_data(); }, methods: { get_video_data(){ let user_name = localStorage.username || sessionStorage.username; //token认证 let token = localStorage.token || sessionStorage.token; //token认证 console.log(this.$route.params.vid) let self = this; var player = polyvPlayer({ wrap: '#player', width: document.documentElement.clientWidth - 300, height: document.documentElement.clientHeight, vid: this.$route.params.vid, // forceH5: true, // code: user_name, playsafe: (vid, next) =>{ console.log(self) self.$axios.get(`${self.$settings.Host}/polyv/video/"htmlcode">import Vue from 'vue' import Router from 'vue-router' import Home from '@/components/Home' import Login from '@/components/Login' import Register from "@/components/Register"; import Course from "@/components/Course" import Detail from "@/components/Detail"; import Cart from "@/components/Cart"; import Order from "@/components/Order"; import Player from "@/components/Player"; //Player组件的url 需要复制这里,其他的url忽略 import Myorder from "@/components/Myorder"; Vue.use(Router) export default new Router({ mode:'history', routes: [ { path: '/', //name: 'heme', component: Home }, { path: '/home', //name: 'heme', component: Home }, { path: '/user/login', //name: 'heme', component: Login }, { path: '/register', //name: 'heme', component: Register }, { path: '/courses', //name: 'heme', component: Course }, { path: '/courses/detail/:id', //name: 'heme', component: Detail }, { path: '/cart', //name: 'heme', component: Cart }, { path: '/order', //name: 'heme', component: Order }, { path: '/myorder', //name: 'heme', component: Myorder }, {//Player组件的url 需要复制这个括号里的,其他的url忽略 path: '/polyv/player/:vid', //name: 'heme', component: Player }, ] }) 自己配置的 访问路径http://www.luffycity.cn:8080/polyv/player/cee1047a76927eb43774263cd93bb69f_c```
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月01日
2024年11月01日
- 孟庭苇《情人的眼泪》开盘母带[低速原抓WAV+CUE]
- 孙露《谁为我停留HQCD》[低速原抓WAV+CUE][1.1G]
- 孙悦《时光音乐会》纯银CD[低速原抓WAV+CUE][1.1G]
- 任然《渐晚》[FLAC/分轨][72.32MB]
- 英雄联盟新英雄安蓓萨上线了吗 新英雄安蓓萨技能介绍
- 魔兽世界奥杜尔竞速赛什么时候开启 奥杜尔竞速赛开启时间介绍
- 无畏契约CGRS准星代码多少 CGRS准星代码分享一览
- 张靓颖.2012-倾听【少城时代】【WAV+CUE】
- 游鸿明.1999-五月的雪【大宇国际】【WAV+CUE】
- 曹方.2005-遇见我【钛友文化】【WAV+CUE】
- Unity6引擎上线:稳定性提升、CPU性能最高提升4倍
- 人皇Sky今日举行婚礼!电竞传奇步入新篇章
- 国风与电竞的奇妙邂逅 “电竞北京2024·国风电竞奇妙夜”引领文化新潮流
- 群星《辉煌滚石30男人篇》开盘母带[低速原抓WAV+CUE]
- 群星《藏迷情歌》紫银合金AQCD[低速原抓WAV+CUE]