1. 程式人生 > >php 七牛雲 視訊加水印

php 七牛雲 視訊加水印

具體參考七牛php SDK的原始碼以及官方文件:

前段程式碼

後臺的key->視屏名稱 , 後臺的token

<form method="post" action="http://upload.qiniu.com" enctype="multipart/form-data">
    <input name="key" type="hidden" value="<?php echo $key;?>">
    <input name="token" type="hidden" value="<?php echo $token ?>">
    <input name="file" type="file" />
    <input name="accept" type="hidden" />
    <input type="submit" value="上傳"/>
</form>

後臺程式碼

引入SDK

<?php

namespace backend\controllers;

use Yii;
use app\models\Common;
use app\models\Check;
use app\models\Upload;
use app\models\Video;
use app\models\News;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

/**
 * Site controller
 */
class VideoController extends Controller
{

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub

        require '../seven/autoload.php'; //需要引入
    }

    // 加密方法
    function base64_urlSafeEncode($data)
    {
        $find = array('+', '/');
        $replace = array('-', '_');
        return str_replace($find, $replace, base64_encode($data));
    }


    // 新增視屏
    public function actionVideo_add()
    {

        $secretKey = "***";
        $accessKey = "***";
        $auth = new Auth($accessKey,$secretKey);
        // 檔案空間名
        $bucket = 'video';
        // 這裡是視屏水印, 地址要求能訪問到的圖片地址
        $wmImg = $this->base64_urlSafeEncode('http:/1539844532.png');
        // 文字水印
        $name = $this->base64_urlSafeEncode('文字水印');
        // 這裡是隨機生成的Key
        $key = date("YmdHis").Common::random(6);
        // 這裡是加過水印的視屏名稱
        $video_name = $this->base64_urlSafeEncode($bucket.':new_'.$key);

        // 訪問介面。拼接圖片水印地址。後面是介面地址的各種引數和值wmFontColor/顏色/wmFontSize/文字大小/wmGravityText/顯示位置|saveas/加過水印的視屏名稱
        $pfop = "avthumb/mp4/wmImage/".$wmImg."/wmText/".$name."/wmFontColor/cmVk/wmFontSize/30/wmGravityText/North|saveas/".$video_name;
        // var_dump($pfop);exit;    
        $opts = array(
            // 回撥地址
            'returnUrl' => 'http://tutor.com/video/callback',
            'persistentOps' => $pfop,
        );
        
        $token = $auth->uploadToken($bucket, null, 3600, $opts);
        // var_dump($token);exit;
        return $this->render('video_add',['token' => $token,'key' => $key]);

    }

過程列印看

/ 回撥地址
    public function actionCallback()
    {
        $upload_ret = Yii::$app->request->get('upload_ret');
        $json_ret = base64_decode($upload_ret);
        $result = json_decode($json_ret);
        // 返回原視屏名稱
        $video_url = $result->key;
        // 前面加上new_得到加過水印的視訊
        $video_url = 'new_'.$result->key;

        // 地址
        $http = "http://ovu8sm392.bkt.clouddn.com/";
        // 第一幀
        $img = '?vframe/jpg/offset/1';
        $model = new Video();
        $model->video_img = $http.'new_'.$video_url.$img;
        $model->title = 'super good';
        $model->video_url = $http.'new_'.$video_url;
        $model->video_length = $model->getVideotime($http.$video_url);
        $model->save();

        header("Location:/video/list");

    }

自己瞎研究的簡便的

2

   
$opts = array(
            'persistentOps' => $pfop,
            'persistentPipeline' => 'http://tutor.com/video/callback'
        ); 

還有一個官方的,獲取persistentId

200:{"hash":"lkneQF5h-knbumUja8ztwZ_9vtOV","key":"mayun.mp4","persistentId":"z0.5991c5ab45a2650c99ca08b7"}

通過這個地址,替換後面的ID=persistentId 

http://api.qiniu.com/status/get/prefop?id=z0.5991c5ab45a2650c99ca08b7

返回結果

{ "code": 0, "desc": "The fop was completed successfully", "id": "z0.5991c5ab45a2650c99ca08b7", "inputBucket": "test-bucket", "inputKey": "mayun.mp4", "items": [
{
"cmd": "avthumb/mp4/wmText/V29yZCBGb3IgVGVzdA==/wmGravityText/NorthEast/wmFontColor/I0ZGRkYwMA==/wmFontSize/2|saveas/dGVzdC1idWNrZXQ6bmV3X21heXVuLm1wNA==/sign/dGVzdC56aGFvamlhbmZlbmcuY24vbWF5dW4ubXA0P2F2dGh1bWIvbXA0L3dtVGV4dC9WMjl5WkNCR2IzSWdWR1Z6ZEE9PS93bUdyYXZpdHlUZXh0L05vcnRoRWFzdC93bUZvbnRDb2xvci9JMFpHUmtZd01BPT0vd21Gb250U2l6ZS8yfHNhdmVhcy9kR1Z6ZEMxaWRXTnJaWFE2Ym1WM1gyMWhlWFZ1TG0xd05BPT0=",
"code": 0,
"desc": "The fop was completed successfully",
"hash": "lnpwZdWVdtP40j7aWRD7VcfkUln0",
"key": "new_mayun.mp4",
"returnOld": 0
}
], "pipeline": "1381151735.av-pipeline", "reqid": "sQkAAC8PjH_swNoU"
}

這個第二個方法的地址: