阿里雲影片點播轉碼 舊版

最閒的碼農發表於2019-11-08

阿里雲點播SDK下載安裝地址:https://helpcdn.aliyun.com/document_detail...

阿里雲點播上傳SDK下載地址:https://helpcdn.aliyun.com/document_detail...

<?php
/**
 * Notes: 影片點播
 * User : zzy
 * Date : 2019/10/31
 * Time : 8:52
 */

require_once APPPATH.'/vendor/aliyuncs/aliyun-php-sdk/aliyun-php-sdk-core/Config.php';
require_once APPPATH."/vendor/aliyuncs/oss-sdk-php/autoload.php";
use vod\Request\V20170321 as vod;
use OSS\OssClient;
use OSS\Core\OssException;

class AliyunDemand{

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

        /**
         * 建構函式初始化配置
         */
        public function __construct()
        {
            $this->accessKeyId = config('aliyun.oss.accessKeyId');
            $this->accessKeySecret = config('aliyun.oss.accessKeySecret');
            $this->callback = config('aliyun.onDemand.callback');
            $this->connection = $this->initVodClient($this->accessKeyId,$this->accessKeySecret);

        }

        /**
         * Notes: 初始化點播服務
         * User: zzy
         * Date: 2019/10/31
         * Time: 8:56
         * Version:
         * @param $accessKeyId
         * @param $accessKeySecret
         * @return DefaultAcsClient
         */
        private function initVodClient($accessKeyId, $accessKeySecret) {
            $regionId = 'cn-shanghai';  // 點播服務接入區域
            $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
            return new DefaultAcsClient($profile);
        }

        /**
         * Notes:重新整理上傳憑證
         * User: zzy
         * Date: 2019/10/31
         * Time: 15:25
         * Version:
         * @param $vodClient
         * @param $videoId
         * @return mixed|SimpleXMLElement
         * @throws ClientException
         * @throws ServerException
         */
        public function refresh_upload_video($vodClient, $videoId) {
            $request = new vod\RefreshUploadVideoRequest();
            $request->setVideoId($videoId);
            return $this->connection->getAcsResponse($request);
        }

