You need to enable JavaScript to run this app.
导航

媒资上传

最近更新时间2023.10.13 17:01:59

首次发布时间2021.02.23 10:42:38

本文为您提供了服务端 PHP SDK 的媒资上传模块相关的 API 调用示例。主要包含:媒资上传、素材上传、URL 批量拉取上传、查询 URL 上传任务状态等。

注意事项

媒资上传模块的获取上传地址和凭证确认上传的 OpenAPI,目前支持 2 个版本。

  • OpenAPI 的版本号为 2022-01-01

    说明

    上传文件时,要求携带文件后缀。例如,mp4 文件上传,携带 .mp4 或 .MP4。

  • OpenAPI 的版本号为 2020-08-01

初始化

使用前请先完成初始化,参考初始化

签发 UploadAuthToken

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个UploadAuthToken,您可以循环调用生成方法。UploadAuthToken用于客户端上传,详见客户端上传

<?php
require('../vendor/autoload.php');

use Volc\Service\Vod\Vod;

$client = Vod::getInstance();
// call below method if you dont set ak and sk in ~/.vcloud/config
// $client->setAccessKey($ak);
// $client->setSecretKey($sk);

$expire = 60; // 请求的签名有效期

echo "\nSTS2鉴权签名\n";
$space = "";
$response = $client->getUploadVideoAuthWithExpiredTime($expire);
echo json_encode($response);

echo "\nSTS2鉴权签名,过期时间默认1小时\n";
$vid = "";
$response = $client->getUploadVideoAuth();
echo json_encode($response);

媒资上传

接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证确认上传

<?php
require('../../vendor/autoload.php');

use VolcServiceVodModelsRequestVodUploadMediaRequest;
use VolcServiceVodModelsResponseVodCommitUploadInfoResponse;
use VolcServiceVodUploadFunctions;
use VolcServiceVodUploadVodUpload;
use VolcServiceVodUploadOptionInfo;

$client = VodUpload::getInstance();
$client->setAccessKey('your ak');
$client->setSecretKey('your sk');

$space = 'your space';
$filePath = "file path";

$functions = new Functions();
$functions->addGetMetaFunc();
$functions->addSnapshotTimeFunc(2.1);
$optionInfo = new OptionInfo();
$optionInfo->IsHlsIndexOnly = true;
$functions->addOptionInfoFunc($optionInfo);
$functions = $functions->getFunctionsString();

$request = new VodUploadMediaRequest();
$request->setSpaceName($space);
$request->setFilePath($filePath);
$request->setFunctions($functions);
$request->setCallbackArgs("my callback");
$request->setFileName("hello/vod.mp4");
$request->setStorageClass(0);

$response = new VodCommitUploadInfoResponse();
try {
    $response = $client->uploadMedia($request);
} catch (Exception $e) {
    echo $e, "
";
} catch (Throwable $e) {
    echo $e, "
";
}
if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError()->serializeToJsonString(), "
";
}
echo $response->serializeToJsonString();
echo "
";

if ($response->getResult() != null) {
    echo $response->getResult()->getData()->getVid(), "
";
    echo $response->getResult()->getData()->getPosterUri(), "
";
    echo $response->getResult()->getData()->getSourceInfo()->getWidth(), "
";
    echo $response->getResult()->getData()->getSourceInfo()->getHeight(), "
";
}


        

素材上传

接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证确认上传

<?php
require('../../vendor/autoload.php');

use VolcServiceVodModelsRequestVodUploadMaterialRequest;
use VolcServiceVodModelsResponseVodCommitUploadInfoResponse;
use VolcServiceVodUploadFunctions;
use VolcServiceVodUploadVodUpload;
use VolcServiceVodUploadOptionInfo;

$client = VodUpload::getInstance();
$client->setAccessKey('your ak');
$client->setSecretKey('your sk');

$space = 'your space';
$filePath = "file path";

//音视频素材上传
$functions = new Functions();
$functions->addGetMetaFunc();
$functions->addSnapshotTimeFunc(2.1);

$optionInfo = new OptionInfo();
$optionInfo->Title = '素材测试视频';
$optionInfo->Tags = 'test';
$optionInfo->Description = '素材测试,视频文件';
$optionInfo->Category = 'video';
$optionInfo->RecordType = 2;
$optionInfo->Format = 'mp4';
$functions->addOptionInfoFunc($optionInfo);

$functions = $functions->getFunctionsString();

$request = new VodUploadMaterialRequest();
$request->setSpaceName($space);
$request->setFilePath($filePath);
$request->setFunctions($functions);
$request->setFileType("media");

$request->setCallbackArgs("my callback");

$response = new VodCommitUploadInfoResponse();
try {
    $response = $client->uploadMaterial($request);
} catch (Exception $e) {
    echo $e, "
";
} catch (Throwable $e) {
    echo $e, "
";
}
if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError(), "
";
}
echo $response->serializeToJsonString();
echo "
";

if ($response->getResult() != null) {
    echo $response->getResult()->getData()->getMid(), "
";
    echo $response->getResult()->getData()->getSourceInfo()->getWidth(), "
";
    echo $response->getResult()->getData()->getSourceInfo()->getHeight(), "
";
}

