阿里云视频点播转码

阿里云点播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
 */
defined('BASEPATH') OR exit('No direct script access allowed');
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()
        {
            $CI = &get_instance();
            $CI->config->load('aliyundemand',TRUE);
            $config = $CI->config->item('demand','aliyundemand');
            $this->accessKeyId = $config['accessKeyId'];
            $this->accessKeySecret = $config['accessKeySecret'];
            $this->callback = $config['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;
        }

}
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 1
PHPer技术栈

大佬,厉害!

1周前 评论

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!