ApsaraVideo Media Processing (MPS) のプリセットテンプレートが要件を満たさない場合は、エンコード形式、フレームレート、解像度などのパラメーターを指定して、カスタムトランスコードテンプレートを作成できます。次の Java SDK V2.0 の例では、トランスコーディングテンプレートの追加、更新、削除、クエリの方法を示します。
前提条件
開始する前に、クライアントを初期化してください。詳細については、「初期化」をご参照ください。
テンプレートの追加
AddTemplate API を呼び出してテンプレートを追加します。
説明
-
テンプレートの追加時に
The resource "Template" quota has been used upエラーが返される場合、トランスコーディングテンプレートのクォータが上限に達しています。クォータの引き上げを依頼するには、チケットを送信してください。 -
テンプレートを追加した後、返されたテンプレート ID を記録してください。また、コンソールでトランスコーディングテンプレートを作成および管理することもできます。詳細については、「トランスコーディングテンプレート」をご参照ください。
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>アクセスキーペアを使用してクライアントを初期化します。</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必須。ALIBABA_CLOUD_ACCESS_KEY_ID 環境変数が設定されていることを確認してください。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必須。ALIBABA_CLOUD_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.AddTemplateRequest addTemplateRequest = new com.aliyun.mts20140618.models.AddTemplateRequest()
// テンプレートの名前。
.setName("mps-example")
// コンテナ。
.setContainer("{\"Format\":\"mp4\"}")
// ビデオストリームの設定。
.setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10s\"}")
// オーディオストリームの設定。
.setAudio("{\"Codec\":\"aac\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}")
// 一般的なトランスコーディング設定。
.setTransConfig("{\"TransMode\":\"onepass\"}")
// 多重化設定。
.setMuxConfig("{\"Segment\":{\"Duration\":\"10\"}}");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// コードをコピーして実行する場合は、API の戻り値を出力します。
client.addTemplateWithOptions(addTemplateRequest, runtime);
} catch (TeaException error) {
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
テンプレートの更新
UpdateTemplate API を呼び出してトランスコーディングテンプレートを更新します。
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>アクセスキーペアを使用してクライアントを初期化します。</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必須。ALIBABA_CLOUD_ACCESS_KEY_ID 環境変数が設定されていることを確認してください。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必須。ALIBABA_CLOUD_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.UpdateTemplateRequest updateTemplateRequest = new com.aliyun.mts20140618.models.UpdateTemplateRequest()
// テンプレート ID。
.setTemplateId("16f01ad6175e4230ac42bb5182cd****")
// テンプレートの名前。
.setName("MPS-example")
// コンテナ。
.setContainer("{\"Format\":\"mp4\"}")
// ビデオストリームの設定。
.setVideo("{\"Codec\":\"H.264\",\"Profile\":\"high\",\"Bitrate\":\"500\",\"Crf\":\"15\",\"Width\":\"256\",\"Height\":\"800\",\"Fps\":\"25\",\"Gop\":\"10s\"}")
// オーディオストリームの設定。
.setAudio("{\"Codec\":\"aac\",\"Samplerate\":\"44100\",\"Bitrate\":\"500\",\"Channels\":\"2\"}")
// 多重化設定。
.setMuxConfig("{\"Segment\":{\"Duration\":\"10\"}}")
// 一般的なトランスコーディング設定。
.setTransConfig("{\"TransMode\":\"onepass\"}");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// コードをコピーして実行する場合は、API の戻り値を出力します。
client.updateTemplateWithOptions(updateTemplateRequest, runtime);
} catch (TeaException error) {
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
テンプレートの削除
DeleteTemplate API を呼び出してトランスコーディングテンプレートを削除します。
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>アクセスキーペアを使用してクライアントを初期化します。</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必須。ALIBABA_CLOUD_ACCESS_KEY_ID 環境変数が設定されていることを確認してください。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必須。ALIBABA_CLOUD_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.DeleteTemplateRequest deleteTemplateRequest = new com.aliyun.mts20140618.models.DeleteTemplateRequest()
// 削除するカスタムトランスコードテンプレートの ID。
.setTemplateId("16f01ad6175e4230ac42bb5182cd****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// コードをコピーして実行する場合は、API の戻り値を出力します。
client.deleteTemplateWithOptions(deleteTemplateRequest, runtime);
} catch (TeaException error) {
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
IDによるテンプレートのクエリ
QueryTemplateList API を呼び出して ID でテンプレートをクエリします。
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>アクセスキーペアを使用してクライアントを初期化します。</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必須。ALIBABA_CLOUD_ACCESS_KEY_ID 環境変数が設定されていることを確認してください。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必須。ALIBABA_CLOUD_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.QueryTemplateListRequest queryTemplateListRequest = new com.aliyun.mts20140618.models.QueryTemplateListRequest()
// クエリ対象のテンプレート ID のリスト。
.setTemplateIds("16f01ad6175e4230ac42bb5182cd****,88c6ca184c0e424d5w5b665e2a12****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// コードをコピーして実行する場合は、API の戻り値を出力します。
client.queryTemplateListWithOptions(queryTemplateListRequest, runtime);
} catch (TeaException error) {
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
ステータスによるテンプレートのクエリ
SearchTemplate API を呼び出してステータスに基づいてテンプレートをクエリします。
// このファイルは自動生成されたものです。編集しないでください。
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description :
* <p>アクセスキーペアを使用してクライアントを初期化します。</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必須。ALIBABA_CLOUD_ACCESS_KEY_ID 環境変数が設定されていることを確認してください。
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// 必須。ALIBABA_CLOUD_ACCESS_KEY_SECRET 環境変数が設定されていることを確認してください。
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-qingdao.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.mts20140618.Client client = Sample.createClient();
com.aliyun.mts20140618.models.SearchTemplateRequest searchTemplateRequest = new com.aliyun.mts20140618.models.SearchTemplateRequest()
// 検索対象のトランスコーディングテンプレートのステータス。
.setState("Normal")
// 検索対象とするテンプレート名のプレフィックス。
.setNamePrefix("S00000001")
// ページングクエリ結果の 1 ページあたりのエントリ数。
.setPageSize(10L)
// 現在のページ番号。
.setPageNumber(1L);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// コードをコピーして実行する場合は、API の戻り値を出力します。
client.searchTemplateWithOptions(searchTemplateRequest, runtime);
} catch (TeaException error) {
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// これはデモ用です。例外は慎重に処理してください。プロジェクトで例外を無視しないでください。
// エラーメッセージ
System.out.println(error.getMessage());
// 診断アドレス
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}