video_processing.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import re
  2. import requests
  3. import json
  4. from common.aliyun_log import AliyunLogger
  5. from common.redis import get_video_data, install_video_data
  6. class VideoProcessing:
  7. def get_ai_data(self, video_path):
  8. url = "http://8.219.186.16:8080/process_video/"
  9. payload = json.dumps( {
  10. "video_path": video_path
  11. } )
  12. headers = {
  13. 'Content-Type': 'application/json'
  14. }
  15. response = requests.request( "POST", url, headers=headers, data=payload )
  16. try:
  17. response = response.json()
  18. print(response)
  19. result = response['result']
  20. print(result)
  21. cleaned_string = result.split( 'json\n', 1 )[-1].strip()
  22. # 清除控制字符
  23. cleaned_string = re.sub( r'[\x00-\x1F]+', '', cleaned_string )
  24. # 修复引号等格式问题(确保字符串用双引号包围)
  25. cleaned_string = cleaned_string.replace( "\n", "" ).replace( "\\", "" ).strip()
  26. json_data = json.loads( cleaned_string )
  27. print(json_data)
  28. return json_data
  29. except Exception as e:
  30. print(f"视频请求异常:{e}")
  31. return None
  32. def get_video(self):
  33. video_data = get_video_data()
  34. if not video_data:
  35. print("没有获取到视频内容")
  36. return
  37. # 解码为字符串
  38. data_str = video_data.decode( 'utf-8' )
  39. # 解析为 JSON 对象
  40. data_json = json.loads( data_str )
  41. video_id = data_json['video_id']
  42. title = data_json['title']
  43. video_path = data_json['video_path']
  44. data = self.get_ai_data(video_path)
  45. AliyunLogger.logging(str(video_id), title, video_path, data)
  46. print("写入日志成功")
  47. if __name__ == '__main__':
  48. video_processor = VideoProcessing()
  49. video_processor.get_video()