このトピックでは、ApsaraVideo Media Processing (MPS) SDK for Javaを使用してメディアファイルをトランスコードする方法について説明します。 このトピックでは、完全なサンプルコードも提供します。

トランスコードのAPI操作と操作のパラメーターの詳細については、「SubmitJobs」をご参照ください。

  1. AcsClient インスタンスの作成
    DefaultProfile profile = DefaultProfile.getProfile(
                 mpsRegionId, // MPSサービスを使用するリージョンのID。
                 accessKeyId, // RAMユーザーのAccessKey ID。
                 accessKeySecret); // RAMユーザーのAccessKeyシークレット。
    IAcsClient client = new DefaultAcsClient(profile);
  2. リクエストを作成し、必要なパラメーターを設定します。
    SubmitJobsRequest request = new SubmitJobsRequest();
  3. トランスコードパラメーターを設定します。
    • 入力
      JSONObject input = new JSONObject();
      input.put("Location" 、ossLocation);
      input.put("バケット" 、ossBucket);
      input.put("オブジェクト", URLEncoder.encode(ossInputObject, "utf-8"));
      request.setInput(input.toJSONString());
    • Output
      文字列outputOSSObject = URLEncoder.encode(ossOutputObject、"utf-8");
      JSONObject output = new JSONObject();
      output.put("OutputObject", outputOSSObject);
      • コンテナー
        JSONObjectコンテナ=new JSONObject();
        container.put("フォーマット" 、"mp4");
        output.put("Container", container.toJSONString());
      • ビデオ
        JSONObject video = new JSONObject();
        video.put("コーデック" 、"H.264");
        video.put("ビットレート" 、"1500");
        video.put("幅" 、"1280");
        video.put("Fps" 、"25");
        output.put("Video", video.toJSONString());
      • Audio
        JSONObject audio = new JSONObject();
        audio.put("コーデック" 、"AAC");
        audio.put("ビットレート" 、"128");
        audio.put("チャンネル" 、"2");
        audio.put("Samplerate" 、"44100");
        output.put("Audio", audio.toJSONString());
      • TemplateId
        output.put("TemplateId", templateId);
    • PipelineId
      request.setPipelineId(pipelineId);
  4. リクエストを送信し、応答または例外を処理します。
    SubmitJobsResponse応答;
    response = client.getAcsResponse (リクエスト);
    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());
    }
サンプルコード
java.io.UnsupportedEncodingExceptionをインポートします。impor t java.net.URLEncoder;
com.alibaba.fastjson.JSONArrayをインポートします。com.alibaba.fastjson.JSONObjectをインポートします。com.aliyuncs.profile.DefaultProfileをインポートします。com.aliyuncs.DefaultAcsClientをインポートします。com.aliyuncs.IAcsClientをインポートします。com.aliyuncs.exceptions.ClientExceptionをインポートします。com.aliyuncs.exceptions.ServerExceptionをインポートします。com.aliyuncs.mts.mo del.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, // MPSサービスを使用するリージョンのID。
                accessKeyId, // RAMユーザーのAccessKey ID。
                accessKeySecret); // RAMユーザーのAccessKeyシークレット。
        IAcsClient client = new DefaultAcsClient(profile);
        // リクエストを作成し、必要なパラメーターを設定します。
        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"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("input URL encode failed");
        }
        request.setInput(input.toJSONString());
        // Output
        String outputOSSObject;
        try {
            outputOSSObject = URLEncoder.encode(ossOutputObject, "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("output URL encode failed");
        }
        JSONObject output = new JSONObject();
        output.put("OutputObject", outputOSSObject);
        // コンテナ
        JSONObject container = new JSONObject();
        container.put("Format", "mp4");
        output.put("Container", container.toJSONString());
        // ビデオ
        JSONObject video = new JSONObject();
        video.put("コーデック" 、"H.264");
        video.put("Bitrate", "1500");
        video.put("Width", "1280");
        video.put("Fps", "25");
        output.put("Video", video.toJSONString());
        // オーディオ
        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);
        JSONArray outputs = new JSONArray();
        outputs.add(output);
        request.setOutputs(outputs.toJSONString());
        request.setOutputBucket(ossBucket);
        request.setOutputLocation(ossLocation);
        // PipelineId
        request.setPipelineId(pipelineId);
        // Send the request and handle the response or exception.
        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();
        }
    }
}