From 6aeb691bf2f1189e8c5277bd90ab7aa40221c452 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Mon, 8 Sep 2025 22:28:45 +0900 Subject: [PATCH] [F] fix single param --- formtool/__init__.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/formtool/__init__.py b/formtool/__init__.py index 3472b42..a91c542 100644 --- a/formtool/__init__.py +++ b/formtool/__init__.py @@ -1,4 +1,5 @@ import glob +import traceback from pathlib import Path from subprocess import check_call @@ -59,16 +60,29 @@ def main(fmt: str, files: list[str], keep: bool, passthrough: list[str]): for inf in files: printc("&e-----------------------------------------") try: - params = defaults[fmt].copy() + params: dict[str, str | None] = defaults[fmt].copy() old_size = inf.stat().st_size # Check for any passthrough arguments and add them to params (overrides defaults) - _tmp = iter(passthrough) - for k, v in zip(_tmp, _tmp): - printc(f"&a> Overriding parameter: {k} {v} (was {params.get(k, 'not set')})") - params[k] = v + i = 0 + while i < len(passthrough): + k = passthrough[i] + # Check if next item exists and is not a flag (i.e., it's a value) + if i + 1 < len(passthrough) and not passthrough[i+1].startswith('-'): + v = passthrough[i+1] + printc(f"&a> Overriding parameter: {k} {v} (was {params.get(k, 'not set')})") + params[k] = v + i += 2 + else: # It's a standalone flag + printc(f"&a> Overriding parameter: {k} (was {params.get(k, 'not set')})") + params[k] = None # Use None to signify a flag without a value + i += 1 + + end = suffixes[fmt] + for ph, v in params.items(): + end = end.replace(f'{{{ph}}}', str(v) if v is not None else '') + end = ''.join(c for c in end if c.isalnum() or c in ' ._-+').rstrip() - end = suffixes[fmt].format(**params) if inf.name.endswith(end): printc(f"&c> Error: File already has target suffix '{end}', skipping: {inf.name}") continue @@ -78,7 +92,7 @@ def main(fmt: str, files: list[str], keep: bool, passthrough: list[str]): # Construct and run the ffmpeg command cmd = ['ffmpeg', '-hide_banner', '-i', str(inf), - *sum(([k, v] for k, v in params.items()), []), + *sum(([k] if v is None else [k, str(v)] for k, v in params.items()), []), str(ouf)] printc(f"&e> Running command: {' '.join(cmd)}") @@ -92,11 +106,13 @@ def main(fmt: str, files: list[str], keep: bool, passthrough: list[str]): if new_size >= old_size: printc(f"&c! Warning: Compressed file is not smaller than original. Keeping original file :(") else: + printc(f"&e- Removing original file: '{inf.name}'") inf.unlink() - printc(f"&e- Removed original file: '{inf.name}'") + printc(f"&a> Original file removed.") print() except Exception as e: printc(f"&c! An error occurred while processing {inf.name}: {e}") printc("&c! Leaving original file intact.\n") + traceback.print_exc()