saveas方法转存并设置m3u8的ts存储过期时间

最近更新时间:2017-08-16 10:08:51

在调用saveas保存回放时 可以在传数据中多传一个字段expireDays,expireDays可以为 -1 或者 15 天 都行,值为 -1 就是处理结果视频和原始ts都保持hub的过期时间,其他数字就是几天后过期 先看看请求

php 版本

POST /v1/streams/z1.1314xicong.57ea2a3e5e77b03c550ae12c@480p/saveas HTTP/1.1
Host: pili.qiniuapi.com
Accept: */*
Accept-Encoding: deflate, gzip
Content-Type: application/json
User-Agent: pili-sdk-php/1.5.4 curl/7.45.0 PHP/7.0.2
Authorization: Qiniu vI2xPIjOoh7udcRw4GdYNvf3o_gKsCx9wdZaC9u-:MX1wWHKptak6peLrxB5PJIpKRV4=
Content-Length: 70

{"name":"testttkkk","format":"mp4","start":-1,"end":-1,"expireDays":2}

123.059.063.002.00080-192.168.205.017.52985: HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 09 Nov 2016 03:18:24 GMT
Content-Type: application/json
Content-Length: 261
Connection: keep-alive
Access-Control-Allow-Origin: *
X-Log: tss.refs:14:9:2:0xc820505e30;ts.get:1109;ts.lcc:6;body;s.ph;s.put.tw;s.put.tr;s.put.tw;s.put.tr;s.ph;PFDS;PFDS;lc.set:1;rs6_1.sel;rs6_1.ups;qtbl.ups;MQ;RS.not:;mc.s;RS:3;rs.put:4;rs-upload.putFile:6;rs6_1.sel;qtbl.get;RS;PFOPMQ:15;STATUS;PFOP:20;APIS:21;up.pop:23;UP:31;UP:32;m3u8.up:35;PILI:1152
X-Reqid: AT87e4RDf5UiWLPA
X-Reqid: AT87e4RDf5UiWLPA

{"url":"http://hus.echohu.top/recordings/z1.1314xicong.57ea2a3e5e77b03c550ae12c@480p/testttkkk.m3u8","duration":9,"targetUrl":"http://hus.echohu.top/recordings/z1.1314xicong.57ea2a3e5e77b03c550ae12c@480p/testttkkk","persistentId":"z1.582295808a3c0c5edc78fbf8"}

这里就是在post数据中多传了一个字段 "expireDays":2 设置为过期时间为2天后

php-sdkv1 然后在 调用 saveas 的时候,增加 expireDays 的参数 lib/Pili/Api.php 中 streamSaveAs 函数做个调整,增加 expireDays 参数

public static function streamSaveAs($transport, $streamId, $name, $format = NULL, $start = NULL, $end = NULL, $notifyUrl = NULL, $pipeline = NULL, $expireDays = -1)
{
$url = self::_getApiBaseUrl() . "streams/$streamId/saveas";
$params = array(
'name' => $name,
);
if (!empty($format)) {
$params['format'] = $format;
}
if (!empty($start)) {
$params['start'] = $start;
}
if (!empty($end)) {
$params['end'] = $end;
}
if (!empty($notifyUrl)) {
$params['notifyUrl'] = $notifyUrl;
}
if (!empty($pipeline)) {
$params['pipeline'] = $pipeline;
}
if ($expireDays != 0) {
$params['expireDays'] = $expireDays;
}
$body = json_encode($params);
return $transport->send(HttpRequest::POST, $url, $body);
}

2. lib/Pili/Stream.php

public function saveAs($name, $format = NULL, $start = NULL, $end = NULL, $notifyUrl = NULL, $pipeline = NULL, $expireDays = -1)
{
return Api::streamSaveAs($this->_transport, $this->id, $name, $format, $start, $end, $notifyUrl, $pipeline, $expireDays);
}

php v2

代码

$hubName = "huxicongPili";
$mac = new Qiniu\Pili\Mac($ak, $sk);
$client = new Qiniu\Pili\Client($mac);
$hub = $client->hub($hubName);
$streamKey = "qiche";
$stream = $hub->stream($streamKey);
print_r($stream);
try {
 //保存直播数据
//     * PARAM
//      * @fname: 保存的文件名, 不指定会随机生成.
//      * @start: Unix 时间戳, 起始时间, 0 值表示不指定, 则不限制起始时间.
//      * @end: Unix 时间戳, 结束时间, 0 值表示当前时间.
//      * @format: 保存的文件格式, 默认为m3u8.
//      * @pipeline: dora 的私有队列, 不指定则用默认队列.
//      * @notify: 保存成功后的回调地址.
//      * @expireDays: 对应ts文件的过期时间.
// echo "================Get stream\n";
    echo "================Save stream:\n";
    $resp = $stream->saveas(array("fname"=>"1489570493","strart"=>-1  ,"end"=>-1,"expireDays"=>60*30*30));
    print_r($resp);
} catch (\Exception $e) {
    echo "Error:", $e, "\n";
}

请求

100.100.033.186.56448-123.059.063.002.00080: POST /v2/hubs/huxicongPili/streams/cWljaGU=/saveas HTTP/1.1
Host: pili.qiniuapi.com
Accept: */*
Accept-Encoding: deflate, gzip
Content-Type: application/json
User-Agent: pili-sdk-php/2.0.0 curl/7.51.0 PHP/5.6.28
Authorization: Qiniu vI2xPIjOoh7udcRw4GdYNvf3o_gKsCx9wdZaC9u-:K6q4ZlfgR1AbEGCeI1SNkpcNucM=
Content-Length: 62

