目录
关于腾讯云点播媒体管理
腾讯云点播(video on demand)服务基于多年技术积累与基础设施建设,为有音视频应用相关需求的客户提供包括音视频存储管理、音视频转码处理、音视频加速播放、音视频加密、音视频ai能力的一站式解决方案,可通过云平台进行上传后的媒资管理。
目前云点播服务使用的是 api 3.0 版本,要使用腾讯云点播 api,需要先执行以下步骤:
本文将主要介绍如何使用云点播服务之媒体上传的常用接口实现。
开发前准备
(1)调用 api 之前,需要生成 hmac-sha1加密返回签名,请参考如下代码:
/// hmac-sha1加密返回签名
<param name="secret">密钥</param>
<param name="strorgdata">源文</param>
public static string gethmacsha1sign(string secret, string strorgdata)
{
var hmacsha1 = new hmacsha1(encoding.utf8.getbytes(secret));
var databuffer = encoding.utf8.getbytes(strorgdata);
var hashbytes = hmacsha1.computehash(databuffer);
return convert.tobase64string(hashbytes);
}
(2)secretid 及 secretkey 的获取在后续范例中均封装为 tcacount 类,创建及访问示例如下:
tcacount tca = new tcacount();
//应用id
string secretid = tca.secretid;
//应用key
string secretkey = tca.secretkey;
(3) 用到两个时间戳函数,代码如下:
public string gettimestamp(int seconds)
{
timespan ts = datetime.utcnow - new datetime(1970, 1, 1, 0, 0, 0, 0);
return convert.toint64(ts.totalseconds + seconds).tostring();
}
public string gettimestamp(datetime dtime)
{
timespan tspan = dtime.touniversaltime() - new datetime(1970, 1, 1, 0, 0, 0, 0);
return convert.toint64(tspan.totalseconds).tostring();
}
(4) sendrequest 方法实现访问 api url 地址并 post 数据,以获取返回结果 json 的功能,参考代码如下:
public static string sendrequest(string url, string completeurl)
{
servicepointmanager.securityprotocol = (securityprotocoltype)192 | (securityprotocoltype)768 | (securityprotocoltype)3072;
httpwebrequest request = (httpwebrequest)webrequest.create(url);
request.method = "post";
request.contenttype = "application/x-www-form-urlencoded";
request.protocolversion = httpversion.version10;
request.host = url.replace("https://", "").replace("/", "");
byte[] data = encoding.utf8.getbytes(completeurl);
request.contentlength = data.length;
stream newstream = request.getrequeststream();
newstream.write(data, 0, data.length);
newstream.close();
httpwebresponse response = null;
string content;
try
{
response = (httpwebresponse)request.getresponse();
streamreader reader = new streamreader(response.getresponsestream(), encoding.utf8);
content = reader.readtoend();
}
catch (webexception e)
{
response = (httpwebresponse)e.response;
using (stream errdata = response.getresponsestream())
{
using (streamreader reader = new streamreader(errdata))
{
content = reader.readtoend();
}
}
}
return content;
}
(5) 需要引用 newtonsoft.json.dll 动态链接库。
范例运行环境
操作系统: windows server 2019 datacenter
.net版本: .netframework4.0 或以上
开发工具:vs2019 c#
常用上传api
申请上传
applyupload 方法用于申请媒体文件的上传,获取文件上传到云点播的元信息(包括上传路径、上传签名等),用于后续上传接口。其关键属性方法说明如下:
序号 | 参数 | 类型 | 说明 |
---|---|---|---|
1 | mediatype | string | 媒体格式,包括: 视频:wmv、rm、mov、mpeg、mp4、3gp、flv、avi、rmvb、ts、asf、mpg、webm、mkv 、m3u8、wm、asx、ram、mpe、vob、dat、mp4v、m4v、f4v、mxf、qt、ogg。 音频:mp3、m4a、flac、ogg、wav、ra、aac、amr。 |
2 | medianame | string | 媒体名称,如 test.mp4 |
实现代码如下:
public string applyupload(string mediatype, string medianame)
{
string medianame = "";
if (medianame != "")
{
medianame = "&medianame=" + medianame;
}
tcacount tca = new tcacount("");
//请求地址
string settingurl = "https://vod.tencentcloudapi.com/";
//应用id
string secretid = tca.secretid;
//应用key
string secretkey = tca.secretkey;
//时间戳
string timestamp = gettimestamp();
//nonce
var nonce = new random().next(10000, 99999);
//拼接参数
string paramsstr = string.format(@"action=applyupload{4}&mediatype={3}&nonce={0}®ion=ap-beijing&secretid={1}&signaturemethod=hmacsha1×tamp={2}&version=2018-07-17",
nonce, secretid, timestamp, mediatype,medianame);
//生成签名参数
// string requesttext = settingurl + "?" + paramsstr;
string requesttext = "post" + settingurl.replace("https://", "") + "?" + paramsstr;
//获得请求签名
string signtext = gethmacsha1sign(secretkey, requesttext);
//这里一定要进行url编码,不然调用api会报错
signtext = httputility.urlencode(signtext, encoding.utf8);
paramsstr = string.format(@"action=applyupload{5}&mediatype={4}&nonce={0}®ion=ap-beijing&secretid={1}&signature={2}&signaturemethod=hmacsha1×tamp={3}&version=2018-07-17",
nonce, secretid, signtext, timestamp, mediatype,medianame);
string resultstr = sendrequest(settingurl, paramsstr);
if (resultstr.indexof("vodsessionkey") != -1)
{
newtonsoft.json.linq.jobject jsonobj = newtonsoft.json.linq.jobject.parse(resultstr);
return jsonobj["response"]["vodsessionkey"].tostring();
}
return "";
} //applyupload
确认上传
commitupload 方法用于确认媒体文件上传到腾讯云点播的结果,并存储媒体信息,返回文件的播放地址和文件 id,其关键属性方法说明如下:
序号 | 参数 | 类型 | 说明 |
---|---|---|---|
1 | vodsessionkey | string | 点播会话,取申请上传接口的 applyupload 方法返回值 vodsessionkey。 |
实现代码如下:
public string commitupload(string vodsessionkey)
{
tcacount tca = new tcacount("");
//请求地址
string settingurl = "https://vod.tencentcloudapi.com/";
//应用id
string secretid = tca.secretid;
//应用key
string secretkey = tca.secretkey;
//时间戳
string timestamp = gettimestamp();
//nonce
var nonce = new random().next(10000, 99999);
//拼接参数
string paramsstr = string.format(@"action=commitupload&nonce={0}®ion=ap-beijing&secretid={1}&signaturemethod=hmacsha1×tamp={2}&version=2018-07-17&vodsessionkey={3}",
nonce, secretid, timestamp, vodsessionkey);
//生成签名参数
// string requesttext = settingurl + "?" + paramsstr;
string requesttext = "post" + settingurl.replace("https://", "") + "?" + paramsstr;
//获得请求签名
string signtext = gethmacsha1sign(secretkey, requesttext);
//这里一定要进行url编码,不然调用api会报错
signtext = httputility.urlencode(signtext, encoding.utf8);
paramsstr = string.format(@"action=commitupload&nonce={0}®ion=ap-beijing&secretid={1}&signature={2}&signaturemethod=hmacsha1×tamp={3}&version=2018-07-17&vodsessionkey={4}",
nonce, secretid, signtext, timestamp, vodsessionkey);
string resultstr = sendrequest(settingurl, paramsstr);
return resultstr;
} //commitupload
拉取上传
pullupload 方法用于将一个网络上的视频拉取到云点播平台,其关键属性方法说明如下:
序号 | 参数 | 类型 | 说明 |
---|---|---|---|
1 | mediaurl | string | 要拉取的媒体 url,暂不支持拉取 dash 格式(可以支持 hls)。 视频:wmv、rm、mov、mpeg、mp4、3gp、flv、avi、rmvb、ts、asf、mpg、webm、mkv 、m3u8、wm、asx、ram、mpe、vob、dat、mp4v、m4v、f4v、mxf、qt、ogg。 音频:mp3、m4a、flac、ogg、wav、ra、aac、amr。 请确保媒体 url 可以访问,示例 :http://www.test.com/test.mp4 |
实现代码如下:
public string pullupload(string mediaurl)
{
tcacount tca = new tcacount("");
//请求地址
string settingurl = "https://vod.tencentcloudapi.com/";
//应用id
string secretid = tca.secretid;
//应用key
string secretkey = tca.secretkey;
//时间戳
string timestamp = gettimestamp();
//nonce
var nonce = new random().next(10000, 99999);
//拼接参数
string paramsstr = string.format(@"action=pullupload&mediaurl={3}&nonce={0}&secretid={1}&signaturemethod=hmacsha1×tamp={2}&version=2018-07-17",
nonce, secretid, timestamp, mediaurl);
//生成签名参数
// string requesttext = settingurl + "?" + paramsstr;
string requesttext = "post" + settingurl.replace("https://", "") + "?" + paramsstr;
//获得请求签名
string signtext = gethmacsha1sign(secretkey, requesttext);
//这里一定要进行url编码,不然调用api会报错
signtext = httputility.urlencode(signtext, encoding.utf8);
paramsstr = string.format(@"action=pullupload&mediaurl={4}&nonce={0}&secretid={1}&signature={2}&signaturemethod=hmacsha1×tamp={3}&version=2018-07-17",
nonce, secretid, signtext, timestamp, mediaurl);
string resultstr = sendrequest(settingurl, paramsstr);
if (resultstr.indexof("taskid") != -1)
{
newtonsoft.json.linq.jobject jsonobj = newtonsoft.json.linq.jobject.parse(resultstr);
return jsonobj["response"]["taskid"].tostring();
}
return "";
} //pullupload
小结
腾讯云点播服务 api 提供了非常丰富与完善的管理功能列表,在这里我们仅是以满足自身应用需要而提取的常用媒体上传管理功能,更多详情请参照如下链接:
https://cloud.tencent.com/document/product/266/31753
本文代码仅供您参考使用,您可以参照官方文档开发出更加贴合自身需求的应用,感谢您的阅读,希望本文能够对您有所帮助。
发表评论