全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网
媒体转码

转码

更新时间:2017-07-28 11:41:32

对存储于OSS上的视频文件,经过转码获得适合在PC、TV以及移动终端上播放的文件。提供一系列转码接口 ,比如SubmitJobs,CancelJob、QueryJobList、ListJob。

提交转码作业

批量提交转码作业接口,一个转码输出会生成一个转码作业,接口返回转码作业列表。

  1. php
  2. $region = '<region>';
  3. $accessKeyId = '<accessKeyId>';
  4. $accessKeySecret = '<accessKeySecret>';
  5. $pipelineId = '<pipelineId>';
  6. #oss-cn-hangzhou、oss-cn-shanghai、oss-us-west-1等;与region对应
  7. $ossLocation='<ossLocation>';
  8. $inputObject='<inputObject>';
  9. $inputBucket='<inputBucket>';
  10. $outputObject='<outputObject>';
  11. $outputBucket='<outputBucket>';
  12. $transcodeTemplateId='<transcodeTemplateId>';
  13. $profile = DefaultProfile::getProfile($region, $accessKeyId,$accessKeySecret);
  14. $client = new DefaultAcsClient($profile);
  15. $inputFile = array(
  16. 'Location' => $ossLocation,
  17. 'Bucket' => $inputBucket,
  18. 'Object' => urlencode($inputObject));
  19. $outputs = array();
  20. $outputs[] = array(
  21. 'OutputObject'=> urlencode($outputObject),
  22. 'TemplateId' => $transcodeTemplateId,
  23. );
  24. $request = new Mts\SubmitJobsRequest();
  25. $request->setAcceptFormat('JSON');
  26. $request->setInput(json_encode($inputFile));
  27. $request->setOutputBucket($outputBucket);
  28. $request->setOutputLocation($ossLocation);
  29. $request->setOUtputs(json_encode($outputs));
  30. $request->setPipelineId($pipelineId);
  31. $response = $client->getAcsResponse($request);
  32. print 'job_id is ' + $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'};

取消转码作业

对于状态处于”已提交”的作业,可以调用CancelJob接口取消作业。

  1. $region = '<region>';
  2. $accessKeyId = '<accessKeyId>';
  3. $accessKeySecret = '<accessKeySecret>';
  4. #only job in Submitted state can be cancelled
  5. $jobId = '<jobId>';
  6. $profile = DefaultProfile::getProfile($region, $accessKeyId,
  7. $accessKeySecret);
  8. $client = new DefaultAcsClient($profile);
  9. $request = new Mts\CancelJobRequest();
  10. $request->setAcceptFormat('JSON');
  11. $request->setJobId($jobId);
  12. try {
  13. $client->getAcsResponse($request);
  14. print 'Cancel Job \"'.$jobId.'\" Success';
  15. }catch (ClientException $clientException){
  16. print 'Cancel Job \"'.$jobId.'\" failed, errorCode is '.$clientException->getErrorCode().', errorMessage is '.$clientException->getErrorMessage();
  17. }catch (ServerException $serverException){
  18. print 'Cancel Job \"'.$jobId.'\" failed, errorCode is '.$serverException->getErrorCode().', errorMessage is '.$serverException->getErrorMessage();
  19. }

查询转码作业

通过转码作业ID列表,调用QueryJobList接口批量查询作业信息。

  1. php
  2. $region = '<region>';
  3. $accessKeyId = '<accessKeyId>';
  4. $accessKeySecret = '<accessKeySecret>';
  5. #jobId split by ',' ,at most ten jobId
  6. $jobIds='<jobId1,jobId2,jobId3...>';
  7. $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
  8. $client = new DefaultAcsClient($profile);
  9. $request = new Mts\QueryJobListRequest();
  10. $request->setAcceptFormat('JSON');
  11. $request->setJobIds($jobIds);
  12. $response = $client->getAcsResponse($request);
  13. print 'Result Job List Info:\n';
  14. $jobs = $response->JobList->Job;
  15. foreach ($jobs as $job) {
  16. print("JobId: " + $job->JobId);
  17. print ("JobStatus: " + $job->State);
  18. if ($job->State == "TranscodeSuccess") {
  19. print 'Transcode success, the target file url is http://' .
  20. $job->Output->OutputFile->Bucket . '.' .
  21. $job->Output->OutputFile->Location. '.aliyuncs.com/' .
  22. urldecode($job->Output->OutputFile->Object);
  23. }elseif ($job->State == "TranscodeFail"){
  24. print "Transcode Failed, Code is " .$job->Code.", Message is ".$job->Message;
  25. }
  26. echo '\n';
  27. }

列出转码作业

通过作业状态、创建时间区间、转码管道,调用ListJob接口列出转码作业(按CreationTime降序排列)。

  1. php
  2. $region = '<region>';
  3. $accessKeyId = '<accessKeyId>';
  4. $accessKeySecret = '<accessKeySecret>';
  5. #UTC date format: YYYY-MM-DDThh:mm:ssZ, eg:2017-02-10T12:00:00Z, optional
  6. $startOfJobCreatedTimeRange = '<startOfJobCreatedTimeRange>';
  7. #UTC date format: YYYY-MM-DDThh:mm:ssZ, eg:2017-02-10T12:00:00Z, optional
  8. $endOfJobCreatedTimeRange = '<endOfJobCreatedTimeRange>';
  9. #optional, default 10, rang:[1,100]
  10. $maximumPageSize = 5;
  11. $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
  12. $client = new DefaultAcsClient($profile);
  13. $request = new Mts\ListJobRequest();
  14. $request->setAcceptFormat('JSON');
  15. $request->setStartOfJobCreatedTimeRange($startOfJobCreatedTimeRange);
  16. $request->setEndOfJobCreatedTimeRange($endOfJobCreatedTimeRange);
  17. $response = $client->getAcsResponse($request);
  18. print 'Result Job List Info:\n';
  19. $jobs = $response->JobList->Job;
  20. foreach ($jobs as $job) {
  21. print("JobId: " + $job->JobId);
  22. print ("JobStatus: " + $job->State);
  23. if ($job->State == "TranscodeSuccess") {
  24. print 'Transcode success, the target file url is http://' .
  25. $job->Output->OutputFile->Bucket . '.' .
  26. $job->Output->OutputFile->Location. '.aliyuncs.com/' .
  27. urldecode($job->Output->OutputFile->Object);
  28. }elseif ($job->State == "TranscodeFail"){
  29. print "Transcode Failed, Code is " .$job->Code.", Message is ".$job->Message;
  30. }
  31. echo '\n';
  32. }
  33. print 'NextPageToken :'.$response.NextPageToken.'\n';
本文导读目录