Le filigranage vidéo permet d'ajouter des informations d'identification aux vidéos, telles que des logos d'entreprise, des icônes de chaînes de télévision, des pseudonymes d'utilisateurs ou des identifiants utilisateur. Cette fonctionnalité met en valeur votre marque, protège vos droits d'auteur et accroît la reconnaissance de vos produits. Des exemples utilisant le SDK Java sont fournis pour ajouter, modifier, supprimer et interroger des filigranes image ou texte.
Remarques sur l'utilisation
Tous les exemples de cette rubrique utilisent une paire AccessKey pour initialiser une instance de client.
Pour plus de détails sur les paramètres de requête et de réponse, consultez Alibaba Cloud OpenAPI Explorer et ouvrez l'onglet API Documentations.
Pour consulter des exemples de SDK relatifs à d'autres opérations d'API, rendez-vous sur Alibaba Cloud OpenAPI Explorer. Dans l'onglet Parameters, spécifiez les paramètres et lancez l'appel. Sélectionnez ensuite la version du SDK et le langage dans l'onglet SDK Sample Code pour afficher et télécharger le code.
Les exemples de cette rubrique utilisent le SDK V1.0. Pour les exemples V2.0, sélectionnez la version appropriée du SDK dans OpenAPI Explorer.
Initialiser un client
Avant d'utiliser le SDK, initialisez un client. Pour plus d'informations, consultez la section Initialisation.
Ajouter un filigrane
Appelez l'opération AddWatermark pour ajouter un filigrane. Consultez l'exemple ci-dessous pour plus de détails.
Portail OpenAPI : ou AddWatermark.
Pour plus d'informations sur les paramètres permettant d'obtenir une URL de téléchargement et des informations d'identification, consultez la documentation relative à l'opération CreateUploadAttachedMedia.
Pour plus de détails sur les paramètres de téléchargement d'un fichier de filigrane vers Object Storage Service (OSS), reportez-vous à la section Télécharger des objets OSS.
L'exemple de code suivant illustre cette procédure :
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.AddWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.AddWatermarkResponse;
/**
* Read the AccessKey information.
*/
public static DefaultAcsClient initVodClient() throws ClientException {
// The region where ApsaraVideo VOD is activated.
String regionId = "cn-shanghai";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M.
// Do not hard-code the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources in your account may be compromised.
// This example shows how to obtain the AccessKey pair from environment variables to authenticate API requests. Before you run the example, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* The function to add watermark configuration information.
*/
public static AddWatermarkResponse addWatermark(DefaultAcsClient client) throws Exception {
AddWatermarkRequest request = new AddWatermarkRequest();
// The name of the watermark.
request.setName("addwatermark");
// The OSS URL of the watermark file. Example: http://example.oss-cn-shanghai.aliyuncs.com/watermark/test-****.png.
String fileUrl = "<your watermarkFile URL>";
// For an image watermark, you must specify the OSS URL of the image file. The watermark file must be in the same region as the video. For example, if the video is in the China (Shanghai) region, the watermark file must also be stored in the China (Shanghai) region.
request.setFileUrl(fileUrl);
// The watermark configuration data.
JSONObject watermarkConfig = null;
// The position configuration data for the image watermark.
watermarkConfig = buildImageWatermarkConfig();
// The position configuration data for the text watermark.
//watermarkConfig = buildTextWatermarkConfig();
request.setWatermarkConfig(watermarkConfig.toJSONString());
// Text watermark: Text. Image watermark: Image.
request.setType("Image");
return client.getAcsResponse(request);
}
/**
* The following code provides an example.
* @param args
* @throws ClientException
*/
public static void main(String[] args) throws ClientException {
DefaultAcsClient client = initVodClient();
AddWatermarkResponse response = new AddWatermarkResponse();
try {
// Add watermark information.
response = addWatermark(client);
// The watermark ID.
System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
// The position and effect configuration data of the watermark.
System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
// The FileUrl of the watermark file. This field is empty for text watermarks.
System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
} catch (Exception e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}
/**
* Build the configuration data for the image watermark. Modify the parameter values as needed.
* @return
*/
public static JSONObject buildImageWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// The horizontal offset of the watermark.
watermarkConfig.put("Dx", "8");
// The vertical offset of the watermark.
watermarkConfig.put("Dy", "8");
// The width of the watermark.
watermarkConfig.put("Width", "55");
// The height of the watermark.
watermarkConfig.put("Height", "55");
// The relative position of the watermark. Valid values: TopLeft, TopRight, BottomLeft, and BottomRight.
watermarkConfig.put("ReferPos", "BottomRight");
// The timeline for displaying the watermark. You can specify the start time and end time.
JSONObject timeline = new JSONObject();
// The start time for displaying the watermark.
timeline.put("Start", "2");
// The end time for displaying the watermark.
timeline.put("Duration", "ToEND");
watermarkConfig.put("Timeline", timeline);
return watermarkConfig;
}
/**
* Build the configuration data for the text watermark. Modify the parameter values as needed.
* @return
*/
public static JSONObject buildTextWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// The content of the text watermark.
watermarkConfig.put("Content", "testwatermark");
// The font name of the text watermark.
watermarkConfig.put("FontName", "SimSun");
// The font size of the text watermark.
watermarkConfig.put("FontSize", 25);
// The font color of the text watermark. You can also specify an RGB color value, such as #000000.
watermarkConfig.put("FontColor", "Black");
// The transparency of the text watermark.
watermarkConfig.put("FontAlpha", "0.2");
// The outline color of the text watermark. You can also specify an RGB color value, such as #ffffff.
watermarkConfig.put("BorderColor", "White");
// The outline width of the text watermark.
watermarkConfig.put("BorderWidth", 1);
// The offset of the text watermark from the top of the video.
watermarkConfig.put("Top", 20);
// The offset of the text watermark from the left of the video.
watermarkConfig.put("Left", 15);
return watermarkConfig;
}
Modifier un filigrane
Appelez l'opération UpdateWatermark pour modifier un filigrane. Consultez l'exemple ci-dessous pour plus de détails.
Portail OpenAPI : | UpdateWatermark.
L'exemple de code suivant illustre cette procédure :
import com.aliyuncs.auth.AlibabaCloudCredentials;
import com.aliyuncs.auth.EnvironmentVariableCredentialsProvider;
import com.aliyuncs.vod.model.v20170321.UpdateWatermarkRequest;
import com.aliyuncs.vod.model.v20170321.UpdateWatermarkResponse;
/**
* Read the AccessKey information.
*/
public static DefaultAcsClient initVodClient() throws ClientException {
// The region where ApsaraVideo VOD is activated.
String regionId = "cn-shanghai";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M.
// Do not hard-code the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources in your account may be compromised.
// This example shows how to obtain the AccessKey pair from environment variables to authenticate API requests. Before you run the example, configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* The function to modify watermark configuration information.
* Note: You cannot modify the file URL of an image watermark. To use a different image, create a new watermark.
*/
public static UpdateWatermarkResponse updateWatermark(DefaultAcsClient client) throws Exception {
UpdateWatermarkRequest request = new UpdateWatermarkRequest();
request.setName("updatewatermark");
// The ID of the watermark whose configuration information you want to update.
request.setWatermarkId("421ddddd4f6e734a526fd2****");
// The watermark configuration data.
JSONObject watermarkConfig = null;
// The position configuration data for the image watermark.
//watermarkConfig = buildImageWatermarkConfig();
// The position configuration data for the text watermark.
watermarkConfig = buildTextWatermarkConfig();
request.setWatermarkConfig(watermarkConfig.toJSONString());
return client.getAcsResponse(request);
}
/**
* The following code provides an example.
*/
public static void main(String[] args) {
DefaultAcsClient client = initVodClient();
UpdateWatermarkResponse response = new UpdateWatermarkResponse();
try {
response = updateWatermark(client);
// The watermark ID.
System.out.println("WatermarkId = " + response.getWatermarkInfo().getWatermarkId());
// The position and effect configuration data of the watermark.
System.out.println("WatermarkConfig = " + response.getWatermarkInfo().getWatermarkConfig());
// The FileUrl of the watermark file. This field is empty for text watermarks.
System.out.println("FileUrl = " + response.getWatermarkInfo().getFileUrl());
} catch (Exception e) {
System.out.println("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.println("RequestId = " + response.getRequestId());
}
/**
* Build the configuration data for the image watermark. Modify the parameter values as needed.
* @return
*/
public static JSONObject buildImageWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// The horizontal offset of the watermark.
watermarkConfig.put("Dx", "10");
// The vertical offset of the watermark.
watermarkConfig.put("Dy", "10");
// The width of the watermark.
watermarkConfig.put("Width", "66");
// The height of the watermark.
watermarkConfig.put("Height", "66");
// The relative position of the watermark. Valid values: TopLeft, TopRight, BottomLeft, and BottomRight.
watermarkConfig.put("ReferPos", "BottomLeft");
// The timeline for displaying the watermark. You can specify the start time and end time.
JSONObject timeline = new JSONObject();
// The start time for displaying the watermark.
timeline.put("Start", "2");
// The end time for displaying the watermark.
timeline.put("Duration", "ToEND");
watermarkConfig.put("Timeline", timeline);
return watermarkConfig;
}
/**
* Build the configuration data for the text watermark. Modify the parameter values as needed.
* @return
*/
public static JSONObject buildTextWatermarkConfig() {
JSONObject watermarkConfig = new JSONObject();
// The content of the text watermark.
watermarkConfig.put("Content", "testwatermark");
// The font name of the text watermark.
watermarkConfig.put("FontName", "SimSun");
// The font size of the text watermark.
watermarkConfig.put("FontSize", 40);
// The font color of the text watermark. You can also specify an RGB color value, such as #000000.
watermarkConfig.put("FontColor", "Black");
// The transparency of the text watermark.
watermarkConfig.put("FontAlpha", "0.2");
// The outline color of the text watermark. You can also specify an RGB color value, such as #ffffff.
watermarkConfig.put("BorderColor", "White");
// The outline width of the text watermark.
watermarkConfig.put("BorderWidth", 2);
// The offset of the text watermark from the top of the video.
watermarkConfig.put("Top", 20);
// The offset of the text watermark from the left of the video.
watermarkConfig.put("Left", 15);
return watermarkConfig;
}
Supprimer un filigrane
Appelez l'opération DeleteWatermark pour supprimer un filigrane.
Portail OpenAPI : / DeleteWatermark.
Interroger une liste de filigranes
Appelez l'opération ListWatermark pour interroger les filigranes.
Portail OpenAPI : ListWatermark.
Interroger un filigrane unique
Appelez l'opération GetWatermark pour interroger un filigrane unique.
Portail OpenAPI : GetWatermark.
Définir un filigrane par défaut
Appelez l'opération SetDefaultWatermark pour définir un filigrane par défaut.
Portail OpenAPI : SetDefaultWatermark.
Références
Filigranes image et texte pour les vidéos : fournit des détails sur la fonctionnalité de filigranes image et texte d'ApsaraVideo VOD, y compris son mécanisme de fonctionnement, ses scénarios d'utilisation et des exemples.