import subprocess import ffmpeg # todo: remove all references to ffmpeg library and instead use direct subprocesses from ..render_engines.ffmpeg_engine import FFMPEG 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", pix_fmt="yuv420p"): subprocess.run([FFMPEG.renderer_path(), "-framerate", str(framerate), "-i", f"{source_glob_pattern}", "-c:v", encoder, "-pix_fmt", pix_fmt, output_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 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,format=yuv420p', '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)