全部產品
Search
文件中心

ApsaraVideo Media Processing:浮水印

更新時間:Jun 30, 2024

在視頻中添加可見的明浮水印(例如,企業Logo、電視台台標),可以突出品牌和著作權,增加產品識別度。ApsaraVideo for Media Processing支援圖片浮水印動畫浮水印文字浮水印三種浮水印類型,您可以按需選擇。本文介紹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);
}

相關文檔