tm/encode.py

50 lines
1.4 KiB
Python
Raw Permalink Normal View History

2023-01-10 22:05:36 +00:00
#! /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',
2023-01-14 10:50:01 +00:00
'-b:a', '128K',
2023-01-10 22:05:36 +00:00
'-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()