typed-ffmpeg icon indicating copy to clipboard operation
typed-ffmpeg copied to clipboard

When wrapping a Python reserved keyword as a parameter, use xxx_ instead of _xxx to ensure proper autocomplete functionality.

Open lucemia opened this issue 10 months ago • 2 comments
trafficstars

lucemia avatar Jan 09 '25 02:01 lucemia

the issue is that some parameters is start from number

lucemia avatar Jan 26 '25 15:01 lucemia

When writing an FFMPEG wrapper in Python, handling parameters that conflict with Python keywords (e.g., pass) or those that start with numbers (e.g., 3d) requires a clean and intuitive naming convention. Here are some good approaches:

Recommended Naming Strategies

  1. Use a Trailing Underscore for Keywords

Python allows identifiers with a trailing underscore to avoid conflicts:

def ffmpeg_filter(pass_: str, format_: str): return f"-filter:v format={format_} -filter:a pass={pass_}"

✅ Pros: • Readable and follows Python’s naming conventions. • Works well with autocompletion.

  1. Prefix Numbers with a Letter

For parameters that start with a number (e.g., 3d), prefixing with a letter (such as _ or n) ensures valid Python identifiers:

def ffmpeg_filter(n3d: str): return f"-vf 3d={n3d}"

Alternatively, an underscore prefix:

def ffmpeg_filter(_3d: str): return f"-vf 3d={_3d}"

✅ Pros: • Keeps parameter names close to their original meaning. • Avoids unnecessary transformations.

  1. Use a Dictionary for Flexible Parameter Naming

Instead of using Python parameter names directly, pass a dictionary where keys match the original FFMPEG parameter names:

def ffmpeg_filter(**kwargs): return " ".join(f"-{k} {v}" for k, v in kwargs.items())

Usage

cmd = ffmpeg_filter(pass="fast", format="yuv420p", _3d="depth")

✅ Pros: • Allows full flexibility without renaming parameters. • Avoids conflicts with Python keywords.

❌ Cons: • No IDE autocomplete for parameters. • Requires manual dictionary handling.

  1. Use a Mapping Dictionary Internally

For a balance of flexibility and readability, use a mapping dictionary to translate Python-friendly names to FFMPEG’s parameter names:

FFMPEG_PARAMS = { "pass_": "pass", "format_": "format", "n3d": "3d", }

def ffmpeg_filter(**kwargs): translated = {FFMPEG_PARAMS.get(k, k): v for k, v in kwargs.items()} return " ".join(f"-{k} {v}" for k, v in translated.items())

Usage

cmd = ffmpeg_filter(pass_="fast", format_="yuv420p", n3d="depth")

✅ Pros: • Keeps the original names recognizable. • Avoids conflicts while allowing IDE autocomplete. • Makes it explicit which parameters are handled.

Best Approach?

If you want readability and IDE autocomplete, I recommend trailing underscores for keywords (pass_, format_) and prefixing numbers with n or _ (n3d, _3d).

If you prefer maximum flexibility, use a dictionary-based approach to avoid conflicts altogether.

Would you like me to draft a reusable wrapper function for your use case? 🚀

lucemia avatar Feb 03 '25 09:02 lucemia