このトピックでは、ビデオ透かしモジュールのAPI操作の使用例を示します。 API操作は、ApsaraVideo VOD SDK for Javaにカプセル化されています。 API操作を呼び出して、透かしを作成、変更、削除、およびクエリできます。
クライアントを初期化
SDKを使用する前に、クライアントを初期化してください。 For more information, see Initialization.
透かしを作成する
You can call the AddWatermark operation to create a watermark.
この操作のリクエストパラメーターとレスポンスパラメーターの詳細については、「AddWatermark」をご参照ください。 サンプルコード:
アップロードURLと資格情報を照会する方法の詳細については、「CreateUploadAttachedMedia」をご参照ください。
OSS (Object Storage Service) バケットに透かしファイルをアップロードする方法の詳細については、「OSSオブジェクトのアップロード」をご参照ください。
com.aliyuncs.vod.mo del.v20170321.AddWatermarkRequestをインポートします。com.aliyuncs.vod.mo del.v20170321.AddWatermarkResponseをインポートします。/**
* 透かしを作成します。
* /
public static AddWatermarkResponse addWatermark(DefaultAcsClientクライアント) throws Exception {
AddWatermarkRequestリクエスト=新しいAddWatermarkRequest();
// 透かしの名前。
request.setName("addwatermark");
// OSSバケットに保存されている透かしファイルのURL。
String fileUrl = "http:// example-bucket-**** .oss-cn-shanghai.aliyuncs.com/watermark/test****.png";
// 画像透かしを作成する場合は、透かしファイルのURLを指定する必要があります。 透かしファイルを保存するOSSバケットは、ビデオが保存されているリージョンと同じリージョンに存在する必要があります。 たとえば、ビデオが中国 (上海) リージョンに保存されている場合、ビデオは同じリージョンに保存されている透かしファイルのみを使用できます。
request.setFileUrl(fileUrl);
// 透かしの設定。
JSONObject watermarkConfig = null;
// 画像透かしの位置設定。
watermarkConfig = buildImageWatermarkConfig();
// テキスト透かしの位置設定。
// watermarkConfig = buildTextWatermarkConfig();
request.setWatermarkConfig(watermarkConfig.toJSONString());
// 透かしのタイプ。 テキスト透かしの場合はText、画像透かしの場合はImageに設定します。
request.setType("イメージ");
return client.getAcsResponse (リクエスト);
}
/**
* 呼び出し例
* @ param args
* @ throws ClientException
* /
public static void main(String[] args) throws ClientException {
DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
AddWatermarkResponseレスポンス=新しいAddWatermarkResponse();
try {
// 透かしを作成します。
応答=addWatermark (クライアント);
// 透かしのID。
System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
// 透かしの位置と効果の設定。
System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
// 透かしファイルのURL。 テキスト透かしを作成する場合は、このパラメーターを空のままにします。
System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
} catch (Exception e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}
/**
* 画像透かしのパラメータを構築します。 必要に応じてパラメーターを設定します。
* @ return
* /
public static JSONObject buildImageWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// 出力ビデオの透かしの水平オフセット。
watermarkConfig.put("Dx" 、"8");
// The vertical offset of the watermark on the output video.
watermarkConfig.put("Dy", "8");
// 出力ビデオの透かしの幅。
watermarkConfig.put("幅" 、"55");
// 出力ビデオの透かしの高さ。
watermarkConfig.put("高さ" 、"55");
// The approximate position of the watermark relative to the output video. 透かしは、左上隅、右上隅、左下隅、または右下隅にあります。
watermarkConfig.put("ReferPos" 、"BottomRight");
// 透かし表示のタイムライン。 透かし表示の開始時間と期間を指定できます。
JSONObjectタイムライン=new JSONObject();
// 透かし表示の開始時刻。
timeline.put("スタート" 、"2");
// 透かし表示の期間。
timeline.put("期間" 、"ToEND");
watermarkConfig.put("Timeline", timeline);
return watermarkConfig;
}
/**
* Build the parameters of a text watermark. 必要に応じてパラメーターを設定します。
* @ return
* /
public static JSONObject buildTextWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// 透かしとして使用するテキスト。
watermarkConfig.put("Content", "testwatermark");
// The font to use for the watermark.
watermarkConfig.put("FontName", "SimSun");
// 透かしに使用するフォントサイズ。
watermarkConfig.put("FontSize", 25);
// 透かしに使用するフォントの色。 You can specify a color name or RGB color code, such as #000000.
watermarkConfig.put("FontColor", "Black");
// The transparency to use for the watermark.
watermarkConfig.put("FontAlpha", "0.2");
// The stroke color to use for the watermark. You can specify a color name or RGB color code, such as #ffffff.
watermarkConfig.put("BorderColor", "White");
// The stroke width to use for the watermark.
watermarkConfig.put("BorderWidth" 、1);
// The distance between the upper-left vertex of the watermark and the upper edge of the output video.
watermarkConfig.put("Top", 20);
// The distance between the upper-left vertex of the watermark and the left edge of the output video.
watermarkConfig.put("Left" 、15);
return watermarkConfig;
}
透かしの変更
UpdateWatermark操作を呼び出して、透かしを変更できます。
この操作のリクエストパラメーターとレスポンスパラメーターの詳細については、「UpdateWatermark」をご参照ください。 サンプルコード:
com.aliyuncs.vod.mo del.v20170321.UpdateWatermarkRequestをインポートします。com.aliyuncs.vod.mo del.v20170321.UpdateWatermarkResponseをインポートします。/**
* 透かしを変更します。
* 注: 画像透かしのファイルURLは変更できません。 If you want to modify the URL, you must create another image watermark.
* /
public static UpdateWatermarkResponse updateWatermark(DefaultAcsClientクライアント) が例外 {をスロー
UpdateWatermarkRequest request = new UpdateWatermarkRequest();
request.setName("updatetermark");
// The ID of the watermark for which you want to modify the configurations.
request.setWatermarkId("421ddddd4f6e734a526fd2****");
// 透かしの設定。
JSONObject watermarkConfig = null;
// The position configurations of an image watermark.
// watermarkConfig = buildImageWatermarkConfig();
// The position configurations of a text watermark.
watermarkConfig = buildTextWatermarkConfig();
request.setWatermarkConfig(watermarkConfig.toJSONString());
return client.getAcsResponse (リクエスト);
}
/**
* 呼び出し例
* /
public static void main(String[] args) {
DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
UpdateWatermarkResponseレスポンス=new UpdateWatermarkResponse();
try {
response = updateWatermark(client);
// The ID of the watermark.
System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
// The position and effect configurations of the watermark.
System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
// The URL of the watermark file. If you want to modify a text watermark, leave this parameter empty.
System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
} catch (Exception e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}
/**
* 画像透かしのパラメータを構築します。 必要に応じてパラメーターを設定します。
* @ return
* /
public static JSONObject buildImageWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// 出力ビデオの透かしの水平オフセット。
watermarkConfig.put("Dx" 、"10");
// 出力ビデオの透かしの垂直オフセット。
watermarkConfig.put("Dy" 、"10");
// 出力ビデオの透かしの幅。
watermarkConfig.put("幅" 、"66");
// 出力ビデオの透かしの高さ。
watermarkConfig.put("高さ" 、"66");
// 出力ビデオに対するウォーターマークのおおよその位置。 透かしは、左上隅、右上隅、左下隅、または右下隅にあります。
watermarkConfig.put("ReferPos" 、"BottomLeft");
// 透かし表示のタイムライン。 透かし表示の開始時間と期間を指定できます。
JSONObjectタイムライン=new JSONObject();
// 透かし表示の開始時刻。
timeline.put("スタート" 、"2");
// 透かし表示の期間。
timeline.put("期間" 、"ToEND");
watermarkConfig.put("タイムライン" 、タイムライン);
return watermarkConfig;
}
/**
* テキスト透かしのパラメータを作成します。 パラメーターをrequireper-dとして設定します。
* @ return
* /
public static JSONObject buildTextWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// 透かしとして使用するテキスト。
watermarkConfig.put("Content" 、"testwatermark");
// 透かしに使用するフォント。
watermarkConfig.put("FontName", "SimSun");
// 透かしに使用するフォントサイズ。
watermarkConfig.put("FontSize" 、40);
// 透かしに使用するフォントの色。 カラー名またはRGBカラーコード (#000000など) を指定できます。
watermarkConfig.put("FontColor" 、"Black");
// 透かしに使用する透明度。
watermarkConfig.put("FontAlpha" 、"0.2");
// 透かしに使用するストロークの色。 # ffffffなどのカラー名またはRGBカラーコードを指定できます。
watermarkConfig.put("BorderColor" 、"White");
// 透かしに使用するストローク幅。
watermarkConfig.put("BorderWidth" 、2);
// 透かしの左上の頂点と出力ビデオの上端との間の距離。
watermarkConfig.put("トップ" 、20);
// 透かしの左上の頂点と出力ビデオの左端の間の距離。
watermarkConfig.put("Left" 、15);
return watermarkConfig;
}
透かしを削除する
DeleteWatermark操作を呼び出して、透かしを削除できます。
この操作のリクエストパラメーターとレスポンスパラメーターの詳細については、「DeleteWatermark」をご参照ください。 サンプルコード:
com.aliyuncs.vod.mo del.v20170321.DeleteWatermarkRequestをインポートします。com.aliyuncs.vod.mo del.v20170321.DeleteWatermarkResponseをインポートします。/**
* 透かしを削除します。
* /
public static DeleteWatermarkResponse deleteWatermark(DefaultAcsClientクライアント) throws Exception {
DeleteWatermarkRequest request=新しいDeleteWatermarkRequest();
request.setWatermarkId("53f9d796fad9d7b862b2e ****");
return client.getAcsResponse (リクエスト);
}
/**
* 呼び出し例
* /
public static void main(String[] args) {
DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
DeleteWatermarkResponseレスポンス=新しいDeleteWatermarkResponse();
try {
response = deleteWatermark (クライアント);
} catch (Exception e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}
複数の透かしを照会する
ListWatermark操作を呼び出して、複数の透かしを照会できます。
この操作のリクエストパラメーターとレスポンスパラメーターの詳細については、「ListWatermark」をご参照ください。 サンプルコード:
com.aliyuncs.vod.mo del.v20170321.ListWatermarkRequestをインポートします。com.aliyuncs.vod.mo del.v20170321.ListWatermarkResponseをインポートします。/**
* 複数の透かしを照会します。
* /
public static ListWatermarkResponse listWatermark(DefaultAcsClientクライアント) throws Exception {
ListWatermarkRequest request = new ListWatermarkRequest();
return client.getAcsResponse (リクエスト);
}
/**
* 呼び出し例
* /
public static void main(String[] args) {
DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
ListWatermarkResponse応答=新しいListWatermarkResponse();
try {
response = listWatermark (クライアント);
for (ListWatermarkResponse.WatermarkInfo watermarkInfo : response.getWatermarkInfos()) {
// 透かしのID。
System.out.println("WatermarkId = " + watermarkInfo.getWatermarkId());
// 透かしの位置と効果の設定。
System.out.println("WatermarkConfig = " + watermarkInfo.getWatermarkConfig());
// 透かしファイルのURL。 テキスト透かしをクエリする場合は、このパラメーターを空のままにします。
System.out.println("FileUrl = " + watermarkInfo.getFileUrl());
}
} catch (Exception e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}
単一の透かしを照会する
GetWatermark操作を呼び出して、単一の透かしに関する詳細を照会できます。
この操作のリクエストパラメーターとレスポンスパラメーターの詳細については、「GetWatermark」をご参照ください。 サンプルコード:
com.aliyuncs.vod.mo del.v20170321.GetWatermarkRequestをインポートします。com.aliyuncs.vod.mo del.v20170321.GetWatermarkResponseをインポートします。/**
* 単一の透かしを照会します。
* /
public static GetWatermarkResponse getWatermark(DefaultAcsClientクライアント) throws Exception {
GetWatermarkRequest request = new GetWatermarkRequest();
// クエリする透かしのID。
request.setWatermarkId("96d4f6e734a526fd2 ****");
return client.getAcsResponse (リクエスト);
}
/**
* 呼び出し例
* /
public static void main(String[] args) {
DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
GetWatermarkResponseレスポンス=新しいGetWatermarkResponse();
try { response = getWatermark (クライアント);
// 透かしのID。
System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
// 透かしの位置と効果の設定。
System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
// 透かしファイルのURL。 テキスト透かしを照会する場合は、このパラメーターを空のままにします。
System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
} catch (例外e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}
透かしをデフォルトの透かしとして指定する
You can call the SetDefaultWatermark operation to specify a watermark as the default one.
この操作のリクエストパラメーターとレスポンスパラメーターの詳細については、「SetDefaultWatermark」をご参照ください。 サンプルコード:
com.aliyuncs.vod.mo del.v20170321.SetDefaultWatermarkRequestをインポートします。com.aliyuncs.vod.mo del.v20170321.SetDefaultWatermarkResponseをインポートします。/**
* 透かしをデフォルトの透かしとして指定します。
* /
パブリック静的SetDefaultWatermarkResponse setDefaultWatermark(DefaultAcsClientクライアント) が例外 {をスローします
SetDefaultWatermarkRequest request=新しいSetDefaultWatermarkRequest();
// デフォルトの透かしのID。
request.setWatermarkId("82105a29c6e96d4f6 ****");
return client.getAcsResponse (要求); }
/**
* 呼び出し例
* /
public static void main(String[] args) {
DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
SetDefaultWatermarkResponse応答=新しいSetDefaultWatermarkResponse();
try {
response = setDefaultWatermark (クライアント);
} catch (Exception e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}