Uploadvideo.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\common\library;
  3. require_once EXTEND_PATH . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
  4. class Uploadvideo{
  5. public function __construct(){
  6. date_default_timezone_set('PRC');
  7. $config = config('vod');
  8. $this->accessKeyId = $config['accessKeyId'];
  9. $this->accessKeySecret = $config['accessKeySecret'];
  10. }
  11. public function test1(){
  12. $localFilePath = 'E:\test.mp4';
  13. //$localFilePath = '/opt/video/sample.mp4';
  14. $this->testUploadLocalVideo($localFilePath);
  15. }
  16. public function test2(){
  17. $webFileURL = 'http://vod-test1.cn-shanghai.aliyuncs.com/b55b904bc612463b812990b7c8cc95c8/daa30814c0c340cf8199926f78aa5c0e-a0bc05ba62c3e95cc672e88b828148c9-ld.mp4?auth_key=1608774986-0-0-c56acd302bea0c331370d8ed686502fe';
  18. $this->testUploadWebVideo($webFileURL);
  19. }
  20. public function test3(){
  21. $localM3u8FilePath = '/opt/video/m3u8/sample.m3u8';
  22. $this->testUploadLocalM3u8($localM3u8FilePath);
  23. }
  24. public function test4(){
  25. $webM3u8FileURL = 'http://vod-test1.cn-shanghai.aliyuncs.com/b55b904bc612463b812990b7c8cc95c8/daa30814c0c340cf8199926f78aa5c0e-195a25af366b5edae324c47e99a03f04-ld.m3u8?auth_key=1608775606-0-0-9fb038deaecd009dadd86721c5855629';
  26. $this->testUploadWebM3u8($webM3u8FileURL);
  27. }
  28. // 测试上传本地视频
  29. public function testUploadLocalVideo($filePath,$title = '')
  30. {
  31. try {
  32. $uploader = new \AliyunVodUploader($this->accessKeyId, $this->accessKeySecret);
  33. $uploadVideoRequest = new \UploadVideoRequest($filePath, $title);
  34. //$uploadVideoRequest->setCateId(1);
  35. //$uploadVideoRequest->setCoverURL("http://xxxx.jpg");
  36. //$uploadVideoRequest->setTags('test1,test2');
  37. //$uploadVideoRequest->setStorageLocation('outin-xx.oss-cn-beijing.aliyuncs.com');
  38. //$uploadVideoRequest->setTemplateGroupId('6ae347b0140181ad371d197ebe289326');
  39. $userData = array(
  40. "MessageCallback"=>array("CallbackURL"=>"https://demo.sample.com/ProcessMessageCallback"),
  41. "Extend"=>array("localId"=>"xxx", "test"=>"www")
  42. );
  43. $uploadVideoRequest->setUserData(json_encode($userData));
  44. $res = $uploader->uploadLocalVideo($uploadVideoRequest);
  45. print_r($res);
  46. } catch (Exception $e) {
  47. printf("testUploadLocalVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
  48. $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
  49. }
  50. }
  51. // 测试上传网络视频
  52. public function testUploadWebVideo($fileURL,$title = '')
  53. {
  54. try {
  55. $uploader = new \AliyunVodUploader($this->accessKeyId, $this->accessKeySecret);
  56. $uploadVideoRequest = new \UploadVideoRequest($fileURL, $title);
  57. $res = $uploader->uploadWebVideo($uploadVideoRequest);
  58. return $res;
  59. } catch (Exception $e) {
  60. printf("testUploadWebVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
  61. $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
  62. }
  63. }
  64. // 测试上传本地m3u8视频
  65. public function testUploadLocalM3u8($m3u8FilePath)
  66. {
  67. try {
  68. $uploader = new \AliyunVodUploader($this->accessKeyId, $this->accessKeySecret);
  69. $uploadVideoRequest = new \UploadVideoRequest($m3u8FilePath, 'testUploadLocalM3u8 via PHP-SDK');
  70. // 调用接口解析m3u8的分片地址列表,如果解析结果不准确,请自行拼接地址列表(默认分片文件和m3u8文件位于同一目录)
  71. $sliceFiles = $uploader->parseM3u8File($m3u8FilePath);
  72. //print_r($sliceFiles);
  73. $res = $uploader->uploadLocalM3u8($uploadVideoRequest, $sliceFiles);
  74. print_r($res);
  75. } catch (Exception $e) {
  76. printf("testUploadLocalM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
  77. $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
  78. }
  79. }
  80. // 测试上传网络m3u8视频
  81. public function testUploadWebM3u8($m3u8FileUrl)
  82. {
  83. try {
  84. $uploader = new \AliyunVodUploader($this->accessKeyId, $this->accessKeySecret);
  85. $uploadVideoRequest = new \UploadVideoRequest($m3u8FileUrl, 'testUploadWebM3u8 via PHP-SDK');
  86. // 调用接口解析m3u8的分片地址列表,如果解析结果不准确,请自行拼接地址列表(默认分片文件和m3u8文件位于同一目录)
  87. $sliceFileUrls = $uploader->parseM3u8File($m3u8FileUrl);
  88. //print_r($sliceFileUrls);
  89. $res = $uploader->uploadWebM3u8($uploadVideoRequest, $sliceFileUrls);
  90. print_r($res);
  91. } catch (Exception $e) {
  92. printf("testUploadWebM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
  93. $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
  94. }
  95. }
  96. }