edit-icon download-icon

Transcoding

Last Updated: Apr 19, 2018

Procedure

  1. Create AcsClient instance.

    1. DefaultProfile profile = DefaultProfile.getProfile(
    2. mpsRegionId, // regionID
    3. accessKeyId, // AccessKey ID of RAM account
    4. accessKeySecret); // Access Key Secret of RAM account
    5. IAcsClient client = new DefaultAcsClient(profile);
  2. Create request, and set parameters.

    1. SubmitJobsRequest request = new SubmitJobsRequest();
  3. Transcoding parameters.

    • Input

      1. JSONObject input = new JSONObject();
      2. input.put("Location", ossLocation);
      3. input.put("Bucket", ossBucket);
      4. input.put("Object", URLEncoder.encode(ossInputObject, "utf-8"));
      5. request.setInput(input.toJSONString());
    • Output

      1. String outputOSSObject = URLEncoder.encode(ossOutputObject, "utf-8");
      2. JSONObject output = new JSONObject();
      3. output.put("OutputObject", outputOSSObject);
      • Container

        1. JSONObject container = new JSONObject();
        2. container.put("Format", "mp4");
        3. output.put("Container", container.toJSONString());
      • Video

        1. JSONObject video = new JSONObject();
        2. video.put("Codec", "H.264");
        3. video.put("Bitrate", "1500");
        4. video.put("Width", "1280");
        5. video.put("Fps", "25");
        6. output.put("Video", video.toJSONString());
      • Audio

        1. JSONObject audio = new JSONObject();
        2. audio.put("Codec", "AAC");
        3. audio.put("Bitrate", "128");
        4. audio.put("Channels", "2");
        5. audio.put("Samplerate", "44100");
        6. output.put("Audio", audio.toJSONString());
      • TemplateId

        1. output.put("TemplateId", templateId);
    • PipelineId

      1. request.setPipelineId(pipelineId);
  4. Initiate API request and display returned value.

    1. SubmitJobsResponse response;
    2. response = client.getAcsResponse(request);
    3. System.out.println("RequestId is:"+response.getRequestId());
    4. if (response.getJobResultList().get(0).getSuccess()) {
    5. System.out.println("JobId is:" + response.getJobResultList().get(0).getJob().getJobId());
    6. } else {
    7. System.out.println("SubmitJobs Failed code:" + response.getJobResultList().get(0).getCode() +
    8. " message:" + response.getJobResultList().get(0).getMessage());
    9. }

Full codes

  1. import java.io.UnsupportedEncodingException;
  2. import java.net.URLEncoder;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.aliyuncs.profile.DefaultProfile;
  6. import com.aliyuncs.DefaultAcsClient;
  7. import com.aliyuncs.IAcsClient;
  8. import com.aliyuncs.exceptions.ClientException;
  9. import com.aliyuncs.exceptions.ServerException;
  10. import com.aliyuncs.mts.model.v20140618.*;
  11. public class SimpleTranscode {
  12. private static String accessKeyId = "xxx";
  13. private static String accessKeySecret = "xxx";
  14. private static String mpsRegionId = "cn-hangzhou";
  15. private static String pipelineId = "xxx";
  16. private static String templateId = "S00000001-200010";
  17. private static String ossLocation = "oss-cn-hangzhou";
  18. private static String ossBucket = "xxx";
  19. private static String ossInputObject = "input.mp4";
  20. private static String ossOutputObject = "output.mp4";
  21. public static void main(String[] args) {
  22. // Create DefaultAcsClient instance and complete initialization
  23. DefaultProfile profile = DefaultProfile.getProfile(
  24. mpsRegionId, // region ID
  25. accessKeyId, // AccessKey ID of RAM account
  26. accessKeySecret); // Access Key Secret of RAM account
  27. IAcsClient client = new DefaultAcsClient(profile);
  28. // Create API request and set parameters
  29. SubmitJobsRequest request = new SubmitJobsRequest();
  30. // Input
  31. JSONObject input = new JSONObject();
  32. input.put("Location", ossLocation);
  33. input.put("Bucket", ossBucket);
  34. try {
  35. input.put("Object", URLEncoder.encode(ossInputObject, "utf-8"));
  36. } catch (UnsupportedEncodingException e) {
  37. throw new RuntimeException("input URL encode failed");
  38. }
  39. request.setInput(input.toJSONString());
  40. // Output
  41. String outputOSSObject;
  42. try {
  43. outputOSSObject = URLEncoder.encode(ossOutputObject, "utf-8");
  44. } catch (UnsupportedEncodingException e) {
  45. throw new RuntimeException("output URL encode failed");
  46. }
  47. JSONObject output = new JSONObject();
  48. output.put("OutputObject", outputOSSObject);
  49. // Ouput->Container
  50. JSONObject container = new JSONObject();
  51. container.put("Format", "mp4");
  52. output.put("Container", container.toJSONString());
  53. // Ouput->Video
  54. JSONObject video = new JSONObject();
  55. video.put("Codec", "H.264");
  56. video.put("Bitrate", "1500");
  57. video.put("Width", "1280");
  58. video.put("Fps", "25");
  59. output.put("Video", video.toJSONString());
  60. // Ouput->Audio
  61. JSONObject audio = new JSONObject();
  62. audio.put("Codec", "AAC");
  63. audio.put("Bitrate", "128");
  64. audio.put("Channels", "2");
  65. audio.put("Samplerate", "44100");
  66. output.put("Audio", audio.toJSONString());
  67. // Ouput->TemplateId
  68. output.put("TemplateId", templateId);
  69. JSONArray outputs = new JSONArray();
  70. outputs.add(output);
  71. request.setOutputs(outputs.toJSONString());
  72. request.setOutputBucket(ossBucket);
  73. request.setOutputLocation(ossLocation);
  74. // PipelineId
  75. request.setPipelineId(pipelineId);
  76. // Initiate request and handle response or exceptions
  77. SubmitJobsResponse response;
  78. try {
  79. response = client.getAcsResponse(request);
  80. System.out.println("RequestId is:"+response.getRequestId());
  81. if (response.getJobResultList().get(0).getSuccess()) {
  82. System.out.println("JobId is:" + response.getJobResultList().get(0).getJob().getJobId());
  83. } else {
  84. System.out.println("SubmitJobs Failed code:" + response.getJobResultList().get(0).getCode() +
  85. " message:" + response.getJobResultList().get(0).getMessage());
  86. }
  87. } catch (ServerException e) {
  88. e.printStackTrace();
  89. } catch (ClientException e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. }
Thank you! We've received your feedback.