Uploadvideo.php 5.0 KB

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