Files
Zordon/lib/utilities/server_helper.py

61 lines
2.4 KiB
Python

import json
import logging
import os
import subprocess
import threading
import requests
from .ffmpeg_helper import generate_thumbnail, save_first_frame
from lib.render_workers.base_worker import RenderStatus
logger = logging.getLogger()
def post_job_to_server(input_path, job_list, hostname, server_port=8080):
# Pack job data and submit to server
job_files = {'file': (os.path.basename(input_path), open(input_path, 'rb'), 'application/octet-stream'),
'json': (None, json.dumps(job_list), 'application/json')}
req = requests.post(f'http://{hostname}:{server_port}/api/add_job', files=job_files)
return req
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 Exception as e:
logger.error(f"Error generating thumbnail for {source}: {e}")
finally:
os.remove(in_progress_path)
# Determine best source file to use for thumbs
if job.status == RenderStatus.COMPLETED: # use finished file for thumb
source_path = job.file_list()
else:
source_path = [job.input_path] # use source if nothing else
if source_path:
video_formats = ['.mp4', '.mov', '.avi', '.mpg', '.mpeg', '.mxf', '.m4v', 'mkv']
image_formats = ['.jpg', '.png', '.exr']
is_image_file = any(ele in source_path[0] for ele in image_formats)
is_video_file = any(ele in source_path[0] for ele in video_formats)
if (is_image_file or is_video_file) and not os.path.exists(thumb_image_path):
try:
logger.debug(f"Generating image thumbnail for {source_path[0]}")
save_first_frame(source_path=source_path[0], dest_path=thumb_image_path, max_width=max_width)
except Exception as e:
logger.error(f"Exception saving first frame: {e}")
if is_video_file and not os.path.exists(thumb_video_path):
x = threading.Thread(target=generate_thumb_thread, args=(source_path[0],))
x.start()