Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

164 lines
6.8KB

  1. # -*- coding: utf-8 -*-
  2. # This file is auto-generated, don't edit it. Thanks.
  3. import sys
  4. from typing import List
  5. from alibabacloud_vod20170321.client import Client as vod20170321Client
  6. from alibabacloud_tea_openapi import models as open_api_models
  7. from alibabacloud_darabonba_env.client import Client as EnvClient
  8. from alibabacloud_vod20170321 import models as vod_20170321_models
  9. from alibabacloud_tea_console.client import Client as ConsoleClient
  10. from alibabacloud_tea_util.client import Client as UtilClient
  11. from vodsdk.AliyunVodUtils import *
  12. from vodsdk.AliyunVodUploader import AliyunVodUploader
  13. from vodsdk.UploadVideoRequest import UploadVideoRequest
  14. class Sample:
  15. def __init__(self):
  16. pass
  17. @staticmethod
  18. def initialization(
  19. region_id: str,
  20. ) -> vod20170321Client:
  21. config = open_api_models.Config()
  22. # 您的AccessKey ID
  23. config.access_key_id = EnvClient.get_env('LTAI5tSJ62TLMUb4SZuf285A')
  24. # 您的AccessKey Secret
  25. config.access_key_secret = EnvClient.get_env('MWYynm30filZ7x0HqSHlU3pdLVNeI7')
  26. # 您的可用区ID
  27. config.region_id = region_id
  28. return vod20170321Client(config)
  29. @staticmethod
  30. def get_play_info_sample(
  31. client: vod20170321Client,
  32. video_id: str,
  33. ) -> vod_20170321_models.GetPlayInfoResponse:
  34. request = vod_20170321_models.GetPlayInfoRequest()
  35. # 视频ID。
  36. request.video_id = video_id
  37. response = client.get_play_info(request)
  38. return response
  39. @staticmethod
  40. async def get_play_info_sample_async(
  41. client: vod20170321Client,
  42. video_id: str,
  43. ) -> vod_20170321_models.GetPlayInfoResponse:
  44. request = vod_20170321_models.GetPlayInfoRequest()
  45. # 视频ID。
  46. request.video_id = video_id
  47. response = await client.get_play_info_async(request)
  48. return response
  49. @staticmethod
  50. def main(
  51. args: List[str],
  52. ) -> None:
  53. try:
  54. region_id = args[0]
  55. video_id = args[1]
  56. client = Sample.initialization(region_id)
  57. response_get_play_info = Sample.get_play_info_sample(client, video_id)
  58. ConsoleClient.log(UtilClient.to_jsonstring(UtilClient.to_map(response_get_play_info)))
  59. except Exception as error:
  60. ConsoleClient.log(error.message)
  61. @staticmethod
  62. async def main_async(
  63. args: List[str],
  64. ) -> None:
  65. try:
  66. region_id = args[0]
  67. video_id = args[1]
  68. client = Sample.initialization(region_id)
  69. response_get_play_info = await Sample.get_play_info_sample_async(client, video_id)
  70. ConsoleClient.log(UtilClient.to_jsonstring(UtilClient.to_map(response_get_play_info)))
  71. except Exception as error:
  72. ConsoleClient.log(error.message)
  73. accessKeyId='LTAI5tSJ62TLMUb4SZuf285A'
  74. accessKeySecret='MWYynm30filZ7x0HqSHlU3pdLVNeI7'
  75. filePath="/home/thsw/chenyukun/video/111111.mp4"
  76. # 测试上传本地音视频
  77. def testUploadLocalVideo(accessKeyId, accessKeySecret, filePath, storageLocation=None):
  78. try:
  79. # 可以指定上传脚本部署的ECS区域。如果ECS区域和视频点播存储区域相同,则自动使用内网上传,上传更快且更省公网流量。
  80. # ecsRegionId ="cn-shanghai"
  81. # uploader = AliyunVodUploader(accessKeyId, accessKeySecret, ecsRegionId)
  82. # 不指定上传脚本部署的ECS区域。
  83. uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
  84. uploadVideoRequest = UploadVideoRequest(filePath, 'aiOnLineVideo')
  85. # 可以设置视频封面,如果是本地或网络图片可使用UploadImageRequest上传图片到视频点播,获取到ImageURL
  86. #ImageURL示例:https://example.com/sample-****.jpg
  87. #uploadVideoRequest.setCoverURL('<your Image URL>')
  88. # 标签
  89. # uploadVideoRequest.setTags('taa')
  90. if storageLocation:
  91. uploadVideoRequest.setStorageLocation(storageLocation)
  92. videoId = uploader.uploadLocalVideo(uploadVideoRequest)
  93. print("videoId: %s" % (videoId))
  94. except AliyunVodException as e:
  95. print(e)
  96. # testUploadLocalVideo(accessKeyId, accessKeySecret, filePath)
  97. from alibabacloud_tea_util import models as util_models
  98. import time
  99. def get_video_url(video_id):
  100. config = open_api_models.Config(access_key_id=accessKeyId, access_key_secret=accessKeySecret)
  101. config.endpoint = f'vod.aliyuncs.com'
  102. client = vod20170321Client(config)
  103. get_play_info_request = vod_20170321_models.GetPlayInfoRequest(video_id=video_id)
  104. runtime = util_models.RuntimeOptions()
  105. start = time.time()
  106. while True:
  107. try:
  108. # 复制代码运行请自行打印 API 的返回值
  109. vod_20170321_models.GetPlayInfoResponse = client.get_play_info_with_options(get_play_info_request, runtime)
  110. play_url = vod_20170321_models.GetPlayInfoResponse.body.play_info_list.play_info[0].play_url
  111. return play_url
  112. except Exception as error:
  113. print("bbbbbbbbbbbbbb")
  114. print(error)
  115. time.sleep(5)
  116. end = time.time()
  117. result = int(end - start)
  118. if result > 1200:
  119. print("aaaaaaaa")
  120. raise error
  121. import json
  122. import traceback
  123. from aliyunsdkcore.client import AcsClient
  124. from aliyunsdkvod.request.v20170321 import CreateUploadVideoRequest
  125. from aliyunsdkvod.request.v20170321 import GetPlayInfoRequest
  126. from vodsdk.AliyunVodUtils import *
  127. from vodsdk.AliyunVodUploader import AliyunVodUploader
  128. from vodsdk.UploadVideoRequest import UploadVideoRequest
  129. # 获取播放地址
  130. def init_vod_client(accessKeyId, accessKeySecret):
  131. regionId = 'cn-shanghai' # 点播服务接入地域
  132. connectTimeout = 3 # 连接超时,单位为秒
  133. return AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout)
  134. def get_play_info(clt, videoId):
  135. request = GetPlayInfoRequest.GetPlayInfoRequest()
  136. request.set_accept_format('JSON')
  137. request.set_VideoId(videoId)
  138. request.set_AuthTimeout(3600*5)
  139. response = json.loads(clt.do_action_with_exception(request))
  140. return response
  141. if __name__ == '__main__':
  142. # testUploadLocalVideo(accessKeyId, accessKeySecret, "/home/thsw/chenyukun/video/百水河7.mp4")
  143. # print(Sample.get_play_info_sample(Sample.initialization('cn-shanghai'), 'dfaf3d140f714d9889562bff10a6f69a'))
  144. # print(get_video_url('3bb41d547bad44a7a9202017b8025838'))
  145. try:
  146. clt = init_vod_client('LTAI5tSJ62TLMUb4SZuf285A', 'MWYynm30filZ7x0HqSHlU3pdLVNeI7')
  147. playInfo = get_play_info(clt, "43e00a1a9d334c30b743d1cd6138207a")
  148. print(playInfo["PlayInfoList"]["PlayInfo"][0]["PlayURL"])
  149. print(json.dumps(playInfo, ensure_ascii=False, indent=4))
  150. except Exception as e:
  151. print("HTTP Status: 403" not in str(e))