全部产品
Search
文档中心

媒体处理:水印

更新时间:Feb 04, 2024

在视频中添加可见的明水印(例如,企业Logo、电视台台标),可以突出品牌和版权,增加产品识别度。媒体处理支持图片水印动画水印文字水印三种水印类型,您可以按需选择。本文介绍PHP SDK使用水印相关功能的API调用示例。

完整代码

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

function initMtsClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // 服务接入区域
    $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*****';   #管道ID  可在控制台查看
    $templateId = "S00000001-100020"; #转码模板ID,可在控制台查看
    $ossLocation = 'oss-cn-shanghai';
    $bucket = '<bucket name>';
    $ossInputObject = 'input.mp4';
    $ossOutputObject = 'output.mp4';
    #水印模板ID,可在控制台查看,仅适用图片、动图水印
    $watermarkTemplateId = '0ba6fb1ab7c5271a2e1293*****';
    $imageWatermarkObject = 'logo.png';
    $videoWatermarkObject = 'logo.mov';
    $request = new Mts\SubmitJobsRequest();
    # input参数
    $input = array('Location' => $ossLocation,
               'Bucket' => $bucket,
               'Object' => urlencode($ossInputObject));

    # output参数
    $output = array('OutputObject' => urlencode($ossOutputObject));
    $output['TemplateId'] = $templateId;
    # 图片水印
    $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
                    );
    # 文字水印
    $textConfig = array(
                # 需传入Base64后的Content值
                'Content' => '5rWL6K+V5paH5a2X5rC05Y2w',
                'FontName' => 'SimSun',
                'FontSize' => 16,
                'FontColor' => 'Red',
                'FontAlpha' => 0.5,
                'Top' => 10,
                'Left' => 10
                );
    $textWatermark = array(
                    'Type' => 'Text',
                    'TextWaterMark' => $textWatermark
                    );
    # 动图水印
    $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
                    );
    # 水印数组,最多支持4个水印
    $watermarks = array($imageWatermark, $textWatermark, $videoWatermark);
    $output['WaterMarks'] = $watermarks;
    $outputs = array($output);
    # 请求参数
    $request->setInput(json_encode($input));
    $request->setOUtputs(json_encode($outputs));
    $request->setOutputBucket($bucket);
    $request->setOutputLocation($ossLocation);
    $request->setPipelineId($pipelineId);
    return $client->getAcsResponse($request);
}

相关文档