如何經過 RESTful API 玩轉Agora雲錄製

錄製在不少實時音視頻場景中都是必備功能,好比在線教育、金融雙錄、社交直播等。聲網也提供了雲錄製服務,可經過 RESTful API 快速實現錄製。在本文中,咱們簡單來說解其使用方法。php

特別說明:

1,雲錄製目前支持合流錄製,docs.agora.io/cn/cloud-re… 2,參照文檔獲取你聲網帳戶下的CustomerID和Customer Certificate:docs.agora.io/cn/cloud-re… 3,官網文檔:docs.agora.io/cn/cloud-re… 4,錯誤碼:docs.agora.io/cn/cloud-re… 5,restful API回調:docs.agora.io/cn/cloud-re… 6,文檔中的截圖僅供參考java

1,先建立一個用於錄製測試的頻道,頻道內至少包含一個有效發流的客戶端

頻道信息能夠在水晶球中查看: dashboard.agora.io/analytics/c… 水晶球使用說明:docs.agora.io/cn/Agora Pl… 水晶球使用範例:docs.agora.io/cn/Agora Pl…apache

2,獲取recourseId

使用postman請求

官網文檔:docs.agora.io/cn/cloud-re… 特別說明:recourseid必須在五分鐘內使用, 不然將返回400錯誤,返回的sid爲空 請求url示例:api.agora.io/v1/apps//cloud_recording/acquirejson

獲取您的customerID帳戶,請參考文檔: docs.agora.io/cn/cloud-re…api

curl命令 請求示例

使用curl命令時,一個空格,一個字符都不能錯。瀏覽器

$ curl -u <customerID>:<customer certificate> https://api.agora.io/v1/apps/<appid>/cloud_recording/acquire -H "Content-Type:application/json" -d '{"cname":"<頻道號>","uid":"<錄製uid>","clientRequest":{}}'
返回的resourceID
{"resourceId":"RrCi4XQBEUIoCdZLAWdRJLVK4_3ppnaJGghIoDZuC3HGNOb-x2I8AXJj_2evYwExvSkP_WgZVT6V4GtjzWg8-ky5w9CfmAQIFKNDGWzaaWgTTnxZ-E3eEN-SLMFK46M4SlSYMTLYqTgrYRGUgclOgcdGGmhUEZhEcw0UrGxdrbjHuqdah1wgnqj-AJ2OIzyUuvEJQCB9SBRuAfiazmqCY3SjdRc1Q-ywJZzIG27Ub40"}
複製代碼

請求成功的示例截圖bash

php 請求示例

測試環境:在Mac 上面安裝 MAMP 便可restful

<?php
$url = "https://api.agora.io/v1/apps/<your appid>/cloud_recording/acquire"; 
$arr_header[] = "Content-Type:application/json"; 
$arr_header[] = "Authorization: Basic ".base64_encode("<your customerID>:<your customerCertificate>"); //http basic auth 

$data = array('cname'=>'<cname>','uid'=>'<錄製uid>','clientRequest'=>json_decode("{}"));
$data_json = json_encode($data);
echo $data_json;

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url ); //request url
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //print

if(!empty($arr_header)){ 
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_header); 
} 
$response = curl_exec($ch); 
curl_close($ch);
echo $response; 
return json_decode($response); 
?>
複製代碼

在瀏覽器執行response http://localhost:8888/cloudrecordingrequire.phpapp

Java post請求示例(僅供參考)

package io.agora.javaapi;
import java.io.IOException;
import javax.xml.bind.DatatypeConverter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class CloudRecording1 {
public static void main(String[] args) throws ParseException, IOException {
// TODO Auto-generated method stub
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://api.agora.io/v1/apps/<appid>/cloud_recording/acquire");

String encoding = DatatypeConverter.printBase64Binary("<customer ID>:<customer certificate>".getBytes("UTF-8"));
String body = "{\n \"cname\":\"<頻道號>\",\n \"uid\":\"<錄製uid>\",\n \"clientRequest\":{}\n}";

httpPost.setHeader("Content-type", "application/json;charset=utf-8");
httpPost.setHeader("Authorization", "Basic " + encoding);
httpPost.setEntity(new StringEntity(body));

HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity, "utf-8");
System.out.println(content);
System.out.println(httpPost.getURI());
System.out.println(response);
}
}

返回結果

{"resourceId":"RrCi4XQBEUIoCdZLAWdRJLVK4_3ppnaJGghIoDZuC3HGNOb-x2I8AXJj_2evYwEx12wmwz04Ik03j8DKekkisQcKKgHqP2IeFt_NigcILOKdp0320OAX4d-gFHWqWdaP56bhnV-g3tJG-1nQGIjco8pDnAQvPqPYIfbqeq2VHpQloUnpn_TcOyDcEoLTHlVqt6uqq8YOfoMnhX6re-iie2eQyxskhOsyBVIyxdUCPT4"}
https://api.agora.io/v1/apps/<appid>/cloud_recording/acquire
HttpResponseProxy{HTTP/1.1 200 0 [Content-Type: application/json, Content-Length: 253, Connection: keep-alive, X-RateLimit-Limit-second: 10, X-RateLimit-Remaining-second: 9, Accept-Ranges: bytes, Access-Control-Allow-Origin: *, X-Request-ID: 7ed81b0e-a3b6-4251-9628-c46ba37e5db5] ResponseEntityProxy{[Content-Type: application/json,Content-Length: 253,Chunked: false]}}
複製代碼

3,開始錄製

特別說明: 集成測試階段建議使用未開appID認證的app ID, 等跑通以後,再添加token 只使用app ID鑑權時, token傳空便可 「token」:""curl

使用postman 請求

官網文檔:docs.agora.io/cn/cloud-re… api.agora.io/v1/apps//cloud_recording/resourceid//mode/mix/start

https://api.agora.io/v1/apps/<appid>/cloud_recording/resourceid/<resourceid>/mode/mix/start

{
"uid": "<錄製uid>",
"cname": "<頻道號>",
"clientRequest": {
"token": "",
"recordingConfig": {
"maxIdleTime": 30,
"streamTypes": 2,
"audioProfile": 1,
"channelType": 0, 
"videoStreamType": 1, 
"transcodingConfig": {
"height": 640, 
"bitrate": 500, 
"fps": 15, 
"mixedVideoLayout": 1,
"maxResolutionUid": "1",
"width": 360
}
}, 
"storageConfig": {
"accessKey": "<accessKey>",
"region": <region>,
"bucket": "<bucket>",
"secretKey": "<secretKey>",
"vendor": <vendor>
}
}
}
複製代碼

獲取你的customerID帳戶,請參考文檔: docs.agora.io/cn/cloud-re…

4,查詢錄製狀況

使用postman請求

官網文檔:docs.agora.io/cn/cloud-re… api.agora.io/v1/apps//cloud_recording/resourceid//sid//mode/mix/query

5,中止錄製

使用postman 請求

官網文檔:docs.agora.io/cn/cloud-re… api.agora.io/v1/apps//cloud_recording/resourceid//sid//mode/mix/stop

6,雲平臺查看上傳狀況

使用聲網雲錄製支持的雲平臺 以七牛云爲例 涉及雲平臺使用問題,請諮詢雲平臺技術方 m3u8文件,生成外鏈,便可在瀏覽器播放
官網文檔: docs.agora.io/cn/cloud-re… 在線播放錄製文件

相關文章
相關標籤/搜索