<?php

namespace app\api\controller;

use app\common\controller\Api;
include_once '../vendor/aliyun/aliyun-php-sdk-core/Config.php';
use Mts\Request\V20140618 as Mts;

/**
 * 示例接口
 */
class Mps extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['mpsvideo'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = [''];

    //阿里云媒体处理视频
    public function mpsvideo()
    {
//        $i = strpos('https://eyefate.oss-cn-hangzhou.aliyuncs.com/uploads/xuanzhuan.mp4','uploads');
//        var_dump($i);
//        $oss_input_object = substr('https://eyefate.oss-cn-hangzhou.aliyuncs.com/uploads/xuanzhuan.mp4', strpos('https://eyefate.oss-cn-hangzhou.aliyuncs.com/uploads/xuanzhuan.mp4', 'uploads'));
//        var_dump($oss_input_object);
//        $oss_output_object = str_replace('uploads', 'output', $oss_input_object);
//        var_dump($oss_output_object);
//        die;


        $access_key_id = 'LTAI5tRun9ZQVSgeg9mbjzjh'; //AccessKey ID
        $access_key_secret = 'nIkOAGe5Xttrjn7B5s0flk4z8zCdw8'; //AccessKey Secret
        $mps_region_id = 'cn-hangzhou';
        $pipeline_id = '0c4f9b648be54da09e29d8819f60a0df'; //管道id
        $watermark_template_id = '0c09ed4ef64e424b89a3449bcc0de522'; //水印模板id
        $template_id = '4c43c0ff9436425dbb6eaf2ce74e0ff0';//'S00000001-200030'; //转码模板id
        $oss_location = 'oss-cn-hangzhou';
        $oss_bucket = 'eyefate'; //bucket
        $oss_input_object = 'uploads/2022330/ZWIFbwAxNmdF.mp4'; //输入文件
        $oss_output_object = 'output/2022330/ZWIFbwAxNmdF.mp4'; //输出文件
        $image_watermark_object = 'logo.png'; //水印logo地址
//        $video_watermark_object = 'logo.mov';
# DefaultAcsClient
        $clientProfile = \DefaultProfile::getProfile(
            $mps_region_id,                   # Region ID
            $access_key_id,                   # AccessKey ID
            $access_key_secret                # AccessKey Secret
        );
        $client = new \DefaultAcsClient($clientProfile);
# request
        $request = new Mts\SubmitJobsRequest();
        $request->setAcceptFormat('JSON');
# Input
        $input = array('Location' => $oss_location,
            'Bucket' => $oss_bucket,
            'Object' => urlencode($oss_input_object));
        $request->setInput(json_encode($input));
# Output
        $output = array('OutputObject' => urlencode($oss_output_object));
# Ouput->TemplateId
        $output['TemplateId'] = $template_id;
## Image Watermark
        $image_watermark_input = array(
            'Location' => $oss_location,
            'Bucket' => $oss_bucket,
            'Object' => urlencode($image_watermark_object)
        );
        $image_watermark = array(
            'WaterMarkTemplateId' => $watermark_template_id,
            'Type' => 'Image',
            'InputFile' => $image_watermark_input,
//            'ReferPos' => 'TopRight',
//            'Width' => 0.05,
            'Dx' => 100,
            'Dy'=> 50
        );
## Text Watermark
       /* $text_config = array(
            'Content' => '5rWL6K+V5paH5a2X5rC05Y2w',
            'FontName' => 'SimSun',
            'FontSize' => 16,
            'FontColor' => 'Red',
            'FontAlpha' => 0.5,
            'Top' => 10,
            'Left' => 10
        );
        $text_watermark = array(
            'WaterMarkTemplateId' => $watermark_template_id,
            'Type' => 'Text',
            'TextWaterMark' => $text_config
        );*/
## Video Watermark
        /*$video_watermark_input = array (
            'Location' => $oss_location,
            'Bucket' => $oss_bucket,
            'Object' => urlencode($video_watermark_object)
        );
        $video_watermark = array(
            'WaterMarkTemplateId' => $watermark_template_id,
            'Type' => 'Image',
            'InputFile'=> $video_watermark_input,
            'ReferPos' => 'BottomLeft',
            'Height' => 240,
            'Dx' => 0,
            'Dy' => 0
        );*/
# Output->Watermarks
//        $watermarks = array($image_watermark, $text_watermark, $video_watermark);
        $watermarks = array($image_watermark);
        $output['WaterMarks'] = $watermarks;
# Outputs
        $outputs = array($output);
        $request->setOUtputs(json_encode($outputs));
        $request->setOutputBucket($oss_bucket);
        $request->setOutputLocation($oss_location);
# PipelineId
        $request->setPipelineId($pipeline_id);
# call api
        try {
            $response = $client->getAcsResponse($request);
            print 'RequestId is:' . $response->{'RequestId'} . "\n";;
            if ($response->{'JobResultList'}->{'JobResult'}[0]->{'Success'}) {
                print 'JobId is:' .
                    $response->{'JobResultList'}->{'JobResult'}[0]->{'Job'}->{'JobId'} . "\n";
            } else {
                print 'SubmitJobs Failed code:' .
                    $response->{'JobResultList'}->{'JobResult'}[0]->{'Code'} .
                    ' message:' .
                    $response->{'JobResultList'}->{'JobResult'}[0]->{'Message'} . "\n";
            }
        } catch(ServerException $e) {
            print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
        } catch(ClientException $e) {
            print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
        }
    }

}