import logging import os import socket import subprocess import threading import psutil from lib.server.server_proxy import RenderServerProxy from lib.utilities.ffmpeg_helper import generate_thumbnail, save_first_frame logger = logging.getLogger() def generate_thumbnail_for_job(job, thumb_video_path, thumb_image_path, max_width=320): # Simple thread to generate thumbs in background def generate_thumb_thread(source): in_progress_path = thumb_video_path + '_IN-PROGRESS' subprocess.run(['touch', in_progress_path]) try: logger.debug(f"Generating video thumbnail for {source}") generate_thumbnail(source_path=source, dest_path=thumb_video_path, max_width=max_width) except subprocess.CalledProcessError as e: logger.error(f"Error generating video thumbnail for {source}: {e}") try: os.remove(in_progress_path) except FileNotFoundError: pass # Determine best source file to use for thumbs source_files = job.file_list() or [job.input_path] if source_files: video_formats = ['.mp4', '.mov', '.avi', '.mpg', '.mpeg', '.mxf', '.m4v', 'mkv'] image_formats = ['.jpg', '.png', '.exr'] image_files = [f for f in source_files if os.path.splitext(f)[-1].lower() in image_formats] video_files = [f for f in source_files if os.path.splitext(f)[-1].lower() in video_formats] if (video_files or image_files) and not os.path.exists(thumb_image_path): try: path_of_source = image_files[0] if image_files else video_files[0] logger.debug(f"Generating image thumbnail for {path_of_source}") save_first_frame(source_path=path_of_source, dest_path=thumb_image_path, max_width=max_width) except Exception as e: logger.error(f"Exception saving first frame: {e}") if video_files and not os.path.exists(thumb_video_path): x = threading.Thread(target=generate_thumb_thread, args=(video_files[0],)) x.start()