本文提供了Node.js SDK设置水印的操作步骤及完整的代码示例。

操作步骤

  1. 创建client实例。
    var RPCClient = require('@alicloud/pop-core').RPCClient;
    
    var client = initVodClient('<Your AccessKeyId>', '<Your AccessKeySecret>', '<YourRegionId>');
  2. 设置水印参数。
    • 图片水印
      // Image Watermark
      
         var imageWatermarkInput={
           Location:ossLocation,
           Bucket: ossBucket,
           Object:encodeURIComponent(imageWatermarkObject),
           WaterMarkTemplateId:watermarkTemplateId,
           Type: 'Image',
           InputFile: imageWatermarkInput,
           ReferPos: 'TopRight',
           Width: '0.05',
           Dx: '0',
           Dy: '0'
         }
    • 文字水印
      // Text Watermark
         var textConfig={
           Content:"5rWL6K+V5paH5a2X5rC05Y2w",
           FontName:"SimSun",
           FontSize:"16",
           FontColor:"Red",
           FontAlpha:"0.5",
           Top:"10",
           Left:"10",
         }
           var textWatermark={
           WaterMarkTemplateId:watermarkTemplateId,
           Type:"Text",
           TextWaterMark:JSON.stringtify(textConfig)
         }
    • 视频水印
      // Video Watermark
         var videoWatermarkInput={
           Location: ossLocation,
           Bucket: ossBucket
           Type: Image,
           Object, JSON.stringtify(videoWatermarkObject)
           InputFile: videoWatermarkInput,
           ReferPos: "BottomLeft",
           Height: "240",
           Dx: "0",
           Dy: "0"  
         }
  3. 发起API请求并显示返回值。
    var reponse=client.request("SubmitJobs",params, options);
    console.log(reponse.RequestId)
    if(response.JobResultList.JobResult[0].Success){
      console.log(response.JobResultList.JobResult[0].Job.JobId);
    }
    else
    {
      console.log("SubmitJobs Failed code:" + response.data.Code +
                           " message:" + response.data.Message);
    }
完整代码
var RPCClient = require('@alicloud/pop-core').RPCClient;

function initMtsClient(accessKeyId, accessKeySecret, regionId) {
    var client = new RPCClient({
        accessKeyId: accessKeyId,
        accessKeySecret: accessKeySecret,
        endpoint: 'http://mts.' + regionId + '.aliyuncs.com',
        apiVersion: '2014-06-18'
    });

    return client;
}

var accessKeyId = 'xxx';
var accessKeySecret = 'xxx';
var RegionId = 'cn-beijing';
var watermarkTemplateId = "xxx"; //自己之前创建的水印模板id
var templateId = "S00000001-200030";
var ossBucket = "xxx";
var ossInputObject = "xxx";
var ossOutputObject = "xxx";
var imageWaterMarkObject = "logo.png";
var videoWatermarkObject = "watermark.mov";
var ossLocation = "xxx";
var pipelineId = 'xxx';

var client = initMtsClient(accessKeyId, accessKeySecret, RegionId);

var input = {
    Location: ossLocation,
    Bucket: ossBucket,
    Object: encodeURIComponent(ossInputObject),
};


var imageWatermarkInput = {
    Location: ossLocation,
    Bucket: ossBucket,
    Object: encodeURIComponent(imageWaterMarkObject)
};


var imageWatermark = {
    WaterMarkTemplateId: watermarkTemplateId,
    Type: "Image",
    InputFile: imageWatermarkInput,
    ReferPos: 'TopRight',
    Width: "0.05",
    Dx: "0",
    Dy: "0"
};
//参数可以自己选
var textConfig = {
    Content: "5rWL6K+V5paH5a2X5rC05Y2w",
    FontName: "SimSun",
    FontSize: "16",
    FontColor: "Red",
    FontAlpha: "0.5",
    Top: "10",
    Left: "10",
};
var textWatermark = {
    WaterMarkTemplateId: watermarkTemplateId,
    Type: "Text",
    TextWaterMark: JSON.stringify(textConfig)
};

var videoWatermarkInput = {
    Location: ossLocation,
    Bucket: ossBucket,
    Object: encodeURIComponent(videoWatermarkObject)
};
//参数可以自己选
var videoWatermark = {
    WaterMarkTemplateId: watermarkTemplateId,
    Type: "Image",
    InputFile: videoWatermarkInput,
    ReferPos: "BottomLeft",
    Height: "240",
    Dx: "0",
    Dy: "0"
};

var output = {
    OutputObject: encodeURIComponent(ossOutputObject),
    TemplateId: templateId,
};

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

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

client.request('SubmitJobs', {
    Input: JSON.stringify(input),
    Outputs: JSON.stringify(outputs),
    OutputBucket: ossBucket,
    PipelineId: pipelineId,
    OutputLocation: ossLocation
}, {}).then(function (response) {
    console.log('Response requestId: = ' + response.RequestId);
    console.log('JobID: ' + response.JobResultList.JobResult[0].Job.JobId);
}).catch(function (response) {
    console.log(JSON.stringify(response));
});