阿里雲視訊點播轉碼 Composer 版

最閒的碼農發表於2020-01-15
    composer require alibabacloud/sdk

阿里雲OSS上傳這篇文章中

    <?php
        /**
         * Notes: 視訊點播
          * User : zzy
         * Date : 2020/1/15 * Time : 10:23 */
        namespace App\Library;

        use OSS\OssClient;
        use AlibabaCloud\Client\AlibabaCloud;
        use AlibabaCloud\Client\Exception\ClientException;
        use AlibabaCloud\Client\Exception\ServerException;
        use AlibabaCloud\Vod\Vod;

        class AliyunOnDemand
        {

             private $accessKeyId = '';
             private $accessKeySecret = '';
             private $callback = '';
             private $client;

             public function __construct()
             {  
                  $this->accessKeyId = config('aliyun.oss.accessKeyId');
                  $this->accessKeySecret = config('aliyun.oss.accessKeySecret');
                  $this->callback = config('aliyun.onDemand.callback');
                  $this->initVodClient();
              }

            /**
             * Notes: 初始化
             * User: zzy
             * Date: 2020/1/15 * Time: 10:28 
             * Version: 
             */  
             private function initVodClient()    {
                 $regionId = 'cn-shanghai'; // 點播服務接入區域
                  $this->client = AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessKeySecret)
                 ->regionId($regionId)
                 ->connectTimeout(1)
                 ->timeout(3)
                 ->asDefaultClient();
              }

              /**
               * Notes: 初始化點播
               * User: zzy
               * Date: 2020/1/15 * Time: 10:34 * Version: * @param $uploadAuth
               * @param $uploadAddress
               */  
             public static function initOssClient($uploadAuth, $uploadAddress)
             { 
                 $ossClient = new OssClient($uploadAuth['AccessKeyId'], $uploadAuth['AccessKeySecret'], $uploadAddress['Endpoint'],
                 false, $uploadAuth['SecurityToken']);
                  $ossClient->setTimeout(86400 * 7); // 設定請求超時時間,單位秒,預設是5184000秒, 建議不要設定太小,如果上傳檔案很大,消耗的時間會比較長
                  $ossClient->setConnectTimeout(10); // 設定連線超時時間,單位秒,預設是10秒
                  return $ossClient;
              }

              /**
               * Notes: 獲取播放地址
               * User: zzy
               * Date: 2020/1/15 * Time: 10:45
               * Version:
               * @param $videoId
               * @return \AlibabaCloud\Client\Result\Result
               * @throws ClientException
               * @throws ServerException
               */ 
             public static function getPlayInfo($videoId)
             {  
                 $onDemand = new AliyunOnDemand();
                 try  {
                      $request = Vod::v20170321()->getPlayInfo()
                                                 ->withVideoId($videoId)
                                                 ->format('JSON')
                                                 ->request();
                     return array("status" => 1, "data" => $request);
                  } catch (Exception $e){  
                      return array("status" => 0, "data" => $e->getMessage());
                  }
             }

              /**
               * Notes: 上傳本地視訊-點播轉碼
               * User: zzy
               * Date: 2020/1/15 * Time: 10:51 
               *  Version:
               * @param $data
              */ 
             public static function localUploadLocalVideo($data, $isCallback = 1, $localFile)
             {
                  $onDemand = new AliyunOnDemand();
                  $request = Vod::v20170321()->createUploadVideo();
                  $request->withTitle($data['title']);//標題,UTF8,128大小
                  $request->withFileName($data['fileName']);//視訊原始檔名
                  $request->withDescription(isset($data['description']) ? $data['description'] : '');//描述,utf-8
                  $request->withCoverURL(isset($data['coverURL']) ? $data['coverURL'] : '');//封面url
                  $request->withCateId(isset($data['cateId']) ? $data['cateId'] : ''); //分類id
                  $request->withTags(isset($data['tags']) ? $data['tags'] : ''); //標籤,隔開
                  $request->withTemplateGroupId(isset($data['templateGroupId']) ? $data['templateGroupId'] : ''); //轉碼模板組ID
                  $request->withStorageLocation(isset($data['storageLocation']) ? $data['storageLocation'] : ''); //儲存地址
                  if($isCallback == 1)
                 {  
                     $userData = array(
                          "MessageCallback" => array("CallbackURL" => $onDemand->callback),
                          "Extend" => array("title" => $data['title']),
                          'EventType' => 'TranscodeComplete',
                      );
                     $request->withUserData(json_encode($userData));
                  }
                  $result = $request->request();
                  $videoId = $result->VideoId;
                  $uploadAddress = json_decode(base64_decode($result->UploadAddress), true);
                  $uploadAuth = json_decode(base64_decode($result->UploadAuth), true);
                  $ossClient = self::initOssClient($uploadAuth, $uploadAddress);
                  self::multipartUploadFile($ossClient, $uploadAddress, $localFile);
                  return array("status" => 1, "data" => $videoId);
              }

              /**
               * Notes: 分片上傳
               * User: zzy
               * Date: 2020/1/15 * Time: 10:36 * Version: * @param $ossClient
               * @param $uploadAddress
               * @param $localFile
               * @return mixed
               */  
             public static function multipartUploadFile($ossClient, $uploadAddress, $localFile)
             {  
                 return $ossClient->multiuploadFile($uploadAddress['Bucket'], $uploadAddress['FileName'], $localFile);
              }

              /**
               * Notes: 重新整理播放憑證
               * User: zzy
               * Date: 2020/1/15 * Time: 14:45 
               *  Version: * @param $videoId 
               */  
              public function refreshUploadVideo($videoId)
              {  
                 $onDemand = new AliyunOnDemand();
                 $result = Vod::v20170321()
                     ->refreshUploadVideo()
                     ->withVideoId($videoId)
                     ->request();
                  return $result['VideoId'];
              }

}

解決辦法:
下載:cacert.pem
修改php.ini

        curl.cainfo = 你的存放路徑\cacert.pem

重啟伺服器

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章