Skip to content

utils

Utility functions for parsing FFmpeg help output.

Functions:

Name Description
glob

Glob a tree of sections and options.

parse_av_option

Parse a section of AVOptions from a tree structure into a list of FFMpegAVOption objects.

parse_general_option

Parse a section of general options from a tree structure into a list of FFMpegOption objects.

parse_section_tree

Parse indented help text into a nested tree structure, preserving section hierarchy.

run_ffmpeg_command

Execute an ffmpeg command with the provided arguments and return its standard output as a string.

glob

glob(
    tree: dict[str, Any], pattern: str
) -> list[tuple[str, dict[str, Any]]]

Glob a tree of sections and options.

Parameters:

Name Type Description Default
tree dict[str, Any]

The tree to glob

required
pattern str

The pattern to match

required

Returns:

Type Description
list[tuple[str, dict[str, Any]]]

An iterable of tuples containing the matched key and value

parse_av_option

parse_av_option(
    section: str,
    tree: dict[str, dict[str, dict[str, None]]],
) -> list[FFMpegAVOption]

Parse a section of AVOptions from a tree structure into a list of FFMpegAVOption objects.

Parameters:

Name Type Description Default
section str

The section name to parse (e.g., 'AVOptions')

required
tree dict[str, dict[str, dict[str, None]]]

The tree structure as produced by parse_section_tree()

required

Returns:

Type Description
list[FFMpegAVOption]

A list of FFMpegAVOption objects, each representing an option with its possible choices

Raises:

Type Description
AssertionError

If the expected option or choice format is not matched

parse_general_option

parse_general_option(
    section: str,
    tree: dict[str, dict[str, dict[str, None]]],
) -> list[FFMpegOption]

Parse a section of general options from a tree structure into a list of FFMpegOption objects.

Parameters:

Name Type Description Default
section str

The section name to parse (e.g., 'Main options')

required
tree dict[str, dict[str, dict[str, None]]]

The tree structure as produced by parse_section_tree()

required

Returns:

Type Description
list[FFMpegOption]

A list of FFMpegOption objects, each representing a general ffmpeg option

Raises:

Type Description
AssertionError

If a general option is found to have choices (which is not expected)

parse_section_tree

parse_section_tree(text: str) -> dict[str, Any]

Parse indented help text into a nested tree structure, preserving section hierarchy.

Parameters:

Name Type Description Default
text str

The help text to parse, typically from ffmpeg's help output

required

Returns:

Type Description
dict[str, Any]

A nested ordered dictionary representing the section hierarchy, where each key is a section or option name

Example

Input text:

Section1
  Option1
    SubOption1
Section2
  Option2

Output:

OrderedDict({
    "Section1": OrderedDict({
        "Option1": OrderedDict({"SubOption1": OrderedDict()})
    }),
    "Section2": OrderedDict({"Option2": OrderedDict()}),
})

run_ffmpeg_command

run_ffmpeg_command(args: Sequence[str]) -> str

Execute an ffmpeg command with the provided arguments and return its standard output as a string.

Parameters:

Name Type Description Default
args Sequence[str]

The command line arguments to pass to ffmpeg (excluding the 'ffmpeg' executable itself)

required

Returns:

Type Description
str

The standard output produced by the ffmpeg command

Example
>>> run_ffmpeg_command(["-version"])
'ffmpeg version ...'