关于阿里云视频点播对接

业务需求:平台有一个用户上传视频的功能,但是涉及到移动端上传,考虑到IOS和安卓设备录制的视频格式不同,无法查看视频。所以统一将资源转为mp4。之前考虑用ffmpeg。量大,开发繁琐,而且不稳定不考虑

开发流程

通过上传sdk的接口,将资源传到服务器(我选得本地上传接口),然后通过接口传到视频点播,返回数据。删除本地服务器的资源。通过视频获取接口获取返回的ID去拿到当前视频的mo4播放地址。

框架:tp5.1

代码

全局设置->转码设置  转码组id
    private $videoId = '8d1a3dbaf2af4037aaf65cfe95a888d5';
    private $filePath = "/mnt/wwwroot/cloud.vinston.net/public/ceshi.3gp";hi.3gp";
   public function __construct()
    {
      require_once "../extend/VodUploadSDK/voduploadsdk/Autoloader.php";
             require_once "../extend/VodUploadSDK/voduploadsdk/uploader/AliyunVodUploader.php";
             require_once "../extend/VodUploadSDK/voduploadsdk/uploader/UploadVideoRequest.php";
        require_once "../extend/aliyun-openapi-php-sdk/aliyun-php-sdk-vod/vod/Request/V20170321/GetPlayInfoRequest.php";

       
    }

   
    public  function initVodClient() {
       
        $profile = DefaultProfile::getProfile($this->regionId,$this->accessKeyId,$this->accessKeySecret);
        return new DefaultAcsClient($profile);
    }
    
      // 测试上传本地视频  (获取上传地址和凭证等等接口,阿里云都已经在此方法里面封装好了,直接用就可以,此方法会返回    videoId。)
        public function testUploadLocalVideo()
        {
        try {
             
            date_default_timezone_set('PRC');
        $uploader = new AliyunVodUploader($this->accessKeyId, $this->accessKeySecret);
        $uploadVideoRequest = new UploadVideoRequest($this->filePath, 'testUploadLocalVideo via PHP-SDK');
        //$uploadVideoRequest->setCateId(1);
        //$uploadVideoRequest->setCoverURL("http://xxxx.jpg");
        //$uploadVideoRequest->setTags('test1,test2');
        //$uploadVideoRequest->setStorageLocation('outin-xx.oss-cn-beijing.aliyuncs.com');
        //$uploadVideoRequest->setTemplateGroupId('6ae347b0140181ad371d197ebe289326');
        $userData = array(
        "MessageCallback"=>array("CallbackURL"=>"https://cloud.vinston.net/api/test/test1"),
        "Extend"=>array("localId"=>"xxx", "test"=>"www")
        );
        $uploadVideoRequest->setUserData(json_encode($userData));
        $res = $uploader->uploadLocalVideo($uploadVideoRequest);
        print_r($res);
        } catch (Exception $e) {
        printf("testUploadLocalVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
        $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
        }
        }
    
    //获取播放地址(拿到上一个方法返回的videoId作为此方法的参数)
    public function getPlayInfo() {
        try {
            $client = $this->initVodClient();
            $request = new GetPlayInfoRequest();
 
            $request->setVideoId($this->videoId);
            $request->setAuthTimeout(3600*24);
            $request->setAcceptFormat('JSON');
            $playInfo = $client->getAcsResponse($request);
 
 
           dump($playInfo->PlayInfoList->PlayInfo);
            //return $playInfo;
        } catch (Exception $e) {
            print $e->getMessage()."\n";
        }
       
    }
  成功后,会返回m3u8播放地址和mp4地址。

 

你可能感兴趣的:(PHP,TP5)