From 8027fce02e06fdfdc43d40fdec77a3bc4c56ac82 Mon Sep 17 00:00:00 2001 From: Marc Beninca Date: Tue, 10 Jan 2023 23:05:36 +0100 Subject: [PATCH] encode --- encode.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 encode.py diff --git a/encode.py b/encode.py new file mode 100755 index 0000000..81585ca --- /dev/null +++ b/encode.py @@ -0,0 +1,48 @@ +#! /usr/bin/env python3 + +import os +import subprocess + + +def encode(input_file, output_file): + output_directory = os.path.dirname(output_file) + os.makedirs(output_directory, exist_ok=True) + if not os.path.exists(output_file): + subprocess.call([ + 'ffmpeg', + '-i', input_file, + '-codec:a', 'libopus', + '-codec:v', 'libx264', + '-preset', 'veryslow', + '-qp', '23', + '-movflags', '+faststart', + '-pix_fmt', 'yuv420p', + output_file + ]) + + +def main(): + file = os.path.realpath(__file__) + root = os.path.dirname(file) + raw_root = os.path.join(root, 'raw') + web_root = os.path.join(root, 'web') + print(f'← {raw_root}') + print(f'→ {web_root}') + tasks = [] + for directory, directories, files in os.walk(raw_root): + for file in files: + relative_directory = os.path.relpath(directory, raw_root) + name, ext = os.path.splitext(file) + relative_name = os.path.join(relative_directory, name) + tasks.append((relative_name, ext)) + for relative_name, ext in sorted(tasks): + raw_file = os.path.join(raw_root, f'{relative_name}{ext}') + web_file = os.path.join(web_root, f'{relative_name}.mp4') + print() + print(f'← {raw_file}') + print(f'→ {web_file}') + encode(raw_file, web_file) + + +if __name__ == '__main__': + main()