All Products
Search
Document Center

ApsaraVideo Media Processing:Add watermarks

Last Updated:Mar 14, 2024

You can add visible watermarks such as company logos or TV station logos to your videos to enhance brand visibility, protect copyrights, and boost product recognition. ApsaraVideo Media Processing (MPS) supports image watermarks, animated watermarks, and text watermarks. You can add watermarks based on your requirements. This topic provides sample code for using use MPS SDK for Node.js to add watermarks.

Sample code

import Console from '@alicloud/tea-console';
import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
import Env from '@alicloud/darabonba-env';
import Util from '@alicloud/tea-util';
import mts20140618, * as $mts20140618 from '@alicloud/mts20140618';
import * as $tea from '@alicloud/tea-typescript';

/**
 * Install Node.js 8.x or later. 
 * Install Alibaba Cloud SDK for Node.js. npm install @alicloud/pop-core --save
 * Install the Alibaba Cloud SDK Credentials package.   npm install @alicloud/credentials
 * Install MPS SDK for Node.js. npm install --save @alicloud/mts20140618
 * 
 */
/** The ID of the MPS queue. You can log on to the MPS console to view the ID. */
var pipelineId = "d7cedd984be7dd63395c*****"; 
/** The ID of the transcoding template. The output file is in the M3U8 format. Select a template based on your business requirements. */
var templateId = "S00000001-100020"; 
var ossLocation = "oss-cn-shanghai";
var bucket = "<bucket name>";
var ossInputObject = "input.mp4";
var ossOutputObject = "output.mp4";
/** The ID of the watermark template. You can log on to the MPS console to view the ID. You can use watermark templates to add only image watermarks and animated watermarks. */
var watermarkTemplateId = "0dc48b5f04945d6717b8fd3*******"; 
var imageWaterMarkObject = "logo.png";
var videoWatermarkObject = "watermark.mov";


export default class Client {

/** Initialize a client. */
static async createClient(accessKeyId: string, accessKeySecret: string, regionId: string): Promise<mts20140618> {
    let config = new $OpenApi.Config({ });
    config.accessKeyId = accessKeyId;
    config.accessKeySecret = accessKeySecret;
    /** The ID of the region in which MPS is deployed. */
    config.regionId = "cn-shanghai";
    return new mts20140618(config);
}

static async main(args: string[]): Promise<void> {
    let client = await Client.createClient(Env.getEnv("ALIBABA_CLOUD_ACCESS_KEY_ID"), Env.getEnv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), args[0]);

    let request = new $mts20140618.SubmitJobsRequest({
        input: inputParam(),
        outputs: outputParam(),
        outputBucket: bucket,
        pipelineId: pipelineId,
        outputLocation: ossLocation
    });
    let response = await client.submitJobs(request);
    Console.log(Util.toJSONString($tea.toMap(response)));
}
}

Client.main(process.argv.slice(2));


function inputParam() {
    var input:any = {};
    input.Location = ossLocation;
    input.Bucket = bucket;
    input.Object = encodeURIComponent(ossInputObject);
    return JSON.stringify(input);
}

function outputParam() {
    var outputOSSObject = encodeURIComponent(ossOutputObject);
    var output:any = {};
    output.OutputObject = outputOSSObject;
    output.TemplateId = templateId;
    output.WaterMarks = waterMarks();

    var outputs = new Array();
    outputs.push(output);

    return JSON.stringify(outputs);
}

function waterMarks(){

    /** The configurations of the image watermark. */
    var imageWatermarkInput = {
        Location: ossLocation,
        Bucket: bucket,
        Object: encodeURIComponent(imageWaterMarkObject)
    };

    var imageWatermark = {
        WaterMarkTemplateId: watermarkTemplateId,
        Type: "Image",
        InputFile: imageWatermarkInput,
        ReferPos: 'TopRight',
        Width: "0.05",
        Dx: "0",
        Dy: "0"
    };
    /** The configurations of the text watermark. */
    var textConfig = {
        /** The content of the text watermark, which must be Base64-encoded. */
        Content: "5rWL6K+V5paH5a2X5rC05Y2w",
        FontName: "SimSun",
        FontSize: "16",
        FontColor: "Red",
        FontAlpha: "0.5",
        Top: "10",
        Left: "10",
    };
    var textWatermark = {
        Type: "Text",
        TextWaterMark: JSON.stringify(textConfig)
    };
    /** The configurations of the animated watermark. */
    var videoWatermarkInput = {
        Location: ossLocation,
        Bucket: bucket,
        Object: encodeURIComponent(videoWatermarkObject)
    };
    var videoWatermark = {
        WaterMarkTemplateId: watermarkTemplateId,
        Type: "Image",
        InputFile: videoWatermarkInput,
        ReferPos: "BottomLeft",
        Height: "240",
        Dx: "0",
        Dy: "0"
    };

    var waterMarks = new Array();
    waterMarks.push(imageWatermark);
    waterMarks.push(textWatermark);
    waterMarks.push(videoWatermark);

    return waterMarks;
}

References