ffmpeg-explorer/parse_filters.py

164 lines
4.5 KiB
Python
Raw Normal View History

2023-08-17 23:53:26 -04:00
import json
import re
from subprocess import run
MAXES = ["INT_MAX", "FLT_MAX", "DBL_MAX", "I64_MAX", "UINT32_MAX"]
MINS = [
"INT_MIN",
"FLT_MIN",
"DBL_MIN",
"-DBL_MAX",
"I64_MIN",
"-FLT_MAX",
]
OPTION_OVERRIDES = {
"drawtext": {
"fontfile": [
{"value": "", "desc": ""},
{"value": "times.ttf", "desc": "Times"},
{"value": "arial.ttf", "desc": "Arial"},
{"value": "courier.ttf", "desc": "Courier"},
{"value": "comic.ttf", "desc": "Comic Sans"},
]
}
}
2023-08-18 17:23:56 -04:00
2023-08-17 23:53:26 -04:00
def get_names():
filters = []
2023-08-19 13:01:37 -04:00
with open("./filternames_wasm.txt", "r") as infile:
2023-08-17 23:53:26 -04:00
input = infile.readlines()
input = [l.strip() for l in input]
for index, l in enumerate(input):
parts = re.split(r" +", l)
filter_meta = parts[0]
filter_name = parts[1]
2023-08-18 17:29:08 -04:00
filter_type = parts[2]
2023-08-17 23:53:26 -04:00
description = " ".join(parts[3:])
item = {
"id": index,
"meta": filter_meta,
"name": filter_name,
"type": filter_type,
"description": description,
}
filters.append(item)
return filters
def get_params(f):
help_text = run(
["ffmpeg", "-hide_banner", "-h", f"filter={f['name']}"],
text=True,
capture_output=True,
)
2023-09-03 16:21:45 -04:00
2023-08-17 23:53:26 -04:00
text = help_text.stdout
2023-09-03 16:21:45 -04:00
input_channels, output_channels = f["type"].split("->")
f["inputs"] = list(input_channels.lower().strip())
f["outputs"] = list(output_channels.lower().strip())
2023-08-17 23:53:26 -04:00
try:
text = text.split("AVOptions:")[1]
except Exception as e:
return f
2023-09-03 16:21:45 -04:00
2023-08-17 23:53:26 -04:00
lines = text.split("\n")
2023-09-03 16:21:45 -04:00
2023-08-17 23:53:26 -04:00
params = []
2023-09-03 16:21:45 -04:00
2023-08-17 23:53:26 -04:00
for l in lines:
if not l.startswith(" "):
continue
issub = l.startswith(" ")
parts = re.split(r" +", l)[1:]
if not issub:
item = {
"name": parts[0],
"type": re.sub("[<>]", "", parts[1]),
"desc": " ".join(parts[3:]),
"min": None,
"max": None,
"default": None,
}
search = re.search(r"\(from (.*?) to (.*?)\)", item["desc"])
if search:
item["min"] = search.group(1)
item["max"] = search.group(2)
search = re.search(r"\(default (.*?)\)", item["desc"])
if search:
item["default"] = search.group(1)
params.append(item)
if item.get("max", 0) in MAXES:
item["max"] = 2000
if item.get("min", 0) in MINS:
item["min"] = -2000
if item.get("default", 0) in MAXES:
item["default"] = 2000
if item.get("default", 0) in MINS:
item["default"] = -2000
2023-08-17 23:53:26 -04:00
if item["default"] == "nan":
item["default"] = None
if item["type"] == "float" or item["type"] == "double":
item["min"] = float(item["min"])
item["max"] = float(item["max"])
if item["default"]:
item["default"] = float(item["default"])
if item["type"] in ["int", "int64", "int32"]:
2023-08-17 23:53:26 -04:00
try:
item["min"] = int(item["min"])
item["max"] = int(item["max"])
if item["default"]:
item["default"] = int(item["default"])
except Exception as e:
pass
2023-08-18 17:23:56 -04:00
if type(item["default"]) == str:
item["default"] = item["default"].replace("'", "").replace('"', "")
2023-09-03 16:21:45 -04:00
if item["name"] == "inputs" and item["default"]:
f["inputs"] = [f["outputs"][0]] * item["default"]
if item["name"] == "outputs" and item["default"]:
f["outputs"] = [f["inputs"][0]] * item["default"]
2023-08-17 23:53:26 -04:00
else:
item = {"value": parts[0], "desc": " ".join(parts[3:])}
if "options" not in params[-1]:
params[-1]["options"] = []
params[-1]["options"].append(item)
f["params"] = params
if f["name"] in OPTION_OVERRIDES:
for key, val in OPTION_OVERRIDES[f["name"]].items():
for p in f["params"]:
if p["name"] == key:
p["options"] = val
p["default"] = val[0]["value"]
2023-08-17 23:53:26 -04:00
return f
filters = get_names()
filters = [get_params(f) for f in filters]
with open("./src/filters.json", "w") as outfile:
json.dump(filters, outfile, indent=2)