本文提供了Java SDK转码的操作步骤及完整的代码示例。

转码相关API调用及参数详细信息请参见提交转码作业

  1. 创建AcsClient实例。
    DefaultProfile profile = DefaultProfile.getProfile(
                 mpsRegionId,      // 地域ID
                 accessKeyId,      // RAM账号的AccessKey ID
                 accessKeySecret); // RAM账号Access Key Secret
    IAcsClient client = new DefaultAcsClient(profile);
  2. 创建request,并设置参数。
    SubmitJobsRequest request = new SubmitJobsRequest();
  3. 转码参数。
    说明
    • 通过SDK提交转码作业时Object需经URLEncode,否则会导致转码失败。更多信息,请参见URL Encoding说明
    • 请按照规范填写文件名称,否则会找不到文件导致转码失败。详细名称规范,请参见参数详情
    • Input
      JSONObject input = new JSONObject();
      input.put("Location", ossLocation);
      input.put("Bucket", ossBucket);
      input.put("Object", URLEncoder.encode(ossInputObject, "utf-8").replace("+", "%20"));
      request.setInput(input.toJSONString());
    • Output
      String outputOSSObject = URLEncoder.encode(ossInputObject, "utf-8").replace("+", "%20");
      JSONObject output = new JSONObject();
      output.put("OutputObject", outputOSSObject);
      • Container
        JSONObject container = new JSONObject();
        container.put("Format", "mp4");
        output.put("Container", container.toJSONString());
      • Video
        说明 如果使用转码模板进行转码则无需配置此参数。如果配置此参数,则覆盖指定转码模板中的对应参数。
        JSONObject video = new JSONObject();
        video.put("Codec", "H.264");
        video.put("Bitrate", "1500");
        video.put("Width", "1280");
        video.put("Fps", "25");
        output.put("Video", video.toJSONString());
      • Audio
        说明 如果使用转码模板进行转码则无需配置此参数。如果配置此参数,则覆盖指定转码模板中的对应参数。
        JSONObject audio = new JSONObject();
        audio.put("Codec", "AAC");
        audio.put("Bitrate", "128");
        audio.put("Channels", "2");
        audio.put("Samplerate", "44100");
        output.put("Audio", audio.toJSONString());
      • TemplateId
        output.put("TemplateId", templateId);
    • PipelineId
      说明 MPS通过管道绑定主题或队列接收消息通知。如需接收消息通知,请先设置。详细操作请参见设置消息通知
      request.setPipelineId(pipelineId);
  4. 发起API请求并显示返回值。
    SubmitJobsResponse response;
    response = client.getAcsResponse(request);
    System.out.println("RequestId is:"+response.getRequestId());
    if (response.getJobResultList().get(0).getSuccess()) {
     System.out.println("JobId is:" + response.getJobResultList().get(0).getJob().getJobId());
    } else {
     System.out.println("SubmitJobs Failed code:" + response.getJobResultList().get(0).getCode() +
                        " message:" + response.getJobResultList().get(0).getMessage());
    }
完整代码
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.mts.model.v20140618.*;
public class SimpleTranscode {
    private static String accessKeyId = "xxx";
    private static String accessKeySecret = "xxx";
    private static String mpsRegionId = "cn-hangzhou";
    private static String pipelineId = "xxx";
    private static String templateId = "S00000001-200010";
    private static String ossLocation = "oss-cn-hangzhou";
    private static String ossBucket = "xxx";
    private static String ossInputObject = "input.mp4";
    private static String ossOutputObject = "output.mp4";
    public static void main(String[] args) {
        // 创建DefaultAcsClient实例并初始化
        DefaultProfile profile = DefaultProfile.getProfile(
                mpsRegionId,      // 地域ID
                accessKeyId,      // RAM账号的AccessKey ID
                accessKeySecret); // RAM账号Access Key Secret
        IAcsClient client = new DefaultAcsClient(profile);
        // 创建API请求并设置参数
        SubmitJobsRequest request = new SubmitJobsRequest();
        // Input
        JSONObject input = new JSONObject();
        input.put("Location", ossLocation);
        input.put("Bucket", ossBucket);
        try {
            input.put("Object", URLEncoder.encode(ossInputObject, "utf-8").replace("+", "%20"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("input URL encode failed");
        }
        request.setInput(input.toJSONString());
        // Output
        String outputOSSObject;
        try {
            outputOSSObject = URLEncoder.encode(ossOutputObject, "utf-8").replace("+", "%20");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("output URL encode failed");
        }
        JSONObject output = new JSONObject();
        output.put("OutputObject", outputOSSObject);
        // Ouput->Container
        JSONObject container = new JSONObject();
        container.put("Format", "mp4");
        output.put("Container", container.toJSONString());
        // Ouput->Video
        JSONObject video = new JSONObject();
        video.put("Codec", "H.264");
        video.put("Bitrate", "1500");
        video.put("Width", "1280");
        video.put("Fps", "25");
        output.put("Video", video.toJSONString());
        // Ouput->Audio
        JSONObject audio = new JSONObject();
        audio.put("Codec", "AAC");
        audio.put("Bitrate", "128");
        audio.put("Channels", "2");
        audio.put("Samplerate", "44100");
        output.put("Audio", audio.toJSONString());
        // Ouput->TemplateId
        output.put("TemplateId", templateId);
        JSONArray outputs = new JSONArray();
        outputs.add(output);
        request.setOutputs(outputs.toJSONString());
        request.setOutputBucket(ossBucket);
        request.setOutputLocation(ossLocation);
        // PipelineId
        request.setPipelineId(pipelineId);
        // 发起请求并处理应答或异常
        SubmitJobsResponse response;
        try {
            response = client.getAcsResponse(request);
            System.out.println("RequestId is:"+response.getRequestId());
            if (response.getJobResultList().get(0).getSuccess()) {
                System.out.println("JobId is:" + response.getJobResultList().get(0).getJob().getJobId());
            } else {
                System.out.println("SubmitJobs Failed code:" + response.getJobResultList().get(0).getCode() +
                                   " message:" + response.getJobResultList().get(0).getMessage());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}