        /**
         * Notes: 初始化點播oss
         * User: zzy
         * Date: 2019/10/31
         * Time: 18:34
         * Version:
         * @param $uploadAuth
         * @param $uploadAddress
         * @return OssClient
         * @throws OssException
         */
        public function init_oss_client($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:大檔案分片上傳,支援斷點續傳;最大支援48.8TB
         * User: zzy
         * Date: 2019/10/31
         * Time: 15:28
         * Version:
         * @param $ossClient
         * @param $uploadAddress
         * @param $localFile
         * @return mixed
         */
        public function multipart_upload_file($ossClient, $uploadAddress, $localFile) {
            return $ossClient->multiuploadFile($uploadAddress['Bucket'], $uploadAddress['FileName'], $localFile);
        }

        /**
         * 獲取影片上傳地址和憑證
         * @param client 傳送請求客戶端w
         * @return CreateUploadVideoResponse 獲取影片上傳地址和憑證響應資料
         */
        /**
         * Notes:獲取影片上傳地址和憑證
         * User: zzy
         * Date: 2019/10/31
         * Time: 16:43
         * Version:
         * @return mixed|SimpleXMLElement
         * @throws ClientException
         * @throws ServerException
         * @return CreateUploadVideoResponse 獲取影片上傳地址和憑證響應資料
         */
        public function createUploadVideo($data) {

            $request = new vod\CreateUploadVideoRequest();
            isset($data['title'])?$request->setTitle($data['title']):"";                               //標題,UTF8,128大小
            isset($data['fileName'])?$request->setFileName($data['fileName']):"";                      //影片原始檔名
            isset($data['fileSize'])?$request->setFileSize($data['fileSize']):"";                      //大小,位元組
            isset($data['description'])?$request->setDescription($data['description']):"";             //描述,utf-8
            isset($data['coverURL'])?$request->setCoverURL($data['coverURL']):"";                      //封面url
            isset($data['cateId'])?$request->setCateId($data['cateId']):"";                            //分類id
            isset($data['tags'])?$request->setTags($data['tags']):"";                                  //標籤,隔開
            isset($data['templateGroupId'])?$request->setTemplateGroupId($data['templateGroupId']):""; //轉碼模板組ID
            isset($data['storageLocation'])?$request->setStorageLocation($data['storageLocation']):""; //儲存地址
            $request->setAcceptFormat('JSON');
            return $this->connection->getAcsResponse($request);
        }

        /**
         * Notes:修改影片資訊
         * User: zzy
         * Date: 2019/11/1
         * Time: 13:07
         * Version:
         * @param $videoId
         * @param $data
         * @return mixed|SimpleXMLElement
         * @throws ClientException
         * @throws ServerException
         */
        public  function updateVideoInfo($videoId,$data) {
            $request = new vod\UpdateVideoInfoRequest();
            $request->setVideoId($videoId);
            isset($data['title'])?$request->setTitle($data['title']):"";                               //標題,UTF8,128大小
            isset($data['description'])?$request->setDescription($data['description']):"";             //描述,utf-8
            isset($data['coverURL'])?$request->setCoverURL($data['coverURL']):"";                      //封面url
            isset($data['tags'])?$request->setTags($data['tags']):"";                                  //標籤,隔開
            isset($data['cateId'])?$request->setCateId($data['cateId']):"";                            //分類id
            $request->setAcceptFormat('JSON');
            return $this->connection->getAcsResponse($request);
        }

        /**
         * Notes: 刪除音訊資訊
         * User: zzy
         * Date: 2019/11/1
         * Time: 13:08
         * Version:
         * @param $videoIds
         * @return mixed|SimpleXMLElement
         * @throws ClientException
         * @throws ServerException
         */
        public function deleteVideos($videoIds) {
            $request = new vod\DeleteVideoRequest();
            $request->setVideoIds($videoIds);   // 支援批次刪除影片;videoIds為傳入的影片ID列表,多個用逗號分隔
            $request->setAcceptFormat('JSON');
            $request->setAcceptFormat('JSON');
            return $this->connection->getAcsResponse($request);
        }

        /**
         * Notes: 上傳點播網路影片
         * User: zzy
         * Date: 2019/11/5
         * Time: 14:04
         * Version:
         * @return string
         */
         public  function ossUploadWebVideo($videoUrl,$videoTitle,$description='',$cateId='',$tags=''){
            try {
                $uploader = new AliyunVodUploader($this->accessKeyId,  $this->accessKeySecret);
                $uploadVideoRequest = new UploadVideoRequest($videoUrl, $videoTitle,$description,$cateId,$tags);

    /*            $userData = array(
                    "MessageCallback"=>array("CallbackURL"=>$this->callback),//你的回撥地址,http://www.xxx.com,這個地址回返回一個json格式,你用獲取HTTP裡的body值
                );
                $uploadVideoRequest->setUserData(json_encode($userData));*/
                $res = $uploader->uploadWebVideo($uploadVideoRequest);
                return array("status"=>1,"data"=>$res);
            } catch (Exception $e) {
                return array("status"=>0,"data"=>$e->getMessage());
            }
        }

        /**
         * Notes: 點播本地影片上傳
         * User: zzy
         * Date: 2019/10/31
         * Time: 16:38
         * Version:
         */
        public  function UploadLocalVideo($data)
        {

            try {
                #$file = $_FILES;
                $localFile = PUBLIC_PATH.$data['videoUrl'];
                $createRes =  $this->createUploadVideo($data);
                $videoId = $createRes->VideoId;
                $uploadAddress = json_decode(base64_decode($createRes->UploadAddress), true);
                $uploadAuth = json_decode(base64_decode($createRes->UploadAuth), true);
                $ossClient = $this->init_oss_client($uploadAuth, $uploadAddress);
                $result = $this->multipart_upload_file($ossClient, $uploadAddress, $localFile);
                return array("status"=>1,"data"=>$videoId);
            } catch (Exception $e) {
                return array("status"=>0,"data"=>$e->getMessage());
            }
        }

        /**
         * Notes: 獲取音訊播放地址
         * User: zzy
         * Date: 2019/10/31
         * Time: 18:15
         * Version:
         * @param $client
         * @param $videoId
         * @return mixed
         */
        public  function getPlayInfo($videoId) {
            $request = new vod\GetPlayInfoRequest();
            $request->setVideoId($videoId);
            $request->setAuthTimeout(0);
            $request->setAcceptFormat('JSON');
            $videoInfoObj = $this->connection->getAcsResponse($request);
            if($videoInfoObj) {
                $videoInfoArr =  json_decode( json_encode( $videoInfoObj),true);
                return $videoInfoArr;
            } else {
                return false;
            }
        }

        /**
         * Notes: 獲取播放憑證
         * User: zzy
         * Date: 2019/10/31
         * Time: 18:28
         * Version:
         * @param $videoId
         * @return mixed|SimpleXMLElement
         * @throws ClientException
         * @throws ServerException
         */
        public  function getPlayAuth($videoId) {
            $request = new vod\GetVideoPlayAuthRequest();
            $request->setVideoId($videoId);
            $request->setAuthInfoTimeout(3000);
            $request->setAcceptFormat('JSON');
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes:媒資分類-建立分類
         * User: zzy
         * Date: 2019/10/31
         * Time: 18:30
         * Version:
         * @param $cateName            分類名稱
         * @param int $parentId        父級id
         * @return mixed|SimpleXMLElement
         * @throws ClientException
         * @throws ServerException
         */
        public function addCategory($cateName, $parentId=-1) {
            $request = new vod\AddCategoryRequest();
            $request->setCateName($cateName);
            $request->setParentId($parentId);
            $request->setAcceptFormat('JSON');
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes: 媒資分類-修改分類
         * User: zzy
         * Date: 2019/10/31
         * Time: 18:31
         * Version:
         * @param $client
         * @param $cateId
         * @param $cateName
         * @return mixed
         */
        public  function updateCategory($cateId, $cateName) {
            $request = new vod\UpdateCategoryRequest();
            $request->setCateId($cateId);
            $request->setCateName($cateName);   // 分類名稱
            $request->setAcceptFormat('JSON');
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes:媒資分類-刪除分類
         * User: zzy
         * Date: 2019/10/31
         * Time: 18:31
         * Version:
         * @param $client
         * @param $cateId
         * @return mixed
         */
        public  function deleteCategory($cateId) {
            $request = new vod\DeleteCategoryRequest();
            $request->setCateId($cateId);
            $request->setAcceptFormat('JSON');
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes:媒資分類-查詢分類及其子分類
         * User: zzy
         * Date: 2019/10/31
         * Time: 18:32
         * Version:
         * @param int $cateId
         * @param int $pageNo
         * @param int $pageSize
         * @return mixed
         */
        public  function getCategories($cateId=-1, $pageNo=1, $pageSize=10) {
            $request = new vod\GetCategoriesRequest();
            $request->setCateId($cateId);   // 分類ID,預設為根節點分類ID即-1
            $request->setPageNo($pageNo);
            $request->setPageSize($pageSize);
            $request->setAcceptFormat('JSON');
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes:提交智慧稽核作業
         * User: zzy
         * Date: 2019/11/1
         * Time: 9:21
         * Version:
         * @return mixed
         */
        public function submitAIMediaAuditJob($videoId) {
            $request = new vod\SubmitAIMediaAuditJobRequest();
            // 設定影片ID
            $request->setMediaId($videoId);
            // 返回結果
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes:查詢智慧稽核作業
         * User: zzy
         * Date: 2019/11/1
         * Time: 9:24
         * Version:
         * @param $videoId
         * @return mixed|SimpleXMLElement
         * @throws ClientException
         * @throws ServerException
         */
        public function getAIMediaAuditJob($videoId) {
            $request = new vod\GetAIMediaAuditJobRequest();
            // 設定作業ID
            $request->setJobId($videoId);
            // 返回結果
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes:獲取智慧稽核結果摘要
         * User: zzy
         * Date: 2019/11/1
         * Time: 9:25
         * Version:
         * @param $videoId
         * @return mixed|SimpleXMLElement
         * @throws ClientException
         * @throws ServerException
         */
        public  function getMediaAuditResult($videoId) {
            $request = new vod\GetMediaAuditResultRequest();
            // 設定影片ID
            $request->setMediaId($videoId);
            // 返回結果
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes:獲取智慧稽核結果詳情
         * User: zzy
         * Date: 2019/11/1
         * Time: 9:29
         * Version:
         * @param $videoId  影片id
         * @param $page 結果詳情頁碼
         * @return mixed|SimpleXMLElement
         * @throws ClientException
         * @throws ServerException
         */
        public function getMediaAuditResultDetail($videoId,$page) {
            $request = new vod\GetMediaAuditResultDetailRequest();
            // 設定影片ID
            $request->setMediaId($videoId);
            // 設定翻頁
            $request->setPageNo($page);
            // 返回結果
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes:預熱快取
         * User: zzy
         * Date: 2019/11/1
         * Time: 9:30
         * Version:
         * @param $client
         * @return mixed
         */
        public function preloadVodObjectCaches($videoPath) {
            $request = new vod\PreloadVodObjectCachesRequest();
            // 需要預熱的檔案路徑
            $request->setObjectPath($videoPath);
            // 返回結果
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

        /**
         * Notes:查詢重新整理預熱次數限制和餘量
         * User: zzy
         * Date: 2019/11/1
         * Time: 9:31
         * Version:
         * @return mixed
         */
        public function describeVodRefreshQuota() {
            $request = new vod\DescribeVodRefreshQuotaRequest();
            $response = $this->connection->getAcsResponse($request);
            return $response;
        }

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

相關文章