{"fname":"1489570493","strart":-1,"end":-1,"expireDays":54000}
123.059.063.002.00080-100.100.033.186.56448: HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 04 Jul 2017 07:55:54 GMT
Content-Type: application/json
Content-Length: 27
Connection: keep-alive
X-Log: tss.refs:2:2:37;ts.get:58;ts.lcc:8;body;s.ph;s.put.tw;s.put.tr;s.put.tw;s.put.tr;s.ph;s.put.tw;s.put.tr;s.ph;PFDS;PFDS:1;PFDS:2;lc.set;rs10_4.sel;rs10_4.ups;rwro.ups:1;RS.not:;mc.s;RS:2;rs.put:5;rs-upload.putFile:8;UP:16;m3u8.up:18;PILI:87;PILI-LINA:89
X-Reqid: oBwAAK770Ap9Ec4U
X-Reqid: oBwAAK770Ap9Ec4U

{"fname":"1489570493.m3u8"}

C# 版本

修改源码文件 API.cs

public static SaveAsResponse saveAs(Credentials credentials, string streamId, string fileName, string format, long start, long end, string notifyUrl, string pipleline, int expireDays)
{
    if (streamId == null)
    {
        throw new PiliException(MessageConfig.NULL_STREAM_ID_EXCEPTION_MSG);
    }

    if (!Utils.isArgNotEmpty(fileName))
    {
        throw new PiliException(MessageConfig.ILLEGAL_FILE_NAME_EXCEPTION_MSG);
    }

    if (start < 0 || end < 0 || start > end)
    {
        throw new PiliException(MessageConfig.ILLEGAL_TIME_MSG);
    }

string urlStr = string.Format("{0}/streams/{1}/saveas", API_BASE_URL, streamId);
HttpWebResponse response = null;
JObject json = new JObject();
json.Add("name", fileName);
if (Utils.isArgNotEmpty(notifyUrl))
{
    json.Add("notifyUrl", notifyUrl);
}
    if (start != 0)
    {
        json.Add("start", start);
    }
    if (end != 0)
    {
        json.Add("end", end);
    }
        json.Add("format", format);
    if (pipleline!="")
    {
        json.Add("pipleline", pipleline);
    }
    if (expireDays != 0)
    {
        json.Add("expireDays", expireDays);
    }

注意:此处仅截取部分代码,添加 expireDays 参数即可。

以上内容是否对您有帮助?
  • 提交工单