2 from tempfile import NamedTemporaryFile
5 def video_from_image(img_path, duration, fps=25):
6 tmp = NamedTemporaryFile(prefix='image', suffix='.mkv', delete=False)
9 ['ffmpeg', '-y', '-framerate', f'1/{duration}', '-i', img_path, '-c:v', 'libx264', '-vf', f'fps={fps},format=yuv420p', tmp.name], check=True)
13 def cut_video(video_path, duration):
14 tmp = NamedTemporaryFile(prefix='cut', suffix='.mkv', delete=False)
17 ['ffmpeg', '-y', '-i', video_path, '-t', str(duration), tmp.name], check=True)
21 def concat_videos(paths):
22 filelist = NamedTemporaryFile(prefix='concat', suffix='.txt')
24 filelist.write(f"file '{path}'\n".encode('utf-8'))
27 output = NamedTemporaryFile(prefix='concat', suffix='.mkv', delete=False)
31 ['ffmpeg', '-y', '-safe', '0', '-f', 'concat', '-i', filelist.name, '-c', 'copy', output.name],
38 def mux(channels, output_path=None):
40 output = NamedTemporaryFile(prefix='concat', suffix='.mkv', delete=False)
42 output_path = output.name
45 args.extend(['-i', c])
46 args.extend(['-shortest', '-y', output_path])
47 subprocess.run(args, check=True)
51 def get_duration(path):
72 def get_framerate(path):
73 rates = subprocess.run(
79 "stream=r_frame_rate",
88 ).stdout.strip().split('\n')
90 a, b = rate.split('/')