ブランドの認知度を高め、著作権を保護し、製品の認知度を高めるために、会社のロゴやテレビ局のロゴなどの表示されるウォーターマークを動画に追加できます。ApsaraVideo Media Processing (MPS) は、[画像ウォーターマーク]、[アニメーションウォーターマーク]、[テストウォーターマーク]などの種類のウォーターマークを提供しています。ビジネス要件に基づいてウォーターマークの種類を選択できます。このトピックでは、ウォーターマークテンプレートの作成、テキストウォーターマークジョブの送信、画像ウォーターマークジョブの送信など、ウォーターマークを管理するために MPS Java SDK にカプセル化されている API 操作を呼び出す方法の例を示します。
前提条件
SDK クライアントが初期化されていること。詳細については、「クライアントの初期化」をご参照ください。
AddWaterMarkTemplate: ウォーターマークテンプレートの作成
ウォーターマークテンプレートは、ウォーターマークのサイズと位置を決定するパラメーターなど、複数のパラメーターの設定を指定します。ウォーターマークテンプレートを使用すると、ウォーターマーク管理を簡素化できます。 AddWaterMarkTemplate 操作を呼び出して、ウォーターマークテンプレートを作成できます。
ウォーターマークテンプレートは画像ウォーターマークにのみ適用可能で、テキストウォーターマークには適用できません。
ウォーターマークテンプレートは、位置やサイズなどのウォーターマーク属性のみを指定し、ウォーターマークコンテンツは指定しません。ウォーターマークジョブを送信するときに、ウォーターマークコンテンツを指定する必要があります。
この操作の呼び出しが成功すると、ウォーターマークテンプレート ID が返されます。 [MPS コンソール] でもウォーターマークテンプレートを作成し、テンプレート ID を取得できます。詳細については、「ウォーターマークテンプレートの管理」をご参照ください。
「リソース「WatermarkTemplate」のクォータが使い果たされました」というエラーメッセージが返された場合は、ウォーターマークテンプレートのクォータが使い果たされています。この場合は、チケットを起票してクォータを増やすことができます。
/**
* Create a watermark template.
* @return
* @throws Exception
*/
public static void addWaterMarkTemplate() throws Exception {
com.aliyun.mts20140618.Client client = WaterMark.createClient();
// For more information about watermark parameters, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a#section-k53-tt4-8b0.
JSONObject waterMarkConfig = new JSONObject();
waterMarkConfig.put("Dx","10");
waterMarkConfig.put("Dy","5");
waterMarkConfig.put("ReferPos","TopRight");
com.aliyun.mts20140618.models.AddWaterMarkTemplateRequest addWaterMarkTemplateRequest = new com.aliyun.mts20140618.models.AddWaterMarkTemplateRequest()
// The name of the watermark.
.setName("Name")
// The job output configuration.
.setConfig(waterMarkConfig.toJSONString());
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Write your own code to display the response of the API operation if necessary.
client.addWaterMarkTemplateWithOptions(addWaterMarkTemplateRequest, runtime);
} catch (TeaException error) {
// In real-world business scenarios, handle exceptions carefully and avoid ignoring them in your project. The exceptions in this example are for illustrative purposes only.
// The error message.
System.out.println(error.getMessage());
// The URL for troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// In real-world business scenarios, handle exceptions carefully and avoid ignoring them in your project. The exceptions in this example are for illustrative purposes only.
// The error message.
System.out.println(error.getMessage());
// The URL for troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}SubmitJobs: ウォーターマーク ジョブの送信
動画にウォーターマークを追加すると、動画の画像が変更されるため、動画を再エンコードする必要があります。 MPS は、ウォーターマーク ジョブを送信するための SubmitJobs 操作を提供しています。
操作を呼び出してジョブを送信する場合は、ウォーターマークとして使用するファイルへのパスで URL エンコーディングを実行する必要があります。そうしないと、ジョブは失敗します。詳細については、「URL エンコーディング」をご参照ください。
ジョブの実行時にファイルが見つかるように、有効なファイル名を指定する必要があります。詳細については、「パラメーターの詳細」をご参照ください。
送信済みジョブの ID を記録することをお勧めします。これは、後続の操作に役立ちます。
テキストウォーターマーク ジョブの送信
/**
* テキストウォーターマーク ジョブを送信する。
* @return
* @throws Exception
*/
public static void submitTextWaterMarkJobs() throws Exception {
com.aliyun.mts20140618.Client client = WaterMark.createClient();
// ウォーターマークの出力設定を構成する。
JSONArray waterMarks = new JSONArray(); // ウォーターマーク配列は最大 4 つのウォーターマークで構成されます。これは、ストリームに最大 4 つのウォーターマークを含めることができることを意味します。
// テキストウォーターマーク。
JSONObject textWaterMarks = new JSONObject();
textWaterMarks.put("WaterMarkTemplateId","<your waterMarkTemplateId>");
textWaterMarks.put("Type","Text");
// Content パラメーターにウォーターマークとして使用するテキストを指定します。指定されたテキストは Base64 でエンコードする必要があります。
textWaterMarks.put("TextWaterMark","{\"Content\":\"5rWL6K+V5paH5a2X5rC05Y2w\",\"FontName\":\"SimSun\",\"FontSize\":\"16\",\"Top\":2,\"Left\":10}");
waterMarks.add(textWaterMarks);
com.aliyun.mts20140618.models.SubmitJobsRequest submitJobsRequest = new com.aliyun.mts20140618.models.SubmitJobsRequest()
// ジョブ入力。
.setInput("{\"Bucket\":\"exampleBucket\",\"Location\":\"oss-cn-shanghai\",\"Object\":\"example.flv\",\"Referer\": \"Object Storage Service (OSS) コンソールでホットリンク保護機能を有効にするために設定したパラメーター\"}")
// ジョブ出力構成。
.setOutputs("[{\"OutputObject\":\"exampleOutput.mp4\",\"TemplateId\":\"6181666213ab41b9bc21da8ff5ff****\",\"WaterMarks\":" + waterMarks.toJSONString() + ",\"UserData\":\"testid-001\"}]")
// 出力ファイルが格納される OSS バケット。
.setOutputBucket("exampleBucket")
// OSS バケットが存在するリージョン。
.setOutputLocation("oss-cn-shanghai")
// MPS キューの ID。
.setPipelineId("dd3dae411e704030b921e52698e5****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// 必要に応じて、API 操作のレスポンスを表示するための独自のコードを記述します。
client.submitJobsWithOptions(submitJobsRequest, runtime);
} catch (TeaException error) {
// 実際のビジネスシナリオでは、例外を慎重に処理し、プロジェクトで例外を無視しないでください。この例の例外は説明のみを目的としています。
// エラーメッセージ。
System.out.println(error.getMessage());
// トラブルシューティング用の URL。
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());
// トラブルシューティング用の URL。
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}画像ウォーターマーク
/**
* 画像ウォーターマーク ジョブを送信する。
* @return
* @throws Exception
*/
public static void submitImageWaterMarkJobs() throws Exception {
com.aliyun.mts20140618.Client client = WaterMark.createClient();
// ウォーターマークの出力設定を構成する。
JSONArray waterMarks = new JSONArray(); // ウォーターマーク配列は最大 4 つのウォーターマークで構成されます。これは、ストリームに最大 4 つのウォーターマークを含めることができることを意味します。
// 画像ウォーターマーク。
JSONObject imageWaterMarks = new JSONObject();
imageWaterMarks.put("WaterMarkTemplateId","<your waterMarkTemplateId>");
imageWaterMarks.put("Type","Image");
// 画像またはアニメーションウォーターマークの幅と高さ。
imageWaterMarks.put("Width","200");
imageWaterMarks.put("Height","100");
// ウォーターマークとして使用する画像へのパス。
JSONObject logoFile = new JSONObject();
logoFile.put("Bucket","<your bucket name>");
logoFile.put("Location","oss-cn-shanghai");
// ビジネス要件に基づいて、静的 PNG 画像、アニメーション PNG 画像、MOV ファイル、または GIF ファイルを指定できます。アニメーション PNG 画像のファイル名拡張子は apng である必要があります。ウォーターマークとして使用する画像が非静的画像の場合は、ファイル名拡張子を小文字にする必要があります。
logoFile.put("Object", URLEncoder.encode("Dynamic logo.apng", "utf-8"));
imageWaterMarks.put("InputFile",logoFile.toJSONString());
waterMarks.add(imageWaterMarks);
com.aliyun.mts20140618.models.SubmitJobsRequest submitJobsRequest = new com.aliyun.mts20140618.models.SubmitJobsRequest()
// ジョブ入力。
.setInput("{\"Bucket\":\"exampleBucket\",\"Location\":\"oss-cn-shanghai\",\"Object\":\"example.flv\",\"Referer\": \"Object Storage Service (OSS) コンソールでホットリンク保護機能を有効にするために設定したパラメーター\"}")
// ジョブ出力構成。
.setOutputs("[{\"OutputObject\":\"exampleOutput.mp4\",\"TemplateId\":\"6181666213ab41b9bc21da8ff5ff****\",\"WaterMarks\":" + waterMarks.toJSONString() + ",\"UserData\":\"testid-001\"}]")
// 出力ファイルが格納される OSS バケット。
.setOutputBucket("exampleBucket")
// OSS バケットが存在するリージョン。
.setOutputLocation("oss-cn-shanghai")
// MPS キューの ID。
.setPipelineId("dd3dae411e704030b921e52698e5****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// 必要に応じて、API 操作のレスポンスを表示するための独自のコードを記述します。
client.submitJobsWithOptions(submitJobsRequest, runtime);
} catch (TeaException error) {
// 実際のビジネスシナリオでは、例外を慎重に処理し、プロジェクトで例外を無視しないでください。この例の例外は説明のみを目的としています。
// エラーメッセージ。
System.out.println(error.getMessage());
// トラブルシューティング用の URL。
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());
// トラブルシューティング用の URL。
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}サンプルコード
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.tea.TeaException;
import java.net.URLEncoder;
public class WaterMark {
/**
* <b>description</b> :
* <p>Use your AccessKey pair to initialize the client.</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()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-shanghai.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
/**
* Create a watermark template.
* @return
* @throws Exception
*/
public static void addWaterMarkTemplate() throws Exception {
com.aliyun.mts20140618.Client client = WaterMark.createClient();
// For more information about watermark parameters, visit https://www.alibabacloud.com/help/zh/mps/developer-reference/parameter-details.
JSONObject waterMarkConfig = new JSONObject();
waterMarkConfig.put("Dx","10");
waterMarkConfig.put("Dy","5");
waterMarkConfig.put("ReferPos","TopRight");
com.aliyun.mts20140618.models.AddWaterMarkTemplateRequest addWaterMarkTemplateRequest = new com.aliyun.mts20140618.models.AddWaterMarkTemplateRequest()
// The name of the watermark.
.setName("Name")
// The job output configuration.
.setConfig(waterMarkConfig.toJSONString());
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Write your own code to display the response of the API operation if necessary.
client.addWaterMarkTemplateWithOptions(addWaterMarkTemplateRequest, runtime);
} catch (TeaException error) {
// In real-world business scenarios, handle exceptions carefully and avoid ignoring them in your project. The exceptions in this example are for illustrative purposes only.
// The error message.
System.out.println(error.getMessage());
// The URL for troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// In real-world business scenarios, handle exceptions carefully and avoid ignoring them in your project. The exceptions in this example are for illustrative purposes only.
// The error message.
System.out.println(error.getMessage());
// The URL for troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
/**
* Submit a text watermark job.
* @return
* @throws Exception
*/
public static void submitTextWaterMarkJobs() throws Exception {
com.aliyun.mts20140618.Client client = WaterMark.createClient();
// Configure the output settings of watermarks.
JSONArray waterMarks = new JSONArray(); // A watermark array consists of up to four watermarks. This means that a stream can contain a maximum of four watermarks.
// Text watermarks.
JSONObject textWaterMarks = new JSONObject();
textWaterMarks.put("WaterMarkTemplateId","<your waterMarkTemplateId>");
textWaterMarks.put("Type","Text");
// Specify the text to be used as watermarks for the Content parameter. The specified text must be encoded in Base64.
textWaterMarks.put("TextWaterMark","{\"Content\":\"5rWL6K+V5paH5a2X5rC05Y2w\",\"FontName\":\"SimSun\",\"FontSize\":\"16\",\"Top\":2,\"Left\":10}");
waterMarks.add(textWaterMarks);
com.aliyun.mts20140618.models.SubmitJobsRequest submitJobsRequest = new com.aliyun.mts20140618.models.SubmitJobsRequest()
// The job input.
.setInput("{\"Bucket\":\"exampleBucket\",\"Location\":\"oss-cn-shanghai\",\"Object\":\"example.flv\",\"Referer\": \"The parameter that you set in the Object Storage Service (OSS) console to enable the hotlink protection feature\"}")
// The job output configuration.
.setOutputs("[{\"OutputObject\":\"exampleOutput.mp4\",\"TemplateId\":\"6181666213ab41b9bc21da8ff5ff****\",\"WaterMarks\":" + waterMarks.toJSONString() + ",\"UserData\":\"testid-001\"}]")
// The OSS bucket in which the output file is stored.
.setOutputBucket("exampleBucket")
// The region in which the OSS bucket resides.
.setOutputLocation("oss-cn-shanghai")
// The ID of the MPS queue.
.setPipelineId("dd3dae411e704030b921e52698e5****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Write your own code to display the response of the API operation if necessary.
client.submitJobsWithOptions(submitJobsRequest, runtime);
} catch (TeaException error) {
// In real-world business scenarios, handle exceptions carefully and avoid ignoring them in your project. The exceptions in this example are for illustrative purposes only.
// The error message.
System.out.println(error.getMessage());
// The URL for troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// In real-world business scenarios, handle exceptions carefully and avoid ignoring them in your project. The exceptions in this example are for illustrative purposes only.
// The error message.
System.out.println(error.getMessage());
// The URL for troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
/**
* Submit an image watermark job.
* @return
* @throws Exception
*/
public static void submitImageWaterMarkJobs() throws Exception {
// 省略
}
public static void main(String[] args_) throws Exception {
// ウォーターマークテンプレートを作成する。
WaterMark.addWaterMarkTemplate();
// 画像ウォーターマーク ジョブを送信する。
//WaterMark.submitImageWaterMarkJobs();
// テキストウォーターマーク ジョブを送信する。
//WaterMark.submitTextWaterMarkJobs();
}
}