Bladeren bron

阿里云查询视频转码结果

15954078560 3 jaren geleden
bovenliggende
commit
b615f98831
2 gewijzigde bestanden met toevoegingen van 117 en 1 verwijderingen
  1. 89 1
      application/api/controller/Eyemargin.php
  2. 28 0
      application/common.php

+ 89 - 1
application/api/controller/Eyemargin.php

@@ -14,7 +14,7 @@ use Mts\Request\V20140618 as Mts;
  */
 class Eyemargin extends Common
 {
-    protected $noNeedLogin = ['mpsvideo'];
+    protected $noNeedLogin = ['mpsvideo', 'getencoderesult'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -383,4 +383,92 @@ class Eyemargin extends Common
 //            print 'Error: ' . $e->getErrorCode() . ' Message: ' . $e->getMessage() . "\n";
         }
     }
+    
+    //阿里云查询视频转码结果
+    public function getencoderesult()
+    {
+        set_time_limit(0);
+
+        $where = array(
+            'eye_type' => 1,
+            'jobid' => ['neq', ''],
+            'encode_status' => 0,
+        );
+        $eyemargin = Db::name('eyemargin');
+        $list = $eyemargin->where($where)->limit(100)->select();
+
+        if (!$list) {
+            echo 'mei shu ju';
+            die;
+        }
+
+        //构建转码查询数据
+        $data['Format'] = 'JSON';
+        $data['Version'] = '2014-06-18';
+        $data['AccessKeyId'] = config('oss.secretId');
+        $data['SignatureMethod'] = 'HMAC-SHA1';
+        $data['SignatureVersion'] = '1.0';
+        $data['Action'] = 'QueryJobList';
+
+        foreach ($list as &$v) {
+            $data['Timestamp'] = str_replace('+00:00', 'Z', gmdate('c', time()));
+            $data['SignatureNonce'] = md5(uniqid(mt_rand(), true));
+            $data['JobIds'] = $v['jobid'];
+            $data['Signature'] = $this->computeSignature($data, config('oss.secretKey'));
+
+            $url = 'http://mts.cn-hangzhou.aliyuncs.com/?';
+            foreach ($data as $apiParamKey => $apiParamValue) {
+                $url .= "$apiParamKey=" . urlencode($apiParamValue) . '&';
+            }
+            $url = substr($url, 0, -1);
+
+            $result = file_get_contents($url);
+            $result = json_decode($result, true);
+            $_data = [];
+            if ($result['JobList']['Job'][0]['Percent'] == 100 && $result['JobList']['Job'][0]['State'] == 'TranscodeSuccess') {
+                $_data['video'] = str_replace('uploads', 'output', $v['video']);
+                $_data['encode_status'] = 1;
+                $eyemargin->where(['id' => $v['id'], 'encode_status' => 0])->setField($_data);
+            } elseif ($result['JobList']['Job'][0]['State'] == 'TranscodeFail') {
+                $_data['encode_status'] = 2;
+                $eyemargin->where(['id' => $v['id'], 'encode_status' => 0])->setField($_data);
+            }
+        }
+    }
+
+    /**
+     * @param $parameters
+     * @param $accessKeySecret
+     * @param $iSigner
+     *
+     * @return mixed
+     */
+    protected function computeSignature($parameters, $accessKeySecret)
+    {
+        ksort($parameters);
+        $canonicalizedQueryString = '';
+        foreach ($parameters as $key => $value) {
+            $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
+        }
+        $stringToBeSigned = 'GET&%2F&' . $this->percentEncode(substr($canonicalizedQueryString, 1));
+        return $this->signString($stringToBeSigned, $accessKeySecret . '&');
+    }
+
+    /**
+     * @param $str
+     *
+     * @return string|string[]|null
+     */
+    protected function percentEncode($str)
+    {
+        $res = urlencode($str);
+        $res = str_replace(array('+', '*'), array('%20', '%2A'), $res);
+        $res = preg_replace('/%7E/', '~', $res);
+        return $res;
+    }
+
+    public function signString($source, $accessSecret)
+    {
+        return base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
+    }
 }

+ 28 - 0
application/common.php

@@ -625,4 +625,32 @@ function getVideoInfo($file) {
         $data['height'] = isset($arr_resolution[1]) ? $arr_resolution[1] : '';
     }
     return $data;
+}
+
+function p($arr) {
+    header('content-type:text/html;charset=utf-8');
+    echo '<pre>';
+    print_r($arr);
+    echo '</pre>';
+}
+
+/**
+ * 手机格式验证
+ * @param string $mobile 验证的手机号码
+ * @return boolean
+ */
+function is_mobile($mobile) {
+    if (!empty($mobile)) {
+        return preg_match('/^1[3|4|5|6|7|8|9][0-9]\d{8}$/', $mobile);
+    }
+    return false;
+}
+
+/**
+ * [getMillisecond 获取毫秒级时间戳]
+ * @return [type] [description]
+ */
+function getMillisecond() {
+    list($t1, $t2) = explode(' ', microtime());
+    return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
 }