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 MPS SDK for PHP to add watermarks.

Sample code

<?php
require_once './aliyun-php-sdk-core/Config.php'; 
use Mts\Request\V20140618 as Mts;

function initMtsClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // The ID of the region in which your MPS service is deployed.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

try {
    $client = initMtsClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
    $response = submitClipJob($client);
    print_r($response);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

function submitClipJob($client) {
    $pipelineId = 'd7cedd984be7dd63395c*****';   # The ID of the MPS queue. You can log on to the MPS console to view the ID.
    $templateId = "S00000001-100020"; # The ID of the transcoding template. You can log on to the MPS console to view the ID.
    $ossLocation = 'oss-cn-shanghai';
    $bucket = '<bucket name>';
    $ossInputObject = 'input.mp4';
    $ossOutputObject = 'output.mp4';
    # The ID of the watermark template. You can view the template ID in the MPS console. Watermark templates are supported only for image watermarks and animated image watermarks.
    $watermarkTemplateId = '0ba6fb1ab7c5271a2e1293*****';
    $imageWatermarkObject = 'logo.png';
    $videoWatermarkObject = 'logo.mov';
    $request = new Mts\SubmitJobsRequest();
    # The job input.
    $input = array('Location' => $ossLocation,
               'Bucket' => $bucket,
               'Object' => urlencode($ossInputObject));

    # The job output.
    $output = array('OutputObject' => urlencode($ossOutputObject));
    $output['TemplateId'] = $templateId;
    # The configurations of the image watermark.
    $imageWatermarkInput = array(
                            'Location' => $ossLocation,
                            'Bucket' => $bucket,
                            'Object' => urlencode($imageWatermarkObject)
                            );
    $imageWatermark = array(
                    'WaterMarkTemplateId' => $watermarkTemplateId,
                    'Type' => 'Image',
                    'InputFile' => $imageWatermarkInput,
                    'ReferPos' => 'TopRight',
                    'Width' => 0.05,
                    'Dx' => 0,
                    'Dy'=> 0
                    );
    # The configurations of the text watermark.
    $textConfig = array(
                # 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
                );
    $textWatermark = array(
                    'Type' => 'Text',
                    'TextWaterMark' => $textWatermark
                    );
    # The configurations of the animated image watermark.
    $videoWatermarkInput = array (
                            'Location' => $ossLocation,
                            'Bucket' => $bucket,
                            'Object' => urlencode($videoWatermarkObject)
                            );
    $videoWatermark = array(
                    'WaterMarkTemplateId' => $watermarkTemplateId,
                    'Type' => 'Image',
                    'InputFile'=> $videoWatermarkInput,
                    'ReferPos' => 'BottomLeft',
                    'Height' => 240,
                    'Dx' => 0,
                    'Dy' => 0
                    );
    # The watermark array. A maximum of four watermarks are supported.
    $watermarks = array($imageWatermark, $textWatermark, $videoWatermark);
    $output['WaterMarks'] = $watermarks;
    $outputs = array($output);
    # The request parameters.
    $request->setInput(json_encode($input));
    $request->setOUtputs(json_encode($outputs));
    $request->setOutputBucket($bucket);
    $request->setOutputLocation($ossLocation);
    $request->setPipelineId($pipelineId);
    return $client->getAcsResponse($request);
}

References