////图片素材上传
//$functions = new Functions();
//$functions->addGetMetaFunc();
//
//$optionInfo = new OptionInfo();
//$optionInfo->Title = '素材测试图片';
//$optionInfo->Tags = 'test';
//$optionInfo->Description = '素材测试,图片文件';
//$optionInfo->Category = 'image';
//$optionInfo->RecordType = 2;
//$optionInfo->Format = 'jpg';
//$functions->addOptionInfoFunc($optionInfo);
//
//$functions = $functions->getFunctionsString();
//
//$request = new VodUploadMaterialRequest();
//$request->setSpaceName($space);
//$request->setFilePath($filePath);
//$request->setFunctions($functions);
//$request->setFileType("image");
//$request->setFileName("hello/vod/image");
//
//$request->setCallbackArgs("my callback");
//
//$response = new VodCommitUploadInfoResponse();
//try {
//    $response = $client->uploadMaterial($request);
//} catch (Exception $e) {
//    echo $e, "
";
//} catch (Throwable $e) {
//    echo $e, "
";
//}
//if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
//    echo $response->getResponseMetadata()->getError(), "
";
//}
//echo $response->serializeToJsonString();
//echo "
";
//
//if ($response->getResult() != null) {
//    echo $response->getResult()->getData()->getMid(), "
";
//    echo $response->getResult()->getData()->getSourceInfo()->getWidth(), "
";
//    echo $response->getResult()->getData()->getSourceInfo()->getHeight(), "
";
//}

////字幕素材上传
//$functions = new Functions();
//$functions->addGetMetaFunc();
//
//$optionInfo = new OptionInfo();
//$optionInfo->Title = '素材测试字幕';
//$optionInfo->Tags = 'test';
//$optionInfo->Description = '素材测试,字幕文件';
//$optionInfo->Category = 'subtitle';
//$optionInfo->RecordType = 2;
//$optionInfo->Format = 'vtt';
//$functions->addOptionInfoFunc($optionInfo);
//
//$functions = $functions->getFunctionsString();
//
//$request = new VodUploadMaterialRequest();
//$request->setSpaceName($space);
//$request->setFilePath($filePath);
//$request->setFunctions($functions);
//$request->setFileType("object");
//$request->setFileName("hello/vod/object");
//
//$request->setCallbackArgs("my callback");
//
//$response = new VodCommitUploadInfoResponse();
//try {
//    $response = $client->uploadMaterial($request);
//} catch (Exception $e) {
//    echo $e, "
";
//} catch (Throwable $e) {
//    echo $e, "
";
//}
//if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
//    echo $response->getResponseMetadata()->getError(), "
";
//}
//echo $response->serializeToJsonString();
//echo "
";
//
//if ($response->getResult() != null) {
//    echo $response->getResult()->getData()->getMid(), "
";
//    echo $response->getResult()->getData()->getSourceInfo()->getSize(), "
";
//}


        

URL 批量拉取上传

接口请求参数和返回参数详见 OpenAPI:URL 批量拉取上传

<?php
require('../../vendor/autoload.php');

$client = Volc\Service\Vod\Vod::getInstance();
$client->setAccessKey('your ak');
$client->setSecretKey('your sk');

$request = new Volc\Service\Vod\Models\Request\VodUrlUploadRequest();
$request->setSpaceName("your SpaceName");

$urlSet = new Volc\Service\Vod\Models\Business\VodUrlUploadURLSet();
$urlSet->setSourceUrl("");
$urlSet->setStorageClass(0);
$urlSet->setFileExtension(".mp4");
$urlSet->setCallbackArgs("");
$customHeaders = ['your header key' => "your header value"]; // 自定义 Header,业务希望访问源视频 URL 携带的 Header(例如User-Agent)可以通过该参数传入,非必须字段
$urlSet->setCustomURLHeaders($customHeaders);
$request->setURLSets([$urlSet]);


$response = new Volc\Service\Vod\Models\Response\VodUrlUploadResponse();
try {
    $response = $client->uploadMediaByUrl($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response != null && $response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError()->serializeToJsonString(), "\n";
} else {
    echo $response->serializeToJsonString(), "\n";
}

查询 URL 上传任务状态

接口请求参数和返回参数详见OpenAPI:查询 URL 批量上传任务状态

<?php
require('../vendor/autoload.php');

use Volc\Service\Vod\Models\Business\VodURLSet;
use Volc\Service\Vod\Models\Request\VodQueryUploadTaskInfoRequest;
use Volc\Service\Vod\Models\Response\VodQueryUploadTaskInfoResponse;
use Volc\Service\Vod\Vod;

$client = Vod::getInstance();
$client->setAccessKey('your ak');
$client->setSecretKey('your sk');

$jobId = 'url jobId';
$jobIds = [$jobId];


$request = new VodQueryUploadTaskInfoRequest();
$request->setJobIds(implode(",", $jobIds));

$response = new VodQueryUploadTaskInfoResponse();
try {
    $response = $client->queryUploadTaskInfo($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response->getResponseMetadata()->getError() != null) {
    print_r($response->getResponseMetadata()->getError());
}
echo $response->serializeToJsonString();
echo "\n";

$mediaInfo = new VodURLSet();
$mediaInfo = $response->getResult()->getData()->getMediaInfoList()[0];
echo $mediaInfo->getRequestId(), "\n";
echo $mediaInfo->getState(), "\n";