VideoDemand.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use AlibabaCloud\SDK\Vod\V20170321\Vod;
  7. use AlibabaCloud\SDK\Vod\V20170321\Models\GetPlayInfoRequest;
  8. use AlibabaCloud\Tea\Exception\TeaError;
  9. use Darabonba\OpenApi\Models\Config;
  10. use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadVideoRequest;
  11. use AlibabaCloud\SDK\Vod\V20170321\Models\RefreshUploadVideoRequest;
  12. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  13. use AlibabaCloud\Tea\Utils\Utils;
  14. use \Exception;
  15. use AlibabaCloud\SDK\Vod\V20170321\Models\GetMezzanineInfoRequest;
  16. use AlibabaCloud\SDK\Vod\V20170321\Models\GetVideoPlayAuthRequest;
  17. require_once '../vendor/aliyunmail/aliyun-php-sdk-core/Config.php'; // 假定您的源码文件和aliyun-php-sdk处于同一目录
  18. $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
  19. if (file_exists($path)) require_once $path;
  20. date_default_timezone_set('PRC');
  21. /**
  22. * @title 阿里云视频点播【接口数据及格式为视频点播接口返回】
  23. * @controller VideoDemand
  24. * @group base
  25. */
  26. class VideoDemand extends Base
  27. {
  28. protected $account_name = 'vod@1122764885952286.onaliyun.com'; // 账号
  29. protected $login_secret = 'NKna#5y1mKBp58k)qqwJHhRcrUE!{ECT';// 密码
  30. protected $account_uid = '285664060640337784';// uid
  31. protected $access_key = 'LTAI5tN5SwDsN2km6PgZPScp';// key
  32. protected $access_secret = 'SqRcmQNYG8d7l2vjDLpVvibwT59bSj';//secret
  33. protected $role_arn= 'acs:ram::1122764885952286:role/voderole';//role_arn
  34. protected $role_session_name= 'voderole';//role_session_name
  35. /**
  36. * 使用AK&SK初始化账号Client
  37. * @param string $accessKeyId
  38. * @param string $accessKeySecret
  39. * @return Vod Client
  40. */
  41. public static function createClient($accessKeyId, $accessKeySecret){
  42. $config = new Config([
  43. // 您的 AccessKey ID
  44. "accessKeyId" => $accessKeyId,
  45. // 您的 AccessKey Secret
  46. "accessKeySecret" => $accessKeySecret
  47. ]);
  48. // 访问的域名
  49. $config->endpoint = "vod.cn-shanghai.aliyuncs.com";
  50. return new Vod($config);
  51. }
  52. /**
  53. * 获取本地文件对象
  54. * @return \think\File
  55. */
  56. private function getUploadFile()
  57. {
  58. try {
  59. return $this->request->file('file');
  60. } catch (\Exception $e) {
  61. $this->error(lang($e->getMessage()));
  62. }
  63. }
  64. /**
  65. * @title 获取视频播放凭证[STS方式]
  66. * @desc 获取视频播放凭证[STS方式]
  67. * @author qc
  68. * @url /api/Video_demand/getVideoCredentials
  69. * @method GET
  70. * @tag 视频播放凭证
  71. * @header name:Authorization require:1 desc:Token
  72. * @return name:RequestId type:string default:-- desc:RequestId
  73. * @return name:Arn type:string default:-- desc:RoleArn(Arn)
  74. * @return name:AssumedRoleId type:string default:-- desc:AssumedRoleId
  75. * @return name:SecurityToken type:string default:-- desc:SecurityToken
  76. * @return name:AccessKeyId type:string default:-- desc:AccessKeyId
  77. * @return name:AccessKeySecret type:string default:-- desc:AccessKeySecret
  78. * @return name:Expiration type:string default:-- desc:Expiration
  79. */
  80. public function getVideoCredentials()
  81. {
  82. //构建一个阿里云客户端,用于发起请求。
  83. //设置调用者(RAM用户或RAM角色)的AccessKey ID和AccessKey Secret。
  84. AlibabaCloud::accessKeyClient($this->access_key, $this->access_secret)->regionId('cn-hangzhou')->asDefaultClient();
  85. //设置参数,发起请求。
  86. try {
  87. $result = AlibabaCloud::rpc()
  88. ->product('Sts')
  89. ->scheme('https') // https | http
  90. ->version('2015-04-01')
  91. ->action('AssumeRole')
  92. ->method('POST')
  93. ->host('sts.aliyuncs.com')
  94. ->options([
  95. 'query' => [
  96. 'RegionId' => "cn-hangzhou",
  97. 'RoleArn' => $this->role_arn,
  98. 'RoleSessionName' =>$this->role_session_name,
  99. ],])
  100. ->request();
  101. $ret = $result->toArray();
  102. $ret['Credentials']['region'] = 'cn-shanghai';
  103. $this->success('请求成功',$ret);
  104. } catch (ClientException $e) {
  105. $this->error($e->getErrorMessage() . PHP_EOL);
  106. } catch (ServerException $e) {
  107. $this->error($e->getErrorMessage() . PHP_EOL);
  108. }
  109. }
  110. /**
  111. * @title 获取视频播放凭证[通过视频ID获取]
  112. * @desc 获取视频播放凭证[通过视频ID获取]
  113. * @author qc
  114. * @url /api/Video_demand/getVideoCredentialsByVideoId
  115. * @method GET
  116. * @tag 视频播放凭证
  117. * @header name:Authorization require:1 desc:Token
  118. * @param name:ali_vid type:int require:1 default:0 desc:ali_vid
  119. * @return name:playAuth type:string default:-- desc:playAuth(时效3000s)
  120. * @return name:requestId type:string default:-- desc:requestId
  121. */
  122. public function getVideoCredentialsByVideoId()
  123. {
  124. $client = self::createClient($this->access_key, $this->access_secret);
  125. $getVideoPlayAuthRequest = new GetVideoPlayAuthRequest([
  126. 'videoId'=>input('ali_vid'),
  127. "authInfoTimeout" => 100
  128. ]);
  129. $runtime = new RuntimeOptions([]);
  130. try {
  131. $res = $client->getVideoPlayAuthWithOptions($getVideoPlayAuthRequest, $runtime);
  132. $json_res = json_encode($res,true);
  133. $res_arr = json_decode($json_res,true);
  134. }catch (\Exception $error){
  135. if (!($error instanceof TeaError)) {
  136. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  137. }
  138. $this->error($error->message);
  139. }
  140. // 复制代码运行请自行打印 API 的返回值
  141. $this->success('ok',$res_arr['body']);
  142. }
  143. /**
  144. * @title 获取视频上传凭证
  145. * @desc 获取视频上传凭证
  146. * @author qc
  147. * @url /api/Video_demand/createUploadVideo
  148. * @method POST
  149. * @tag 获取视频上传凭证
  150. * @header name:Authorization require:1 desc:Token
  151. * @name name:file type:string default:-- desc:视频文件
  152. * @return name:requestId type:string default:-- desc:requestId
  153. * @return name:uploadAddress type:string default:-- desc:uploadAddress
  154. * @return name:uploadAuth type:string default:-- desc:uploadAuth
  155. * @return name:videoId type:string default:-- desc:videoId
  156. */
  157. public function createUploadVideo(){
  158. if (!($file = $this->getUploadFile()) || empty($file)) $this->error('文件上传异常,文件可能过大或未上传!');
  159. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) $this->error('文件上传类型受限,请在后台配置!');
  160. if ($file->checkExt('php,sh')) $this->error('可执行文件禁止上传到本地服务器!');
  161. $size = $file->getSize();
  162. $file_name = $file->getInfo('name');
  163. $extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  164. $up_param = [
  165. 'fileName'=>$file_name,
  166. 'FileSize'=>$size,
  167. 'title'=>'视频上传',
  168. ];
  169. try {
  170. $client = self::createClient($this->access_key, $this->access_secret);
  171. $createUploadVideoRequest = new CreateUploadVideoRequest($up_param);
  172. $runtime = new RuntimeOptions([]);
  173. $res = $client->createUploadVideoWithOptions($createUploadVideoRequest, $runtime);
  174. $json_res = json_encode($res,true);
  175. $res_arr = json_decode($json_res,true);
  176. }catch (\Exception $error){
  177. if (!($error instanceof TeaError)) {
  178. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  179. }
  180. $this->error($error->message);
  181. }
  182. $this->success('ok',$res_arr['body']);
  183. }
  184. /**
  185. * @title 刷新视频上传凭证
  186. * @desc 刷新视频上传凭证
  187. * @author qc
  188. * @url /api/Video_demand/refreshUploadVideoToken
  189. * @method GET
  190. * @tag 刷新视频上传凭证
  191. * @header name:Authorization require:1 desc:Token
  192. * @header name:Authorization require:1 desc:Token
  193. * @param name:ali_vid type:string default:-- desc:videoId
  194. * @return name:uploadAddress type:string default:-- desc:uploadAddress
  195. * @return name:uploadAuth type:string default:-- desc:uploadAuth
  196. * @return name:videoId type:string default:-- desc:videoId
  197. */
  198. public function refreshUploadVideoToken()
  199. {
  200. $client = self::createClient($this->access_key, $this->access_secret);
  201. $refreshUploadVideoRequest = new RefreshUploadVideoRequest([
  202. 'videoId'=>input('ali_vid'),
  203. ]);
  204. $runtime = new RuntimeOptions([]);
  205. try {
  206. // 复制代码运行请自行打印 API 的返回值
  207. $res = $client->refreshUploadVideoWithOptions($refreshUploadVideoRequest, $runtime);
  208. $json_res = json_encode($res,true);
  209. $res_arr = json_decode($json_res,true);
  210. }
  211. catch (\Exception $error) {
  212. if (!($error instanceof TeaError)) {
  213. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  214. }
  215. $this->error($error->message);
  216. }
  217. $this->success('ok',$res_arr['body'] );
  218. }
  219. /**
  220. * @title 获取视频播放地址[通过视频ID获取]
  221. * @desc 获取视频播放地址[通过视频ID获取]
  222. * @author qc
  223. * @url /api/Video_demand/getVideoPath
  224. * @method GET
  225. * @tag 视频播放凭证
  226. * @header name:Authorization require:1 desc:Token
  227. * @param name:ali_vid type:int require:1 default:0 desc:ali_vid
  228. * @return name:playInfoList type:array default:-- desc:playInfoList
  229. * @return name:playInfoList.playInfo.playURL type:string default:-- desc:播放地址
  230. */
  231. public function getVideoPath()
  232. {
  233. $client = self::createClient($this->access_key, $this->access_secret);
  234. $getVideoPlayAuthRequest = new GetPlayInfoRequest(['videoId'=>input('ali_vid')]);
  235. $runtime = new RuntimeOptions([]);
  236. try {
  237. $res = $client->getPlayInfoWithOptions($getVideoPlayAuthRequest, $runtime);
  238. $json_res = json_encode($res,true);
  239. $res_arr = json_decode($json_res,true);
  240. }catch (\Exception $error){
  241. if (!($error instanceof TeaError)) {
  242. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  243. }
  244. $this->error($error->message);
  245. }
  246. $this->success('ok',$res_arr['body'] );
  247. }
  248. public function getVideoInfo($ali_vid)
  249. {
  250. $client = self::createClient($this->access_key, $this->access_secret);
  251. $getVideoPlayAuthRequest = new GetPlayInfoRequest([
  252. 'videoId'=>$ali_vid,
  253. 'resultType'=>'Multiple',
  254. 'Formats'=>'mp4'
  255. ]);
  256. $runtime = new RuntimeOptions();
  257. try {
  258. $res = $client->getPlayInfoWithOptions($getVideoPlayAuthRequest, $runtime);
  259. $json_res = json_encode($res,true);
  260. $res_arr = json_decode($json_res,true);
  261. }catch (\Exception $error){
  262. return [];
  263. }
  264. return $res_arr['body'];
  265. }
  266. /**
  267. * @title 获取到完整的源文件信息[通过视频ID获取]
  268. * @desc 获取到完整的源文件信息[通过视频ID获取]
  269. * @author qc
  270. * @url /api/Video_demand/getMezzanineInfo
  271. * @method GET
  272. * @header name:Authorization require:1 desc:Token
  273. * @param name:ali_vid type:int require:1 default:0 desc:ali_vid
  274. * @return name:Mezzanine type:array default:0 desc:文件信息
  275. * @return name:FileURL type:string default:0 desc:源文件地址
  276. * @return name:fileName type:string default:0 desc:文件名称
  277. * @return name:Bitrate type:string default:0 desc:文件码率。单位:Kbps。
  278. * @return name:Height type:string default:0 desc:文件高度。单位:px。
  279. * @return name:Width type:string default:0 desc:文件宽度。单位:px。
  280. * @return name:Size type:string default:0 desc:文件大小。单位:Byte。
  281. * @return name:Duration type:string default:0 desc:文件时长。单位:秒。
  282. * @return name:Fps type:string default:0 desc:文件帧率,每秒多少帧。
  283. */
  284. public function getMezzanineInfo()
  285. {
  286. $client = self::createClient($this->access_key, $this->access_secret);
  287. $getMezzanineInfoRequest = new GetMezzanineInfoRequest([
  288. 'videoId'=>input('ali_vid'),
  289. ]);
  290. $runtime = new RuntimeOptions([]);
  291. try {
  292. // 复制代码运行请自行打印 API 的返回值
  293. $res = $client->getMezzanineInfoWithOptions($getMezzanineInfoRequest, $runtime);
  294. $json_res = json_encode($res,true);
  295. $res_arr = json_decode($json_res,true);
  296. }
  297. catch (Exception $error) {
  298. if (!($error instanceof TeaError)) {
  299. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  300. }
  301. $this->error($error->message);
  302. }
  303. $this->success('ok',$res_arr['body'] );
  304. }
  305. }