保利威文档中心

幫助中心

取得直播中影片片段的介面

更新時間:2023-11-14 14:07:19

介面URL

https://api.polyv.net/live/v2/channel/recordFile/{channelId}/streamRecordIndex

介面說明

1、取得目前直播頻道直播中的影片片段
2、介面支援https協定

支援格式

JSON

請求方式

GET or POST

請求數限制

TRUE

請求參數

參數名 必選 類型 說明
appId string 從API設定中取得,在直播系統登記的appId
timestamp string 目前13位毫秒級時間戳,3分鐘內有效
startTime string 影片片段的開始時間,格式為yyyy-MM-dd HH:mm:ss
endTime string 影片片段的結束時間,格式為yyyy-MM-dd HH:mm:ss
fileName string 轉存點播的檔案名稱,不儲存預設影片名稱為"精彩片段"
cataId long 轉存點播存放的目錄ID
sign String 簽名,為32位大寫的MD5值,產生簽名的appSecret金鑰作為通訊資料安全的關鍵資訊,嚴禁儲存在用戶端直接使用,所有API都必須透過客戶自己伺服器轉發呼叫POLYV伺服器取得回應資料【詳見簽名產生規則

回應成功JSON範例:

{
  "code": 200,
  "status": "success",
  "message": "",
  "data": "8205ac89d3f0c634988ef97f528aa745_8"
}

appId不正確

{
    "code": 400,
    "status": "error",
    "message": "application not found.",
    "data": ""
}

時間戳錯誤

{
    "code": 400,
    "status": "error",
    "message": "invalid timestamp.",
    "data": ""
}

簽名錯誤

{
    "code": 403,
    "status": "error",
    "message": "invalid signature.",
    "data": ""
}

找不到該頻道

{
    "code": 400,
    "status": "error",
    "message": "channel not found.",
    "data": ""
}

查詢開始時間或查詢結束時間為空

{
    "code": 400,
    "status": "error",
    "message": "startTime or endTime not found.",
    "data": ""
}

開始時間或查詢結束時間非法

{
    "code": 400,
    "status": "error",
    "message": "startTime or endTime valid.",
    "data": ""
}

頻道暫無此功能,請聯絡客服人員

{
    "code": 403,
    "status": "error",
    "message": "該頻道暫不支援此api功能",
    "data": ""
}

轉存失敗,請重試,如果一直不成功,請及時檢視PLYVO帳號的空間以及套餐過期時間情況,並聯絡客服

{
    "code": 403,
    "status": "error",
    "message": "轉存失敗,請重試",
    "data": ""
}

欄位說明

參數名 說明
code 請求狀態回應碼
status 請求狀態
message 錯誤資訊
data 成功將返回影片轉存點播的vid

PHP請求範例

<?php

include 'config.php';
//接口需要的参数(非sign)赋值
$channelId = "108888";

$startTime = "2018-04-16 09:33:33";
$endTime = "2018-04-16 09:44:30";

$params = array(
 'appId'=> $appId,
 'timestamp'=> $timestamp,
 'startTime'=> $startTime,
 'endTime'=> $endTime,
  );
//生成sign
$sign = getSign($params); //详细查看config.php文件的getSign方法
//接口请求url
$url = "https://api.polyv.net/live/v2/channel/recordFile/$channelId/streamRecordIndex?appId=$appId&timestamp=$timestamp&sign=$sign&startTime=$startTime&endTime=$endTime";

//输出接口请求结果 
echo file_get_contents($url);

?>

JAVA請求範例

import com.live.util.EncryptionUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.*;

public class TestStreamIndex {
    // 尽量把时间设置较长
    private RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(15000)
            .setConnectionRequestTimeout(60000).build();

    private static final String APP_ID = "xxxx";
    private static final String APP_SECRET = "xxxxxxxxxxxx";
    private static final String STREAM_RECORD_INDEX_URL = "http://api.polyv.net/live/v2/channel/recordFile/{param}/streamRecordIndex";

    public static void main(String[] args) {
        TestStreamIndex demo = new TestStreamIndex();
        int channelId = 183730;
        String start = "2018-04-17 10:54:50";
        String end = "2018-04-17 11:05:00";
        String fileName = "我的直播片段";
        long cataId = 1;
        demo.streamRecordIndexDemo(channelId, start, end, fileName, cataId);
    }

    private void streamRecordIndexDemo(int channelId, String start, String end, String fileName, long cataId) {
        Map<String, String> params = new HashMap<>();
        params.put("appId", APP_ID);
        params.put("startTime", start);
        params.put("endTime", end);
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        params.put("fileName", fileName);
        params.put("cataId", String.valueOf(cataId));
        params.put("sign", this.generateSign(params, APP_SECRET));

        String result = sendHttpPost(getRealApiUrl(STREAM_RECORD_INDEX_URL, String.valueOf(channelId)), params);
        System.out.println("stream record index result=" + result);
    }

    private String getRealApiUrl(String url, String param){
        return url.replace("{param}", param);
    }

    public String sendHttpPost(String httpUrl, Map<String, String> maps) {
        HttpPost httpPost = new HttpPost(httpUrl);
        List<NameValuePair> nameValuePairs = new ArrayList<>();
        for (String key : maps.keySet()) {
            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
        }
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sendHttpPost(httpPost);
    }

    private String sendHttpPost(HttpPost httpPost) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        String responseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            httpPost.setConfig(requestConfig);
            response = httpClient.execute(httpPost);
            entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseContent;
    }


    /**
     * 根据map里的参数构建加密串
     * @param map
     * @param secretKey
     * @return
     */
    protected String generateSign(Map<String, String> map, String secretKey) {
        Map<String, String> params = this.paraFilter(map);
        // 处理参数,计算MD5哈希值
        String concatedStr = this.concatParams(params);
        String plain = secretKey + concatedStr + secretKey;
        String encrypted = EncryptionUtils.md5Hex(plain);

        // 32位大写MD5值
        return encrypted.toUpperCase();
    }

    /**
     * 对params根据key来排序并且以key1=value1&key2=value2的形式拼接起来
     * @param params
     * @return
     */
    private String concatParams(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            sb.append(key).append(value);
        }
        return sb.toString();
    }

    /**
     * 除去数组中的空值和签名参数
     * @param sArray 签名参数组
     * @return 去掉空值与签名参数后的新签名参数组
     */
    private Map<String, String> paraFilter(Map<String, String> sArray) {
        Map<String, String> result = new HashMap<String, String>();
        if (sArray == null || sArray.size() <= 0) {
            return result;
        }
        for (String key : sArray.keySet()) {
            String value = sArray.get(key);
            if (value == null || value.equals("") || key.equalsIgnoreCase("sign")
                    || key.equalsIgnoreCase("sign_type")) {
                continue;
            }
            result.put(key, value);
        }
        return result;
    }

}
联系客服,在线咨询