You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
7.5KB

  1. # -*- coding: utf-8 -*-
  2. from json import loads
  3. from os.path import join
  4. from traceback import format_exc
  5. import oss2
  6. import time
  7. from aliyunsdkvod.request.v20170321.GetPlayInfoRequest import GetPlayInfoRequest
  8. from loguru import logger
  9. from common.YmlConstant import aliyun_yml_path
  10. from exception.CustomerException import ServiceException
  11. from enums.ExceptionEnum import ExceptionType
  12. from aliyunsdkcore.client import AcsClient
  13. from aliyunsdkvod.request.v20170321 import GetPlayInfoRequest
  14. from util.RWUtils import getConfigs
  15. from vodsdk.AliyunVodUploader import AliyunVodUploader
  16. from vodsdk.UploadVideoRequest import UploadVideoRequest
  17. class AliyunOssSdk:
  18. __slots__ = ('bucket', '__request_id', '__config')
  19. def __init__(self, *args):
  20. base_dir, env, self.__request_id = args
  21. self.bucket = None
  22. self.__config = getConfigs(join(base_dir, aliyun_yml_path % env))
  23. self.get_oss_bucket()
  24. def get_oss_bucket(self):
  25. if self.bucket is None:
  26. auth = oss2.Auth(self.__config["access_key"], self.__config["access_secret"])
  27. self.bucket = oss2.Bucket(auth, self.__config["oss"]["endpoint"],
  28. self.__config["oss"]["bucket"],
  29. connect_timeout=self.__config["oss"]["connect_timeout"])
  30. def put_object(self, updatePath, fileByte):
  31. request_id = self.__request_id
  32. logger.info("开始上传文件到oss, requestId:{}", request_id)
  33. max_retries = 3
  34. retry_count = 0
  35. while True:
  36. try:
  37. self.get_oss_bucket()
  38. self.bucket.put_object(updatePath, fileByte)
  39. logger.info("上传文件到oss成功! requestId:{}", request_id)
  40. break
  41. except Exception as e:
  42. retry_count += 1
  43. time.sleep(1)
  44. self.bucket = None
  45. logger.info("上传文件到oss失败, 重试次数:{}, requestId:{}", retry_count, request_id)
  46. if retry_count > max_retries:
  47. logger.error("上传文件到oss重试失败:{}, requestId:{}", format_exc(), request_id)
  48. raise e
  49. class ThAliyunVodSdk:
  50. __slots__ = ('__config', '__request_id')
  51. def __init__(self, base_dir, env, request_id):
  52. self.__request_id = request_id
  53. self.__config = getConfigs(join(base_dir, aliyun_yml_path % env))
  54. def init_vod_client(self, accessKeyId, accessKeySecret):
  55. regionId = self.__config["vod"]["ecsRegionId"]
  56. return AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=30)
  57. def get_play_info(self, videoId):
  58. logger.info("开始获取视频地址,videoId:{}, requestId:{}", videoId, self.__request_id)
  59. clt = self.init_vod_client(self.__config["access_key"], self.__config["access_secret"])
  60. start = time.time()
  61. retry_num = 0
  62. while True:
  63. try:
  64. request: GetPlayInfoRequest = GetPlayInfoRequest.GetPlayInfoRequest()
  65. request.set_accept_format('JSON')
  66. request.set_VideoId(videoId)
  67. request.set_AuthTimeout(3600 * 5)
  68. response = loads(clt.do_action_with_exception(request))
  69. play_url = response["PlayInfoList"]["PlayInfo"][0]["PlayURL"]
  70. logger.info("获取视频地址成功,视频地址: {}, requestId: {}", play_url, self.__request_id)
  71. return play_url
  72. except Exception as e:
  73. logger.info("获取视频地址失败,5秒后重试, requestId: {}", self.__request_id)
  74. retry_num += 1
  75. time.sleep(5)
  76. current_time = time.time()
  77. if "HTTP Status: 403" not in str(e) and retry_num > 12:
  78. logger.error("获取视频地址失败: {}, requestId: {}", format_exc(), self.__request_id)
  79. raise ServiceException(ExceptionType.GET_VIDEO_URL_EXCEPTION.value[0],
  80. ExceptionType.GET_VIDEO_URL_EXCEPTION.value[1])
  81. if "HTTP Status: 403" in str(e) and ("UploadFail" in str(e) or "TranscodeFail" in str(e) or
  82. "ProduceFail" in str(e)):
  83. logger.error("获取视频地址失败: {}, requestId: {}", format_exc(), self.__request_id)
  84. raise ServiceException(ExceptionType.GET_VIDEO_URL_EXCEPTION.value[0],
  85. ExceptionType.GET_VIDEO_URL_EXCEPTION.value[1])
  86. diff_time = current_time - start
  87. if diff_time > 60 * 60 * 5:
  88. logger.error("获取视频地址失败超时异常: {},超时时间:{}, requestId: {}", format_exc(),
  89. diff_time, self.__request_id)
  90. raise ServiceException(ExceptionType.GET_VIDEO_URL_TIMEOUT_EXCEPTION.value[0],
  91. ExceptionType.GET_VIDEO_URL_TIMEOUT_EXCEPTION.value[1])
  92. def upload_local_video(self, filePath, file_title):
  93. logger.info("开始执行vod视频上传, filePath: {}, requestId: {}", filePath, self.__request_id)
  94. uploader = AliyunVodUploader(self.__config["access_key"], self.__config["access_secret"], self.__request_id)
  95. uploadVideoRequest: UploadVideoRequest = UploadVideoRequest(filePath, file_title)
  96. logger.info("视频分类:{}, requestId:{}", self.__config["vod"]["cateId"], self.__request_id)
  97. uploadVideoRequest.setCateId(self.__config["vod"]["cateId"])
  98. # 可以设置视频封面,如果是本地或网络图片可使用UploadImageRequest上传图片到视频点播,获取到ImageURL
  99. # ImageURL示例:https://example.com/sample-****.jpg
  100. # uploadVideoRequest.setCoverURL('<your Image URL>')
  101. # 标签
  102. # uploadVideoRequest.setTags('tag1,tag2')
  103. MAX_RETRIES = 3
  104. retry_count = 0
  105. while True:
  106. try:
  107. result = uploader.uploadLocalVideo(uploadVideoRequest)
  108. logger.info("vod视频上传成功, videoId:{}, requestId:{}", result.get("VideoId"), self.__request_id)
  109. return result.get("VideoId")
  110. except Exception:
  111. retry_count += 1
  112. time.sleep(1)
  113. uploader = AliyunVodUploader(self.__config["access_key"], self.__config["access_secret"],
  114. self.__request_id)
  115. uploadVideoRequest: UploadVideoRequest = UploadVideoRequest(filePath, file_title)
  116. uploadVideoRequest.setCateId(self.__config["vod"]["cateId"])
  117. logger.error("vod视频上传失败:{},重试次数:{}, requestId:{}", format_exc(), retry_count,
  118. self.__request_id)
  119. if retry_count >= MAX_RETRIES:
  120. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  121. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  122. def get_play_url(self, filePath, file_title):
  123. videoId = self.upload_local_video(filePath, file_title)
  124. if videoId is None or len(videoId) == 0:
  125. raise ServiceException(ExceptionType.GET_VIDEO_URL_EXCEPTION.value[0],
  126. ExceptionType.GET_VIDEO_URL_EXCEPTION.value[1])
  127. return self.get_play_info(videoId)
  128. # if __name__ == "__main__":
  129. # aa = ThAliyunVodSdk('/home/th/tuo_heng/dev/tuoheng_alg', 'dev', "1")
  130. # print(aa.get_play_info('6928821035b171ee9f3b6632b68f0102'))