run_video_extract_text.py 2.7 KB

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