Ajoutez des filigranes visibles, tels que des logos d'entreprise ou de chaînes de télévision, à vos vidéos pour améliorer la visibilité de votre marque, protéger les droits d'auteur et renforcer la reconnaissance de vos produits. ApsaraVideo Media Processing (MPS) propose les types de filigranes suivants : image watermark, animated watermark et test watermark. Sélectionnez le type de filigrane adapté à vos besoins métier. Cette rubrique présente des exemples d'appels aux opérations API encapsulées dans le SDK MPS pour Java afin de gérer les filigranes, notamment la création de modèles de filigrane et la soumission de tâches de filigrane textuel ou d'image.
Prérequis
Initialisez le client SDK. Pour plus d'informations, consultez la rubrique Initialiser un client.
AddWaterMarkTemplate : Créer un modèle de filigrane
Un modèle de filigrane définit plusieurs paramètres, tels que la taille et l'emplacement du filigrane. L'utilisation d'un modèle simplifie la gestion des filigranes. Appelez l'opération AddWaterMarkTemplate pour créer un modèle de filigrane.
Un modèle de filigrane s'applique uniquement aux filigranes d'image et non aux filigranes textuels.
Un modèle de filigrane spécifie uniquement les attributs du filigrane (emplacement et taille), mais pas son contenu. Spécifiez le contenu du filigrane lors de la soumission d'une tâche.
Si l'appel aboutit, l'opération renvoie un ID de modèle de filigrane. Vous pouvez également créer un modèle et obtenir son ID dans la console MPS. Pour plus d'informations, consultez la rubrique Gérer les modèles de filigrane.
Si le message d'erreur « The resource "WatermarkTemplate" quota has been used up » s'affiche, votre quota de modèles de filigrane est épuisé. Dans ce cas, soumettez un ticket pour augmenter le quota.
/**
* 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 : Soumettre une tâche de filigrane
L'ajout d'un filigrane modifie les images vidéo et nécessite un réencodage. Utilisez l'opération SubmitJobs pour soumettre une tâche de filigrane.
Lors de la soumission d'une tâche, encodez l'URL du chemin d'accès au fichier utilisé comme filigrane. Sinon, la tâche échoue. Pour plus d'informations, consultez la rubrique Encodage d'URL.
Spécifiez un nom de fichier valide pour garantir que le fichier soit trouvé lors de l'exécution de la tâche. Pour plus d'informations, consultez la rubrique Détails des paramètres.
Enregistrez l'ID de la tâche soumise pour faciliter les opérations ultérieures.
Soumettre une tâche de filigrane textuel
/**
* 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);
}
}
Filigrane d'image
/**
* Submit an image watermark job.
* @return
* @throws Exception
*/
public static void submitImageWaterMarkJobs() 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.
// Image watermarks.
JSONObject imageWaterMarks = new JSONObject();
imageWaterMarks.put("WaterMarkTemplateId","<your waterMarkTemplateId>");
imageWaterMarks.put("Type","Image");
// The width and height of the image or the animated watermark.
imageWaterMarks.put("Width","200");
imageWaterMarks.put("Height","100");
// The path to the image to be used as watermarks.
JSONObject logoFile = new JSONObject();
logoFile.put("Bucket","<your bucket name>");
logoFile.put("Location","oss-cn-shanghai");
// You can specify a static PNG image, an animated PNG image, a MOV file, or a GIF file based on your business requirements. The file name extension of an animated PNG image must be apng. If the image to be used as the watermark is a non-static image, the file name extension must be in lowercase.
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()
// 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);
}
}
Exemple de code
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 {
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.
// Image watermarks.
JSONObject imageWaterMarks = new JSONObject();
imageWaterMarks.put("WaterMarkTemplateId","<your waterMarkTemplateId>");
imageWaterMarks.put("Type","Image");
// The width and height of the image or the animated watermark.
imageWaterMarks.put("Width","200");
imageWaterMarks.put("Height","100");
// The path to the image to be used as watermarks.
JSONObject logoFile = new JSONObject();
logoFile.put("Bucket","<your bucket name>");
logoFile.put("Location","oss-cn-shanghai");
// You can specify a static PNG image, an animated PNG image, a MOV file, or a GIF file based on your business requirements. The file name extension of an animated PNG image must be apng. If the image to be used as the watermark is a non-static image, the file name extension must be in lowercase.
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()
// 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);
}
}
public static void main(String[] args_) throws Exception {
// Create a watermark template.
WaterMark.addWaterMarkTemplate();
// Submit an image watermark job.
//WaterMark.submitImageWaterMarkJobs();
// Submit a text watermark job.
//WaterMark.submitTextWaterMarkJobs();
}
}