dever 5 years ago
parent
commit
b90802f1fc
5 changed files with 112 additions and 9 deletions
  1. 32 0
      database/upload.php
  2. 3 0
      sdk/qiniu.php
  3. 64 0
      src/Qiniu.php
  4. 12 9
      src/Store/Qiniu.php
  5. 1 0
      src/View.php

+ 32 - 0
database/upload.php

@@ -29,6 +29,12 @@ $save_type = array
 	4 => '仅云端存储',
 );
 
+$vod_convert = array
+(
+	1 => '不转码',
+	2 => '云端转码',
+);
+
 return array
 (
 	# 表名
@@ -166,6 +172,32 @@ return array
 			//'list'		=> true,
 			'show'	=> 'save_type=2,3,4',
 		),
+
+		'vod_convert'		=> array
+		(
+			'type' 		=> 'int-1',
+			'name' 		=> '是否进行视频转码',
+			'default' 	=> '1',
+			'desc' 		=> '是否进行视频转码',
+			'match' 	=> 'is_numeric',
+			'option' 	=> $vod_convert,
+			'update'	=> 'radio',
+			//'list'		=> true,
+			'show'	=> 'save_type=2,3,4',
+			'control'	=> 'vod_convert',
+		),
+
+		'pipeline'		=> array
+		(
+			'type' 		=> 'varchar-800',
+			'name' 		=> '队列名称-多个用逗号隔开,将随机使用队列',
+			'default' 	=> '',
+			'desc' 		=> '队列名称',
+			'match' 	=> 'option',
+			'update'	=> 'textarea',
+			//'list'		=> true,
+			'show'	=> 'vod_convert=2',
+		),
 		
 		'state'		=> array
 		(

+ 3 - 0
sdk/qiniu.php

@@ -18,3 +18,6 @@ Dever::apply('sdk/Qiniu/Storage/FormUploader', 'upload');
 
 
 Dever::apply('sdk/Qiniu/Storage/UploadManager', 'upload');
+
+Dever::apply('sdk/Qiniu/Processing/Operation', 'upload');
+Dever::apply('sdk/Qiniu/Processing/PersistentFop', 'upload');

+ 64 - 0
src/Qiniu.php

@@ -75,10 +75,74 @@ class Qiniu
         $config = Dever::db('upload/upload')->one($key);
 
         if ($config) {
+
+            if ($config['yun'] > 0 && $config['vod_convert'] == 2) {
+                $yun = Dever::db('upload/yun')->one($config['yun']);
+                if ($yun) {
+                    $this->convert($key, $file, $config, $yun);
+                }
+            }
             $this->initFile($key, $file, $source);
         }
     }
 
+    private function convert($key, $file, $config, $yun)
+    {
+        $accessKey = $yun['appkey'];
+        $secretKey = $yun['appsecret'];
+        $host = $yun['host'];
+        $auth = new \Qiniu\Auth($accessKey, $secretKey);
+        $bucket = $config['bucket'];
+
+        # 转码
+        //转码是使用的队列名称。 https://portal.qiniu.com/mps/pipeline
+        $pipeline = $config['pipeline'];
+        if ($pipeline) {
+            $array = explode(',', $pipeline);
+            $index = array_rand($array);
+            if (isset($array[$index])) {
+                $pipeline = $array[$index];
+            }
+        } else {
+            $pipeline = 'convert';
+        }
+
+        $ext = 'mp4';
+
+        if (strstr($file, '.mp3') || strstr($file, '.wma') || strstr($file, '.wav')) {
+            $ext = 'mp3';
+        }
+        
+        $force = false;
+        //转码完成后通知到你的业务服务器。
+        $notifyUrl = 'http://375dec79.ngrok.com/notify.php';
+        $notifyUrl = false;
+        $config = new \Qiniu\Config();
+        //$config->useHTTPS=true;
+        $pfop = new \Qiniu\Processing\PersistentFop($auth, $config);
+        //要进行转码的转码操作。 http://developer.qiniu.com/docs/v6/api/reference/fop/av/avthumb.html
+        //$fops = "avthumb/m3u8/s/640x360/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ":qiniu_640x360.mp4");
+        $fops = "avthumb/mp4/vb/1.4m|saveas/" . \Qiniu\base64_urlSafeEncode($bucket . ':' . $file . '_c.' . $ext);
+        list($id, $err) = $pfop->execute($bucket, $file, $fops, $pipeline, $notifyUrl, $force);
+
+        /*
+        echo "\n====> pfop avthumb result: \n";
+        if ($err != null) {
+            var_dump($err);
+        } else {
+            echo "PersistentFop Id: $id\n";
+        }
+        die;
+        //查询转码的进度和状态
+        list($ret, $err) = $pfop->status($id);
+        echo "\n====> pfop avthumb status: \n";
+        if ($err != null) {
+            var_dump($err);
+        } else {
+            var_dump($ret);
+        }*/
+    }
+
     public function token()
     {
         $result = Dever::input('result', 2);

+ 12 - 9
src/Store/Qiniu.php

@@ -32,10 +32,15 @@ class Qiniu extends Core implements Config
 
         $this->path()->name($this->data['file']['name'])->ext()->file();
 
-        list($ret, $err) = $upload->putFile($token, $this->file, $this->data['file']['tmp_name'], $param = null);
+        $file = $this->file;
+        if ($this->config['cover'] == 3) {
+            $file = false;
+        }
+        list($ret, $err) = $upload->putFile($token, $file, $this->data['file']['tmp_name'], $param = null);
 
-        if (isset($ret['hash']) && isset($ret['key']) && $ret['key'] == $this->file) {
+        if (isset($ret['hash']) && isset($ret['key'])) {
 
+            $this->file = $ret['key'];
             $file = Dever::load('upload/file-name', array('where_name' => $this->name, 'where_upload' => $this->data['key'], 'rand' => time()));
             
             if (!$this->limit) {
@@ -206,17 +211,15 @@ class Qiniu extends Core implements Config
                 }
                 $this->name .= '_t' . $id;
                 $dest = $this->file();
-            }       
-            
-            $size = $config['width'] . '_' . $config['height'] . '_2';
+            }
 
-            $this->img()->thumb($source, $size, true, $dest);
+            $dest = $source . '?imageView2/1/w/'.$config['width'].'/h/'.$config['height'].'/q/75';
 
-            # 同时进行压缩
             if (isset($config['compress']) && $config['compress'] > 0) {
-                $this->img()->setType('im');
-                $this->img()->compress($dest, $config['compress'], $dest);
+                $dest = $dest . '/q/' . $config['compress'];
             }
+
+            return $dest;
         }
     }
     

+ 1 - 0
src/View.php

@@ -253,6 +253,7 @@ class View
      */
 	public function create($config = array(), $filename = '', $type = 'gd', $update = 2)
 	{
+        print_r($config);die;
 		$this->img = isset($this->img) ? $this->img : new Img();
 
         /*