run_video_extract_text.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. @author: luojunhui
  3. @tools: PyCharm MarsCodeAI && DeepSeek
  4. """
  5. import sys
  6. import signal
  7. import time
  8. import threading
  9. from tqdm import tqdm
  10. from coldStartTasks.multi_modal import GenerateTextFromVideo
  11. from config import apolloConfig
  12. class VideoProcessing:
  13. """
  14. video processing task
  15. """
  16. def __init__(self):
  17. self.generate_text_from_video = GenerateTextFromVideo()
  18. self.generate_text_from_video.connect_db()
  19. self.running = True
  20. self.lock = threading.Lock()
  21. self.upload_thread = None
  22. self.convert_thread = None
  23. def upload_task(self):
  24. """
  25. upload task to google cloud storage
  26. """
  27. while self.running:
  28. with self.lock:
  29. tqdm.write("start upload_video_to_google_ai task...")
  30. upload_videos_count = self.generate_text_from_video.upload_video_to_google_ai()
  31. tqdm.write("upload_video_to_google_ai task completed, total upload_videos_count: {}".format(upload_videos_count))
  32. time.sleep(600)
  33. def convert_task(self):
  34. """
  35. convert video to text
  36. """
  37. while self.running:
  38. with self.lock:
  39. tqdm.write("Starting convert_video_to_text_with_google_ai task...")
  40. self.generate_text_from_video.convert_video_to_text_with_google_ai()
  41. tqdm.write("convert_video_to_text_with_google_ai() task completed.")
  42. time.sleep(10)
  43. def stop(self):
  44. """停止所有线程"""
  45. with self.lock:
  46. tqdm.write("Stopping threads...")
  47. self.running = False # 设置标志为 False,通知线程退出
  48. # 等待线程结束
  49. if self.upload_thread:
  50. self.upload_thread.join()
  51. if self.convert_thread:
  52. self.convert_thread.join()
  53. tqdm.write("All threads stopped.")
  54. def signal_handler(sig, frame):
  55. """捕获信号,优雅退出"""
  56. video_processing.stop()
  57. sys.exit(0)
  58. if __name__ == '__main__':
  59. video_processing = VideoProcessing()
  60. # 创建并启动第一个线程(上传任务)
  61. video_processing.upload_thread = threading.Thread(target=video_processing.upload_task)
  62. video_processing.upload_thread.daemon = False
  63. video_processing.upload_thread.start()
  64. # 创建并启动第二个线程(转换任务)
  65. video_processing.convert_thread = threading.Thread(target=video_processing.convert_task)
  66. video_processing.convert_thread.daemon = False
  67. video_processing.convert_thread.start()
  68. # 注册信号处理函数
  69. signal.signal(signal.SIGINT, signal_handler)
  70. signal.signal(signal.SIGTERM, signal_handler)
  71. # 主线程保持运行,防止程序退出
  72. while video_processing.running:
  73. time.sleep(1)