From dfde504c68a210db0ff7537fa5de30f55ab4820c Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Mon, 8 Sep 2025 13:45:50 +0900 Subject: [PATCH] [+] Colors --- formtool/__main__.py | 32 +++++++++++++++++++++----------- pyproject.toml | 4 +++- uv.lock | 23 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 uv.lock diff --git a/formtool/__main__.py b/formtool/__main__.py index 1874723..614d24e 100644 --- a/formtool/__main__.py +++ b/formtool/__main__.py @@ -3,6 +3,8 @@ from pathlib import Path import argparse import sys +from hypy_utils import printc + def main(): agupa = argparse.ArgumentParser("formtool", "ffmpeg shortcuts") @@ -12,14 +14,14 @@ def main(): # Process each file provided on the command line for inf in args.files: - inf = Path(inf) + inf: Path = Path(inf) if not inf.exists(): - print(f"Error: File not found, skipping: {inf}", file=sys.stderr) + printc(f"&cError: File not found, skipping: {inf}") continue - ouf = f'{inf.stem}.comp.av1.mp4' - print(f"-> Compressing '{inf.name}'...") + ouf = inf.with_name(f'{inf.stem}.comp.av1.mp4') + printc(f"&e-> Compressing '{inf.name}' > '{ouf.name}'") try: params = { @@ -30,28 +32,36 @@ def main(): '-b:a': '96k', '-vbr': 'on', } + 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): - print(f" Overriding parameter: {k} {v} (was {params.get(k, 'not set')})") + printc(f"&a Overriding parameter: {k} {v} (was {params.get(k, 'not set')})") params[k] = v # Construct and run the ffmpeg command cmd = ['ffmpeg', '-hide_banner', '-i', inf.absolute(), *sum(([k, v] for k, v in params.items()), []), ouf] - print(f" Running command: {' '.join(cmd)}") + printc(f"&e Running command: {' '.join(cmd)}") check_call(cmd) - print(f" Compression successful.") + printc(f"&a Compression successful :)") + new_size = ouf.stat().st_size + ratio = new_size / old_size + printc(f"&a Size: {old_size / 1_000_000:.2f} MB -> {new_size / 1_000_000:.2f} MB ({ratio:.2%})") if not args.keep: - inf.unlink() - print(f" Removed original file: '{inf.name}'") + if new_size >= old_size: + printc(f"&c Warning: Compressed file is not smaller than original! Keeping original file.") + else: + inf.unlink() + printc(f"&c Removed original file: '{inf.name}'") print() except Exception as e: - print(f"An error occurred while processing {inf.name}: {e}", file=sys.stderr) - print(" Leaving original file intact.\n", file=sys.stderr) + printc(f"&cAn error occurred while processing {inf.name}: {e}") + printc("&c Leaving original file intact.\n") if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index de65001..c96e2cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,4 +4,6 @@ version = "0.1.0" description = "ffmpeg command shortcuts" readme = "README.md" requires-python = ">=3.10" -dependencies = [] +dependencies = [ + "hypy-utils>=1.0.29", +] diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..aec7638 --- /dev/null +++ b/uv.lock @@ -0,0 +1,23 @@ +version = 1 +revision = 3 +requires-python = ">=3.10" + +[[package]] +name = "formtool" +version = "0.1.0" +source = { virtual = "." } +dependencies = [ + { name = "hypy-utils" }, +] + +[package.metadata] +requires-dist = [{ name = "hypy-utils", specifier = ">=1.0.29" }] + +[[package]] +name = "hypy-utils" +version = "1.0.29" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/e1/02/6c93a5f7be972d55b4a29c14ffa4d9e1ae919449084861584dd2c18ba12a/hypy_utils-1.0.29.tar.gz", hash = "sha256:c72217df93e62f1c9af955e4f8cecd92ef51428c6c7399efc824cfb41fd9c4bd", size = 1403860, upload-time = "2024-12-10T11:51:14.233Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e8/34/7e4f6c09da586d317a74958e6b853da8a4c220898c5a8adf98030abb1492/hypy_utils-1.0.29-py3-none-any.whl", hash = "sha256:146a5aa85d3b4bc175e64d3f9e26befd0d87d221c4366eb70e137e46933eb65d", size = 18099, upload-time = "2024-12-10T11:51:12.019Z" }, +]