video_processing.py 1.9 KB

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