X-Git-Url: https://git.mdrn.pl/audio.git/blobdiff_plain/4660a2368dbaa858636ca3ab8b64433b721b1457..7a7629297f382c47a84ec5786c8cdd52224887ae:/src/youtube/utils.py diff --git a/src/youtube/utils.py b/src/youtube/utils.py index 59d1033..d5b320e 100644 --- a/src/youtube/utils.py +++ b/src/youtube/utils.py @@ -1,3 +1,4 @@ +from os import unlink import subprocess from tempfile import NamedTemporaryFile @@ -6,7 +7,7 @@ def video_from_image(img_path, duration, fps=25): tmp = NamedTemporaryFile(prefix='image', suffix='.mkv', delete=False) tmp.close() subprocess.run( - ['ffmpeg', '-y', '-framerate', f'1/{duration}', '-i', img_path, '-c:v', 'libx264', '-vf', f'fps={fps},format=yuv420p', tmp.name], check=True) + ['ffmpeg', '-y', '-loop', '1', '-t', str(duration), '-i', img_path, '-c:v', 'libx264', '-vf', f'fps={fps},format=yuv420p', tmp.name], check=True) return tmp.name @@ -18,13 +19,13 @@ def cut_video(video_path, duration): return tmp.name -def concat_videos(paths): +def ffmpeg_concat(paths, suffix): filelist = NamedTemporaryFile(prefix='concat', suffix='.txt') for path in paths: filelist.write(f"file '{path}'\n".encode('utf-8')) filelist.flush() - output = NamedTemporaryFile(prefix='concat', suffix='.mkv', delete=False) + output = NamedTemporaryFile(prefix='concat', suffix=suffix, delete=False) output.close() subprocess.run( @@ -35,6 +36,37 @@ def concat_videos(paths): return output.name +def concat_videos(paths): + return ffmpeg_concat(paths, '.mkv') + +def concat_audio(paths): + std_paths = [ + standardize_audio(p) + for p in paths + ] + output = ffmpeg_concat(std_paths, '.flac') + for p in std_paths: + unlink(p) + return output + +def standardize_audio(p): + output = NamedTemporaryFile(prefix='standarize', suffix='.flac', delete=False) + output.close() + subprocess.run( + ['ffmpeg', '-y', '-i', p, '-sample_fmt', 's16', '-acodec', 'flac', '-ac', '2', '-ar', '44100', output.name], + check=True) + return output.name + +def standardize_video(p): + output = NamedTemporaryFile(prefix='standarize', suffix='.mkv', delete=False) + output.close() + subprocess.run( + ['ffmpeg', '-y', '-i', p, output.name], + check=True) + return output.name + + + def mux(channels, output_path=None): if not output_path: output = NamedTemporaryFile(prefix='concat', suffix='.mkv', delete=False) @@ -43,7 +75,8 @@ def mux(channels, output_path=None): args = ['ffmpeg'] for c in channels: args.extend(['-i', c]) - args.extend(['-shortest', '-y', output_path]) + args.extend([ + '-y', output_path]) subprocess.run(args, check=True) return output_path