全部产品
媒体转码

转码

更新时间:2017-07-28 11:36:50   分享:   

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

提交转码作业

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

  1. java
  2. String region = "<region>";
  3. String accessKeyId = "<accessKeyId>";
  4. String accessKeySecret = "<accessKeySecret>";
  5. String pipelineId = "<pipelineId>";
  6. //oss-cn-hangzhou、oss-cn-shanghai、oss-us-west-1等;与region对应
  7. String ossLocation = "<ossLocation>";
  8. String inputObject = "<inputObject>";
  9. String inputBucket = "<inputBucket>";
  10. String outputObject = "<outputObject>";
  11. String outputBucket = "<outputBucket>";
  12. String transcodeTemplateId = "<transcodeTemplateId>";
  13. DefaultProfile profile = DefaultProfile.getProfile(region, accessKeyId, accessKeySecret);
  14. DefaultAcsClient client = new DefaultAcsClient(profile);
  15. SubmitJobsRequest request = new SubmitJobsRequest();
  16. request.setPipelineId(pipelineId);
  17. JSONObject input = new JSONObject();
  18. input.put("Location", ossLocation);
  19. input.put("Bucket", inputBucket);
  20. input.put("Object", inputObject);
  21. request.setInput(input.toJSONString());
  22. JSONArray outputs = new JSONArray();
  23. JSONObject outputConfigJson = new JSONObject();
  24. outputConfigJson.put("TemplateId", transcodeTemplateId);
  25. outputConfigJson.put("OutputObject", outputObject);
  26. outputs.add(outputConfigJson);
  27. request.setOutputs(outputs.toJSONString());
  28. request.setOutputBucket(outputBucket);
  29. request.setOutputLocation(ossLocation);
  30. request.setOutputBucket(outputBucket);
  31. SubmitJobsResponse response=client.getAcsResponse(request);
  32. SubmitJobsResponse.JobResult jobResult = response.getJobResultList().get(0);
  33. if(jobResult.getSuccess()){
  34. System.out.println(String.format("SubmitJob Success, JobId=%s", jobResult.getJob().getJobId()));
  35. }else {
  36. System.out.println(String.format("SubmitJob Failed, RequestId=%s;Code=%s;Message=%s",
  37. response.getRequestId(), jobResult.getCode(), jobResult.getMessage()));
  38. }

取消转码作业

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

  1. java
  2. String region = "<region>";
  3. String accessKeyId = "<accessKeyId>";
  4. String accessKeySecret = "<accessKeySecret>";
  5. String jobIds = "<jobId1,jobId2...>";
  6. //jobId to be cancelled, only Submitted Job can be cancelled
  7. String jobId = "<jobId>";
  8. DefaultProfile profile = DefaultProfile.getProfile(region, accessKeyId, accessKeySecret);
  9. DefaultAcsClient client = new DefaultAcsClient(profile);
  10. CancelJobRequest request = new CancelJobRequest();
  11. request.setJobId(jobId);
  12. CancelJobResponse response = client.getAcsResponse(request);
  13. System.out.println(String.format("JobId:\"%s\" has been canceled", response.getJobId()));

查询转码作业

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

  1. java
  2. String region = "<region>";
  3. String accessKeyId = "<accessKeyId>";
  4. String accessKeySecret = "<accessKeySecret>";
  5. String jobIds = "<jobId1,jobId2...>";
  6. DefaultProfile profile = DefaultProfile.getProfile(region, accessKeyId, accessKeySecret);
  7. DefaultAcsClient client = new DefaultAcsClient(profile);
  8. QueryJobListRequest request = new QueryJobListRequest();
  9. request.setJobIds(jobIds);
  10. QueryJobListResponse response = client.getAcsResponse(request);
  11. for (QueryJobListResponse.Job job : response.getJobList()) {
  12. System.out.println("JobId: " + job.getJobId());
  13. System.out.println("JobStatus: " + job.getState());
  14. if(job.getState().equals("TranscodeSuccess")) {
  15. QueryJobListResponse.Job.Output.OutputFile output_file = job.getOutput().getOutputFile();
  16. System.out.println(String.format("OutputFile OSS url is http://%s.%s.aliyuncs.com/%s",
  17. output_file.getBucket(),
  18. output_file.getLocation(),
  19. URLDecoder.decode(output_file.getObject(), "UTF-8")));
  20. }else if(job.getState().equals("TranscodeFail")){
  21. System.out.println(String.format("Transcode failed, Code is %s, Message is %s",
  22. job.getCode(), job.getMessage()));
  23. }
  24. System.out.println();
  25. }

列出转码作业

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

  1. java
  2. String region = "<region>";
  3. String accessKeyId = "<accessKeyId>";
  4. String accessKeySecret = "<accessKeySecret>";
  5. //UTC时间:YYYY-MM-DDThh:mm:ssZ,eq: 2017-01-10T12:00:00Z , 可空
  6. String startOfJobCreatedTimeRange = "<startOfJobCreatedTimeRange>";
  7. //UTC时间:YYYY-MM-DDThh:mm:ssZ,eq: 2017-02-10T12:00:00Z , 可空
  8. String endOfJobCreatedTimeRange = "<endOfJobCreatedTimeRange>";
  9. long maximumPageSize = 5L;
  10. DefaultProfile profile = DefaultProfile.getProfile(region, accessKeyId, accessKeySecret);
  11. DefaultAcsClient client = new DefaultAcsClient(profile);
  12. ListJobRequest request = new ListJobRequest();
  13. request.setStartOfJobCreatedTimeRange(startOfJobCreatedTimeRange);
  14. request.setEndOfJobCreatedTimeRange(endOfJobCreatedTimeRange);
  15. request.setMaximumPageSize(maximumPageSize);
  16. ListJobResponse response = client.getAcsResponse(request);
  17. if(response.getJobList() != null && !response.getJobList().isEmpty()){
  18. for (ListJobResponse.Job job : response.getJobList()) {
  19. System.out.println("JobId: " + job.getJobId());
  20. System.out.println("JobStatus: " + job.getState());
  21. if(job.getState().equals("TranscodeSuccess")) {
  22. ListJobResponse.Job.Output.OutputFile output_file = job.getOutput().getOutputFile();
  23. System.out.println(String.format("OutputFile OSS url is http://%s.%s.aliyuncs.com/%s",
  24. output_file.getBucket(),
  25. output_file.getLocation(),
  26. URLDecoder.decode(output_file.getObject(), "UTF-8")));
  27. }else if(job.getState().equals("TranscodeFail")){
  28. System.out.println(String.format("Transcode failed, Code is %s, Message is %s",
  29. job.getCode(), job.getMessage()));
  30. }
  31. System.out.println();
  32. }
  33. }
  34. System.out.println("NextPageToken: " + response.getNextPageToken());
本文导读目录
本文导读目录
以上内容是否对您有帮助?