阿里云视频点播之URL批量拉取上传(调整为多个视频上传)
项目引入阿里云视频点播PHP-SDK
背景:2021年乐视云的点播将停止提供服务,项目决定选择选用阿里云的视频的点播。在上线前,需要将之前的视频提前导入资源库,URLS方式拉取是比较方便的,对编辑同事来说操作简单,而且乐视云提供视频下载url,下面是对案例进行的调整
URL批量拉取上传(调整为多个视频上传)
阿里云PHP-SDK文档示例内容如下:点击地址进入
调用UploadMediaByURL接口,完成URL批量拉取上传功能。
接口参数和返回字段请参见UploadMediaByURL。调用示例如下:
/**
* URL批量拉取上传
* @param client 发送请求客户端
* @return UploadMediaByURLResponse URL批量拉取上传响应数据
*/
function uploadMediaByURL($client) {
$request = new vod\UploadMediaByURLRequest();
$url = "http://192.168.0.0/16/***.mp4";
$request->setUploadURLs($url);
$uploadMetadataList = array();
$uploadMetadata = array();
$uploadMetadata["SourceUrl"] = $url;
$uploadMetadata["Title"] = "upload by url sample";
$uploadMetadataList[] = $uploadMetadata;
$request->setUploadMetadatas(json_encode($uploadMetadataList));
return $client->getAcsResponse($request);
}
try {
$client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
$result = uploadMediaByURL($client);
var_dump($result);
} catch (Exception $e) {
print $e->getMessage()."\n";
}
根据阿里云VOD的api文档,点击地址进入。
对于UploadURLs这个参数的描述是:
URL中需要包括扩展名, 比如https://****.mp4中mp4为扩展名。
如果URL中不包含扩展名,可以在UploadMetadatas中传入FileExtension来指定。
如果URL中有扩展名且同时传入FileExtension,以传入的FileExtension为准。
指定支持的扩展名,请参见上传概述。
URL编码,多个地址以英文逗号(,)分隔,最多支持20个。
避免存在特殊字符导致无法上传视频,需要URL编码后再做逗号拼接。
所以我们将示例代码进行下面的调整:
/**
* URL批量拉取上传
* @param client 发送请求客户端
* @return UploadMediaByURLResponse URL批量拉取上传响应数据
*/
function uploadMediaByURL($client) {
$request = new vod\UploadMediaByURLRequest();
$urlDates = [];
$uploadMetadataList = array();
$uploadUrls = '';
foreach ($urlDates as $urlDate){
$url = urlencode($urlDate['url']);
$uploadMetadata = array();
$uploadMetadata["SourceUrl"] = $url;
$uploadMetadata["Title"] = $urlDate['video_name'];
$uploadMetadata["CateId"] = 123;
$uploadMetadata["FileExtension"] = pathinfo($url, PATHINFO_EXTENSION);
$uploadUrls .= $url.',';
$uploadMetadataList[] = $uploadMetadata;
}
$uploadUrls = rtrim($uploadUrls, ",");
$request->setUploadURLs($uploadUrls);
$request->setUploadMetadatas(json_encode($uploadMetadataList));
return $client->getAcsResponse($request);
}
try {
$client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
$result = uploadMediaByURL($client,);
var_dump($result);
} catch (Exception $e) {
print $e->getMessage()."\n";
}
相信这时候你已经明白了,我自己集成的类里的方法如下,供大家再参考下:
/**
* URL批量拉取上传
* @param client 发送请求客户端
* @return UploadMediaByURLResponse URL批量拉取上传响应数据
*/
private function uploadMediaByURL($client,$urlDates) {
$request = new vod\UploadMediaByURLRequest();
$uploadMetadataList = array();
$uploadUrls = '';
foreach ($urlDates as $urlDate){
$url = urlencode($urlDate['url']);
$uploadMetadata = array();
$uploadMetadata["SourceUrl"] = $url;
$uploadMetadata["Title"] = $urlDate['video_name'];
$uploadMetadata["CateId"] = *****;
$uploadMetadata["FileExtension"] = pathinfo($url, PATHINFO_EXTENSION);
$uploadUrls .= $url.',';
$uploadMetadataList[] = $uploadMetadata;
}
$uploadUrls = rtrim($uploadUrls, ",");
$request->setUploadURLs($uploadUrls);
$request->setUploadMetadatas(json_encode($uploadMetadataList));
return $client->getAcsResponse($request);
}
public function uploadMediaByURLResult($urlDates)
{
try {
$client = $this->initVodClient();
$result = $this->uploadMediaByURL($client,$urlDates);
return $result;
} catch (Exception $e) {
print $e->getMessage()."\n";
}
}
欢迎大家讨论,有好的优化大家可以在评论区提出来。
转载请标注来源https://blog.csdn.net/weixin_42557545/article/details/111482420