scripts/encoder-parameters.py

48 lines
1.2 KiB
Python
Raw Normal View History

2021-07-11 10:47:36 +02:00
#!/usr/bin/env cached-nix-shell
#!nix-shell -i python3 -p python3
2020-01-16 19:25:19 +01:00
import sys
import subprocess
import json
2020-02-26 00:37:11 +01:00
2020-01-16 19:25:19 +01:00
def is_number(value):
2020-02-26 00:37:11 +01:00
numeric_chars = [*map(str, range(10)), "-"]
2020-01-16 19:25:19 +01:00
numeric_charcodes = list(map(ord, numeric_chars))
return all(ord(char) in numeric_charcodes for char in value)
2020-02-26 00:37:11 +01:00
2020-01-16 19:25:19 +01:00
def parse_encoder_params(encoder_params):
2020-02-26 00:37:11 +01:00
for param in encoder_params.split(" / "):
if "=" in param:
key, value = param.split("=", 1)
2020-01-16 19:25:19 +01:00
if is_number(value):
value = int(value)
2020-02-26 00:37:11 +01:00
elif is_number(value.replace(".", "", 1)):
2020-01-16 19:25:19 +01:00
value = float(value)
else:
key = param
value = True
yield key, value
2020-02-26 00:37:11 +01:00
2020-01-16 19:25:19 +01:00
def run_mediainfo(file):
cmd = [
2020-02-26 00:37:11 +01:00
"mediainfo",
"--Inform=Video;%Encoded_Library%\\n%Encoded_Library_Settings%",
file,
2020-01-16 19:25:19 +01:00
]
process = subprocess.run(cmd, stdout=subprocess.PIPE)
2020-02-26 00:37:11 +01:00
output = process.stdout.decode("utf-8").split("\n")
2020-01-16 19:25:19 +01:00
encoder = output[0]
params = dict(parse_encoder_params(output[1]))
2020-02-26 00:37:11 +01:00
return {"file": file, "encoder": encoder, "params": params}
2020-01-16 19:25:19 +01:00
info = list(map(run_mediainfo, sys.argv[1:]))
print(json.dumps(info, indent=2, sort_keys=True))