当前位置:   article > 正文

php实现阿里云视频合成_php合成视频

php合成视频

获取视频上传凭证,这个时候会产生一个“上传中”的空videoid,实际上是没有上传内容的;

通过获取到的uploadAuth/uploadAddress/videoId进行上传您本地的视频完成了2和3之后才会出现“正常”状态下的视频。

 

1:视频合成第一步创建云剪辑工程 

  1. /**
  2. * 使用AK&SK初始化账号Client
  3. * @param string $accessKeyId
  4. * @param string $accessKeySecret
  5. * @return Vod Client
  6. */
  7. public static function createClient($accessKeyId, $accessKeySecret){
  8. $config = new Config([
  9. // 您的AccessKey ID
  10. "accessKeyId" => $accessKeyId,
  11. // 您的AccessKey Secret
  12. "accessKeySecret" => $accessKeySecret
  13. ]);
  14. // 访问的域名
  15. $config->endpoint = "vod.cn-beijing.aliyuncs.com";
  16. $config->endpoint = "vod.cn-shanghai.aliyuncs.com";
  17. return new Vod($config);
  18. }
  19. /**
  20. * 创建云剪辑工程
  21. */
  22. public static function cloudClip($info){
  23. $client = self::createClient("", "");
  24. $addEditingProjectRequest = new AddEditingProjectRequest([
  25. "title" => $info['Title'],
  26. 'description' => $info['Description'],
  27. 'coverURL' => $info['CoverURL'],
  28. ]);
  29. return $client -> addEditingProject($addEditingProjectRequest);
  30. }
  31. $info = [
  32. 'Title' => 'zjzj',
  33. 'Description' => '123456789',
  34. 'CoverURL' => 'http://api.cloudtanke.com/static/images/login_logo.png',
  35. 'FileName' => 'zjzj.mp4',
  36. ];
  37. $cloudClip = self::cloudClip($info);#创建云剪辑工程

2:第二步 获取视频上传地址和凭证  (如果报错:The storageLocation does not exist 的话 , 请查看服务地址。改成上海试试)

  1. /**
  2. * 获取视频上传地址和凭证
  3. */
  4. public static function createVideo($info){
  5. $client = self::createClient("", "");
  6. $createUploadVideoRequest = new CreateUploadVideoRequest([
  7. "fileName" => $info['FileName'],
  8. "title" => $info['Title'],
  9. ]);
  10. return $client->createUploadVideo($createUploadVideoRequest);
  11. }
  12. $videoId = self::createVideo($info);

3:第三步 查看文档 基于OSS原生SDK上传视频到点播 

  1. $uploadAuth = json_decode(base64_decode($videoId->body->uploadAuth),true);
  2. $uploadAddress = json_decode(base64_decode($videoId->body->uploadAddress),true);
  3. $localFile = $_SERVER['DOCUMENT_ROOT'].'/uploads/20210409/eb0b8db0e74c61d11529613e3a3c5f2d.mp4';
  4. $ossClient = self::initOssClient($uploadAuth,$uploadAddress,$localFile);#基于OSS原生SDK上传视频
  5. /**
  6. * 基于OSS原生SDK上传视频
  7. * @param $uploadAuth
  8. * @param $uploadAddress
  9. * @param $localFile
  10. * @return null
  11. * @throws \OSS\Core\OssException
  12. */
  13. public static function initOssClient($uploadAuth, $uploadAddress,$localFile) {
  14. $ossClient = new OssClient($uploadAuth['AccessKeyId'], $uploadAuth['AccessKeySecret'], $uploadAddress['Endpoint'],
  15. false, $uploadAuth['SecurityToken']);
  16. $ossClient->setTimeout(86400*7); // 设置请求超时时间,单位秒,默认是5184000秒, 建议不要设置太小,如果上传文件很大,消耗的时间会比较长
  17. $ossClient->setConnectTimeout(10); // 设置连接超时时间,单位秒,默认是10秒
  18. //return $ossClient->uploadFile($uploadAddress['Bucket'], $uploadAddress['FileName'], $localFile);
  19. return $ossClient->multiuploadFile($uploadAddress['Bucket'], $uploadAddress['FileName'], $localFile);
  20. }

4:第四步 视频合成 

合成视频   视频+音频   音频上传如第二步操作一样     

$result这个数组里面包含很多个二维数组,一维数组不会报错,但是会上传不成功

  1. $result = [
  2. "VideoTracks" => [
  3. [
  4. "VideoTrackClips" =>[
  5. [
  6. "MediaId" => $videoId->body->videoId,
  7. "Effects" => [
  8. [
  9. "Type" => "Volume",
  10. "Gain" => "0"
  11. ]
  12. ]
  13. ]
  14. ]
  15. ],
  16. ],
  17. "AudioTracks" => [
  18. [
  19. "AudioTrackClips" =>[
  20. [
  21. "MediaId" => $audioId->body->videoId,
  22. "TimelineIn" => 5
  23. ]
  24. ]
  25. ]
  26. ]
  27. ];
  28. $resultJson = json_encode($result);
  29. $arr=self::videoPost($cloudClip->body->project->projectId,$resultJson);#视频合成
  30. /**
  31. * 视频合成
  32. */
  33. public function videoPost($projectId,$result){
  34. $client = self::createClient("", "");
  35. $produceEditingProjectVideoRequest = new ProduceEditingProjectVideoRequest([
  36. 'projectId' => $projectId,
  37. 'timeline' => $result
  38. ]);
  39. return $client->produceEditingProjectVideo($produceEditingProjectVideoRequest);
  40. }

完结!!!

 

补充

1:use的路径一定要准确!!!!

  1. use AlibabaCloud\SDK\Vod\V20170321\Models\AddEditingProjectRequest;
  2. use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadVideoRequest;
  3. use AlibabaCloud\SDK\Vod\V20170321\Models\GetPlayInfoRequest;
  4. use AlibabaCloud\SDK\Vod\V20170321\Models\GetVideoPlayAuthRequest;
  5. use AlibabaCloud\SDK\Vod\V20170321\Models\ProduceEditingProjectVideoRequest;
  6. use AlibabaCloud\SDK\Vod\V20170321\Models\RefreshUploadVideoRequest;
  7. use AlibabaCloud\SDK\Vod\V20170321\Vod;
  8. use app\common\controller\Frontend;
  9. use Darabonba\OpenApi\Models\Config;
  10. use OSS\OssClient;

2:SDK包  安装新本SDK 使用composer可

      写代码的时候 找不到 文件  打开https://github.com/alibabacloud-sdk-php/vod-20170321加载到sdk下面

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/246038
推荐阅读
相关标签
  

闽ICP备14008679号