mirror of
https://github.com/blw1138/Zordon.git
synced 2025-12-17 16:58:12 +00:00
Major file reorganization
This commit is contained in:
0
lib/utilities/__init__.py
Normal file
0
lib/utilities/__init__.py
Normal file
57
lib/utilities/ffmpeg_helper.py
Normal file
57
lib/utilities/ffmpeg_helper.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import subprocess
|
||||
import ffmpeg # todo: remove all references to ffmpeg library and instead use direct subprocesses
|
||||
|
||||
|
||||
def file_info(path):
|
||||
try:
|
||||
return ffmpeg.probe(path)
|
||||
except Exception as e:
|
||||
print('Error getting ffmpeg info: ' + str(e))
|
||||
return None
|
||||
|
||||
|
||||
def image_sequence_to_video(source_glob_pattern, output_path, framerate="24", encoder="libx264"):
|
||||
subprocess.run(['ffmpeg', "-y", "-framerate", framerate, "-pattern_type", "glob", "-i", f"{source_glob_pattern}",
|
||||
"-c:v", encoder, output_path])
|
||||
|
||||
|
||||
def save_first_frame(source_path, dest_path, max_width=1280, run_async=False):
|
||||
stream = ffmpeg.input(source_path)
|
||||
stream = ffmpeg.output(stream, dest_path, **{'vf': f'format=yuv420p,scale={max_width}:trunc(ow/a/2)*2',
|
||||
'vframes': '1'})
|
||||
return _run_output(stream, run_async)
|
||||
|
||||
|
||||
def generate_fast_preview(source_path, dest_path, max_width=1280, run_async=False):
|
||||
stream = ffmpeg.input(source_path)
|
||||
stream = ffmpeg.output(stream, dest_path, **{'vf': 'format=yuv420p,scale={width}:-2'.format(width=max_width),
|
||||
'preset': 'ultrafast'})
|
||||
return _run_output(stream, run_async)
|
||||
|
||||
|
||||
def generate_thumbnail(source_path, dest_path, max_width=240, run_async=False):
|
||||
stream = ffmpeg.input(source_path).video
|
||||
stream = ffmpeg.output(stream, dest_path, **{'vf': f'scale={max_width}:trunc(ow/a/2)*2',
|
||||
'preset': 'veryfast',
|
||||
'r': '15',
|
||||
'c:v': 'libx265',
|
||||
'tag:v': 'hvc1'})
|
||||
return _run_output(stream, run_async)
|
||||
|
||||
|
||||
def generate_prores_trim(source_path, dest_path, start_frame, end_frame, handles=10, run_async=False):
|
||||
stream = ffmpeg.input(source_path)
|
||||
stream = stream.trim(**{'start_frame': max(start_frame-handles, 0), 'end_frame': end_frame + handles})
|
||||
stream = stream.setpts('PTS-STARTPTS') # reset timecode
|
||||
stream = ffmpeg.output(stream, dest_path, strict='-2', **{'c:v': 'prores_ks', 'profile:v': 4})
|
||||
return _run_output(stream, run_async)
|
||||
|
||||
|
||||
def _run_output(stream, run_async):
|
||||
return ffmpeg.run_async(stream, quiet=True, overwrite_output=True) if run_async else \
|
||||
ffmpeg.run(stream, quiet=True, overwrite_output=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
x = generate_thumbnail("/Users/brett/Desktop/pexels.mp4", "/Users/brett/Desktop/test-output.mp4", max_width=320)
|
||||
print(x)
|
||||
45
lib/utilities/server_helper.py
Normal file
45
lib/utilities/server_helper.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import threading
|
||||
|
||||
import requests
|
||||
|
||||
from .ffmpeg_helper import generate_thumbnail, save_first_frame
|
||||
from lib.render_workers.render_worker import RenderStatus
|
||||
|
||||
|
||||
def post_job_to_server(input_path, job_list, client, 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://{client}:{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])
|
||||
generate_thumbnail(source_path=source, dest_path=thumb_video_path, max_width=max_width)
|
||||
os.remove(in_progress_path)
|
||||
|
||||
# Determine best source file to use for thumbs
|
||||
if job.render_status() == RenderStatus.COMPLETED: # use finished file for thumb
|
||||
source_path = job.file_list()
|
||||
elif len(job.file_list()) > 1: # if image sequence, use second to last file (last may be in use)
|
||||
source_path = [job.file_list()[-2]]
|
||||
else:
|
||||
source_path = [job.worker.input_path] # use source if nothing else
|
||||
|
||||
if source_path:
|
||||
# Todo: convert image sequence to animated movie
|
||||
valid_formats = ['.mp4', '.mov', '.avi', '.mpg', '.mpeg', '.jpg', '.png', '.exr', '.mxf']
|
||||
is_valid_file_type = any(ele in source_path[0] for ele in valid_formats)
|
||||
if is_valid_file_type and not os.path.exists(thumb_video_path):
|
||||
save_first_frame(source_path=source_path[0], dest_path=thumb_image_path, max_width=max_width)
|
||||
x = threading.Thread(target=generate_thumb_thread, args=(source_path[0],))
|
||||
x.start()
|
||||
Reference in New Issue
Block a user