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:
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)
|
||||
Reference in New Issue
Block a user