TrackMania old videos archive tm.tilde.link

encode

+48
+48
encode.py
··· 1 + #! /usr/bin/env python3 2 + 3 + import os 4 + import subprocess 5 + 6 + 7 + def encode(input_file, output_file): 8 + output_directory = os.path.dirname(output_file) 9 + os.makedirs(output_directory, exist_ok=True) 10 + if not os.path.exists(output_file): 11 + subprocess.call([ 12 + 'ffmpeg', 13 + '-i', input_file, 14 + '-codec:a', 'libopus', 15 + '-codec:v', 'libx264', 16 + '-preset', 'veryslow', 17 + '-qp', '23', 18 + '-movflags', '+faststart', 19 + '-pix_fmt', 'yuv420p', 20 + output_file 21 + ]) 22 + 23 + 24 + def main(): 25 + file = os.path.realpath(__file__) 26 + root = os.path.dirname(file) 27 + raw_root = os.path.join(root, 'raw') 28 + web_root = os.path.join(root, 'web') 29 + print(f'← {raw_root}') 30 + print(f'→ {web_root}') 31 + tasks = [] 32 + for directory, directories, files in os.walk(raw_root): 33 + for file in files: 34 + relative_directory = os.path.relpath(directory, raw_root) 35 + name, ext = os.path.splitext(file) 36 + relative_name = os.path.join(relative_directory, name) 37 + tasks.append((relative_name, ext)) 38 + for relative_name, ext in sorted(tasks): 39 + raw_file = os.path.join(raw_root, f'{relative_name}{ext}') 40 + web_file = os.path.join(web_root, f'{relative_name}.mp4') 41 + print() 42 + print(f'← {raw_file}') 43 + print(f'→ {web_file}') 44 + encode(raw_file, web_file) 45 + 46 + 47 + if __name__ == '__main__': 48 + main()