参考链接https://helpcdn.aliyun.com/document_detail/68612.html?spm=a2c4g.11174283.6.682.2d50149bh8HsIh
web版阿里云点播,苹果手机并不支持私有加密视频播放,只支持标准加密播放。
这张图片和上边的链接很清晰的把流程给罗列出来了,我就记录几个小问题
先添加一个加速域名:https://help.aliyun.com/document_detail/86074.html?spm=a2c4g.11186623.6.577.7990586fmjANip
CNAME绑定 :https://help.aliyun.com/document_detail/86076.html?spm=a2c4g.11186623.2.12.32312338qlE7gt
判断有没有添加成功可以看媒资库,视频的封面图片有没有显示出来
添加完成之后要提交工单开通域名重写功能,这样才能把MtsHlsUriToken改写到我们配置的解密地址上去
比如说我们配置的解密地址是:http://xxxx.com/index.index.decrypt
视频的播放地址是:http://demo.com/xxx?MtsHlsUriToken=xxxx(你自己管理的token)
ps:这个可以先获取视频播放地址再手动把MtsHlsUriToken和你生成的token拼接上去
视频播放的时候就会跑去访问:http://xxxx.com/index.index.decrypt?Ciphertext=xxxx&MtsHlsUriToken=xxxx
所以校验工作就这里开始
下边是php版的相关代码
上传本地视频
function upload_local_video($filePath){
try {
$uploader = initUploader();
$uploadVideoRequest = new UploadVideoRequest($filePath, '名称');
//$uploadVideoRequest->setCateId(1);
//$uploadVideoRequest->setCoverURL("http://xxxx.jpg");
//$uploadVideoRequest->setTags('test1,test2');
//$uploadVideoRequest->setStorageLocation('outin-xx.oss-cn-beijing.aliyuncs.com');
//$uploadVideoRequest->setTemplateGroupId('VOD_NO_TRANSCODE');
$userData = array(
"MessageCallback"=>array("CallbackURL"=>"http://www.xxx.com/"),
"Format"=>'json'
);
$uploadVideoRequest->setUserData(json_encode($userData));
$res = $uploader->uploadLocalVideo($uploadVideoRequest);
return $res;
} catch (Exception $e) {
printf("testUploadLocalVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
$e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
}
}
开始标准加密
function submitTranscodeJobs($vid) {
$client = initVodClient('xx', 'xxxx');
$request = new vod\SubmitTranscodeJobsRequest();
//需要转码的视频ID
$request->setVideoId($vid);
//创建的加密模板ID
$request->setTemplateGroupId("xxx");
//HLS标准加密配置(只有标准加密才需要传递)
$request->setEncryptConfig(buildEncryptConfig($client));
return $client->getAcsResponse($request);
}
创建秘钥代码
function buildEncryptConfig($client) {
try {
//随机生成一个加密的秘钥,返回的response包含明文秘钥以及密文秘钥,
//视频标准加密只需要传递密文秘钥即可
$request = new kms\GenerateDataKeyRequest();
//秘钥管理服务id,可以提交工单申请开通
$request->setKeyId('xxxx');
$request->setKeySpec("AES_128");
$response = $client->getAcsResponse($request);
$encryptConfig = array();
# 解密接口地址 视频播放时会访问这里,可以在这里获取密文并使用下边的解密函数去解密
$encryptConfig["DecryptKeyUri"] = "http://xxxx.com/index.index.decrypt?Ciphertext=" . $response->CiphertextBlob;
//秘钥服务的类型,目前只支持KMS
$encryptConfig["KeyServiceType"] = "KMS";
# Ciphertext作为解密接口的参数名称,可自定义,此处只作为参考
$encryptConfig["CipherText"] = $response->CiphertextBlob;
return json_encode($encryptConfig);
} catch (Exception $e) {
print $e->getMessage()."\n";
return null;
}
}
解密方法
function buildDecryptConfig($key) {
//秘钥管理服务的key信息
$client = initVodClient('xxx', 'xxxxxx');
$request = new kms\DecryptRequest();
$request->setCiphertextBlob($key);
$response = $client->getAcsResponse($request);
return base64_decode($response->Plaintext);
}
值得注意的是播放过程可能会出现跨域问题,可以在阿里云控制台配置,或者提交工单去问下阿里云
解密接口也可以加上这些
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with, content-type');