【问题标题】:Alibaba cloud - video streaming阿里云——视频流
【发布时间】:2018-08-16 12:30:46
【问题描述】:

我想使用阿里云的视频点播服务进行视频流媒体。 视频点播从上传的视频中制作不同分辨率的视频以进行数据流传输。 为此,我使用https://github.com/aliyun/aliyun-openapi-php-sdk
现在的问题是我不知道如何通过代码在 视频点播面板 中上传视频。我已签入https://github.com/aliyun/aliyun-openapi-php-sdk/blob/master/aliyun-php-sdk-vod/vod/Request/V20170321/CreateUploadVideoRequest.php,但没有找到用于上传视频的字段或参数。如果有任何其他 SDK 或代码,请告诉我。甚至任何代码或 sn-p 的文档也受到赞赏。

【问题讨论】:

    标签: php video-streaming laravel-5.4 alibaba-cloud


    【解决方案1】:

    示例代码 sn-p 可以在这里找到(虽然是简体中文):https://help.aliyun.com/document_detail/61069.html

    <?php
    include_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php'; //source php and aliyun-php-sdk in same directory
    use vod\Request\V20170321 as vod;
    function init_vod_client($accessKeyId, $accessKeySecret) {
        $regionId = 'cn-shanghai';  
        $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
        return new DefaultAcsClient($profile);
    }
    function create_upload_video($client) {
        $request = new vod\CreateUploadVideoRequest();
        $request->setTitle("VideoTitle");               // Video Title (Mandatory)
        $request->setFileName("filename.mov");          // Source document file name with file extension (Mandatory)
        $request->setDescription("Video Description");  // Video Description (Optional)
        $request->setCoverURL("http://img.alicdn.com/tps/XXXXXXXXXXXXXXXXXXXXXXXXXXX-700-700.png"); // Custom video coverpage (Optional)
        $request->setTags("Tag1,Tag2");                 // Video tags, separated by commas (Optional)
        $request->setAcceptFormat('JSON');
        return $client->getAcsResponse($request);
    }
    try {
        $client = init_vod_client('<AccessKeyId>', '<AccessKeySecret>');
        $uploadInfo = create_upload_video($client);
        var_dump($uploadInfo);
    } catch (Exception $e) {
        print $e->getMessage()."\n";
    }
    ?>
    

    还有一个使用OSS SDK上传视频的demohttps://help.aliyun.com/document_detail/61388.html(也有简体中文)

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      请找到代码sn-p。

      <?php
      require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';    
      require_once './aliyun-php-sdk/aliyun-oss-php-sdk-2.2.4/autoload.php';
      
      use vod\Request\V20170321 as vod;
      use OSS\OssClient;
      use OSS\Core\OssException;
      
      
      function init_vod_client($accessKeyId, $accessKeySecret) {
          $regionId = 'cn-shanghai'; 
          $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
          return new DefaultAcsClient($profile);
      }
      
      
      function create_upload_video($vodClient) {
          $request = new vod\CreateUploadVideoRequest();
      
          $request->setTitle("Movie");        
          $request->setFileName("elephant.mov"); 
      
          $request->setDescription("It is about elephant"); 
          $request->setCoverURL("http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png"); 
          $request->setTags("forest,elephant"); 
      
          return $vodClient->getAcsResponse($request);
      }
      
      function refresh_upload_video($vodClient, $videoId) {
          $request = new vod\RefreshUploadVideoRequest();
          $request->setVideoId($videoId);
          return $vodClient->getAcsResponse($request);
      }
      
      function init_oss_client($uploadAuth, $uploadAddress) {
          $ossClient = new OssClient($uploadAuth['AccessKeyId'], $uploadAuth['AccessKeySecret'], $uploadAddress['Endpoint'], 
              false, $uploadAuth['SecurityToken']);
          $ossClient->setTimeout(86400*7);    
          $ossClient->setConnectTimeout(10);  
          return $ossClient;
      }
      
      
      function upload_local_file($ossClient, $uploadAddress, $localFile) {
          return $ossClient->uploadFile($uploadAddress['Bucket'], $uploadAddress['FileName'], $localFile);
      }
      
      
      function multipart_upload_file($ossClient, $uploadAddress, $localFile) {
          return $ossClient->multiuploadFile($uploadAddress['Bucket'], $uploadAddress['FileName'], $localFile);
      }
      
      $accessKeyId = '<AccessKeyId>';
      $accessKeySecret = '<AccessKeySecret>';           
      $localFile = '/Users/yours/Video/testVideo.flv';   
      
      try {
      
          $vodClient = init_vod_client($accessKeyId, $accessKeySecret);
          $createRes = create_upload_video($vodClient);
      
          $videoId = $createRes->VideoId;
          $uploadAddress = json_decode(base64_decode($createRes->UploadAddress), true);
          $uploadAuth = json_decode(base64_decode($createRes->UploadAuth), true);
      
      
          $ossClient = init_oss_client($uploadAuth, $uploadAddress);
      
      
          //$result = upload_local_file($ossClient, $uploadAddress, $localFile);
          $result = multipart_upload_file($ossClient, $uploadAddress, $localFile);
          printf("Succeed, VideoId: %s", $videoId);
      
      } catch (Exception $e) {
          // var_dump($e);
          printf("Failed, ErrorMessage: %s", $e->getMessage());
      }
      

      更多详情请关注official documentation

      【讨论】:

        【解决方案3】:

        这是 202105 的最新代码更新。 指导链接如下:

        https://www.alibabacloud.com/help/doc-detail/100976.htm?spm=a2c63.p38356.879954.13.330a12fcc5cLMD#task-1995280

        <? php
        /**
         * Created by Aliyun ApsaraVideo VOD.
         */
        require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
        date_default_timezone_set('PRC');
        // Test the upload of an on-premises video.
        function testUploadLocalVideo($accessKeyId, $accessKeySecret, $filePath)
        {
            try {
                $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
                $uploadVideoRequest = new UploadVideoRequest($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://demo.sample.com/ProcessMessageCallback"),
                    "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());
            }
        }
        // Tests the upload of an online video.
        function testUploadWebVideo($accessKeyId, $accessKeySecret, $fileURL)
        {
            try {
                $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
                $uploadVideoRequest = new UploadVideoRequest($fileURL, 'testUploadWebVideo via PHP-SDK');
                $res = $uploader->uploadWebVideo($uploadVideoRequest);
                print_r($res);
            } catch (Exception $e) {
                printf("testUploadWebVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
                    $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
            }
        }
        // Test the upload of an on-premises M3U8 video.
        function testUploadLocalM3u8($accessKeyId, $accessKeySecret, $m3u8FilePath)
        {
            try {
                $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
                $uploadVideoRequest = new UploadVideoRequest($m3u8FilePath, 'testUploadLocalM3u8 via PHP-SDK');
                // Call the method for parsing the M3U8 playlist to obtain the URLs of parts. If the parsing result is invalid, manually assemble the URLs of parts. By default, the part files and M3U8 files are stored in the same directory.
                $sliceFiles = $uploader->parseM3u8File($m3u8FilePath);
                //print_r($sliceFiles);
                $res = $uploader->uploadLocalM3u8($uploadVideoRequest, $sliceFiles);
                print_r($res);
            } catch (Exception $e) {
                printf("testUploadLocalM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
                    $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
            }
        }
        // Test the upload of an online M3U8 video.
        function testUploadWebM3u8($accessKeyId, $accessKeySecret, $m3u8FileUrl)
        {
            try {
                $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
                $uploadVideoRequest = new UploadVideoRequest($m3u8FileUrl, 'testUploadWebM3u8 via PHP-SDK');
                // Call the method for parsing the M3U8 playlist to obtain the URLs of parts. If the parsing result is invalid, manually assemble the URLs of parts. By default, the part files and M3U8 files are stored in the same directory.
                $sliceFileUrls = $uploader->parseM3u8File($m3u8FileUrl);
                //print_r($sliceFileUrls);
                $res = $uploader->uploadWebM3u8($uploadVideoRequest, $sliceFileUrls);
                print_r($res);
            } catch (Exception $e) {
                printf("testUploadWebM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
                    $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
            }
        }
        ####   Run the test code.   ####
        $accessKeyId = '<AccessKeyId>';
        $accessKeySecret = '<AccessKeySecret>';
        //$localFilePath = 'C:\test\sample.mp4';
        $localFilePath = '/opt/video/sample.mp4';
        //testUploadLocalVideo($accessKeyId, $accessKeySecret, $localFilePath);
        $webFileURL = 'http://vod-test1.cn-shanghai.aliyuncs.com/b55b904bc612463b812990b7c8cc95c8/daa30814c0c340cf8199926f78aa5c0e-a0bc05ba62c3e95cc672e88b828148c9-ld.mp4?auth_key=1608774986-0-0-c56acd302bea0c331370d8ed686502fe';
        testUploadWebVideo($accessKeyId, $accessKeySecret, $webFileURL);
        $localM3u8FilePath = '/opt/video/m3u8/sample.m3u8';
        //testUploadLocalM3u8($accessKeyId, $accessKeySecret, $localM3u8FilePath);
        $webM3u8FileURL = 'http://vod-test1.cn-shanghai.aliyuncs.com/b55b904bc612463b812990b7c8cc95c8/daa30814c0c340cf8199926f78aa5c0e-195a25af366b5edae324c47e99a03f04-ld.m3u8?auth_key=1608775606-0-0-9fb038deaecd009dadd86721c5855629';
        //testUploadWebM3u8($accessKeyId, $accessKeySecret, $webM3u8FileURL);
        

        【讨论】:

          猜你喜欢
          • 2019-07-05
          • 1970-01-01
          • 2019-05-25
          • 2018-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-04
          相关资源
          最近更新 更多