2026-04-22 21:13:21 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
# Copyright 2026 The HuggingFace Inc. team. All rights reserved.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
"""PyAV-based compatibility checks for :class:`VideoEncoderConfig`.
|
|
|
|
|
|
|
|
|
|
Centralises all :mod:`av` introspection of the bundled FFmpeg build.
|
|
|
|
|
Checks degrade to a no-op when the target codec isn't available locally.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import functools
|
|
|
|
|
import logging
|
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
|
|
|
|
|
|
import av
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from lerobot.datasets.video_utils import VideoEncoderConfig
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
FFMPEG_NUMERIC_OPTION_TYPES = ("INT", "INT64", "UINT64", "FLOAT", "DOUBLE")
|
2026-04-30 14:23:48 +02:00
|
|
|
FFMPEG_INTEGER_OPTION_TYPES = ("INT", "INT64", "UINT64")
|
2026-04-22 21:13:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@functools.cache
|
|
|
|
|
def get_codec(vcodec: str) -> av.codec.Codec | None:
|
|
|
|
|
"""PyAV write-mode ``Codec`` for *vcodec*, or ``None`` if unavailable."""
|
|
|
|
|
try:
|
|
|
|
|
return av.codec.Codec(vcodec, "w")
|
|
|
|
|
except Exception:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@functools.cache
|
|
|
|
|
def _get_codec_options_by_name(vcodec: str) -> dict[str, av.option.Option]:
|
|
|
|
|
"""Private-option name → PyAV ``Option`` for *vcodec* (empty if unavailable)."""
|
|
|
|
|
codec = get_codec(vcodec)
|
|
|
|
|
if codec is None:
|
|
|
|
|
return {}
|
|
|
|
|
return {opt.name: opt for opt in codec.descriptor.options}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@functools.cache
|
|
|
|
|
def _get_codec_video_formats(vcodec: str) -> tuple[str, ...]:
|
|
|
|
|
"""Pixel formats accepted by *vcodec* in PyAV's preferred order (empty if unknown)."""
|
|
|
|
|
codec = get_codec(vcodec)
|
|
|
|
|
if codec is None:
|
|
|
|
|
return ()
|
|
|
|
|
return tuple(fmt.name for fmt in (codec.video_formats or []))
|
|
|
|
|
|
|
|
|
|
|
2026-04-24 16:43:54 +02:00
|
|
|
def detect_available_encoders_pyav(encoders: list[str] | str) -> list[str]:
|
2026-04-22 21:13:21 +02:00
|
|
|
"""Return the subset of *encoders* available as video encoders in the local FFmpeg build.
|
|
|
|
|
|
2026-04-24 16:43:54 +02:00
|
|
|
Each name is probed directly via :func:`get_codec`; input order is preserved.
|
2026-04-22 21:13:21 +02:00
|
|
|
"""
|
|
|
|
|
if isinstance(encoders, str):
|
|
|
|
|
encoders = [encoders]
|
|
|
|
|
|
2026-04-24 16:43:54 +02:00
|
|
|
available: list[str] = []
|
2026-04-22 21:13:21 +02:00
|
|
|
for name in encoders:
|
2026-04-24 16:43:54 +02:00
|
|
|
codec = get_codec(name)
|
|
|
|
|
if codec is not None and codec.type == "video":
|
2026-04-22 21:13:21 +02:00
|
|
|
available.append(name)
|
|
|
|
|
else:
|
|
|
|
|
logger.debug("encoder '%s' not available as video encoder", name)
|
|
|
|
|
return available
|
|
|
|
|
|
|
|
|
|
|
2026-04-30 14:23:48 +02:00
|
|
|
def _check_option_value(vcodec: str, label: str, value: Any, opt: av.option.Option) -> None:
|
|
|
|
|
"""Range-check numeric *value* and choice-check string *value* against *opt*."""
|
2026-04-22 21:13:21 +02:00
|
|
|
type_name = opt.type.name
|
|
|
|
|
if type_name in FFMPEG_NUMERIC_OPTION_TYPES:
|
2026-04-30 14:23:48 +02:00
|
|
|
if isinstance(value, bool):
|
2026-04-22 21:13:21 +02:00
|
|
|
raise ValueError(
|
2026-04-30 14:23:48 +02:00
|
|
|
f"{label}={value!r} is not numeric; codec {vcodec!r} expects a number for this option."
|
|
|
|
|
)
|
|
|
|
|
elif isinstance(value, str):
|
|
|
|
|
try:
|
|
|
|
|
num_val = float(value)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"{label}={value!r} is not numeric; codec {vcodec!r} expects a number for this option."
|
|
|
|
|
) from e
|
|
|
|
|
elif isinstance(value, (float, int)):
|
|
|
|
|
num_val = value
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"{label}={value!r} is not numeric; codec {vcodec!r} expects a number for this option."
|
2026-04-22 21:13:21 +02:00
|
|
|
)
|
|
|
|
|
|
2026-04-30 14:23:48 +02:00
|
|
|
# Check integer type compatibility
|
|
|
|
|
if type_name in FFMPEG_INTEGER_OPTION_TYPES and not num_val.is_integer():
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"{label}={num_val!r} must be an integer for codec {vcodec!r} "
|
|
|
|
|
f"(FFmpeg option {opt.name!r} is {type_name}); float values are not allowed."
|
|
|
|
|
)
|
2026-04-22 21:13:21 +02:00
|
|
|
|
2026-04-30 14:23:48 +02:00
|
|
|
# Check numeric range compatibility
|
|
|
|
|
lo, hi = float(opt.min), float(opt.max)
|
|
|
|
|
if lo < hi and not (lo <= num_val <= hi):
|
2026-04-30 14:42:37 +02:00
|
|
|
raise ValueError(
|
|
|
|
|
f"{label}={num_val} is out of range for codec {vcodec!r}; must be in [{lo}, {hi}]"
|
|
|
|
|
)
|
2026-04-22 21:13:21 +02:00
|
|
|
|
2026-04-30 14:23:48 +02:00
|
|
|
elif type_name == "STRING":
|
|
|
|
|
if isinstance(value, bool):
|
2026-04-30 14:42:37 +02:00
|
|
|
raise ValueError(f"{label}={value!r} is not a valid string value for codec {vcodec!r}.")
|
2026-04-30 14:23:48 +02:00
|
|
|
if isinstance(value, str):
|
|
|
|
|
str_val = value
|
|
|
|
|
elif isinstance(value, (int, float)):
|
|
|
|
|
str_val = str(value)
|
|
|
|
|
else:
|
2026-04-30 14:42:37 +02:00
|
|
|
raise ValueError(f"{label}={value!r} has unsupported type for STRING option on codec {vcodec!r}")
|
2026-04-22 21:13:21 +02:00
|
|
|
|
2026-04-30 14:23:48 +02:00
|
|
|
# Check string choice compatibility
|
|
|
|
|
choices = [c.name for c in (opt.choices or [])]
|
|
|
|
|
if choices and str_val not in choices:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"{label}={str_val!r} is not a supported choice for codec "
|
|
|
|
|
f"{vcodec!r}; valid choices: {choices}"
|
|
|
|
|
)
|
2026-04-22 21:13:21 +02:00
|
|
|
else:
|
2026-04-30 14:23:48 +02:00
|
|
|
return
|
2026-04-22 21:13:21 +02:00
|
|
|
|
|
|
|
|
|
2026-04-30 14:23:48 +02:00
|
|
|
def _check_pixel_format(vcodec: str, pix_fmt: str) -> None:
|
|
|
|
|
formats = _get_codec_video_formats(vcodec)
|
2026-04-22 21:13:21 +02:00
|
|
|
if formats and pix_fmt not in formats:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"pix_fmt={pix_fmt!r} is not supported by codec {vcodec!r}; "
|
|
|
|
|
f"supported pixel formats: {list(formats)}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-04-30 14:42:37 +02:00
|
|
|
def _check_codec_options(vcodec: str, codec_options: dict[str, Any], config: VideoEncoderConfig) -> None:
|
2026-04-30 14:23:48 +02:00
|
|
|
"""Validate merged encoder options (typed) against the codec's published AVOptions."""
|
|
|
|
|
supported_options = _get_codec_options_by_name(vcodec)
|
|
|
|
|
for key, value in codec_options.items():
|
|
|
|
|
# GOP size is not a codec-specific option, it has to be validated separately.
|
|
|
|
|
if key == "g":
|
2026-04-24 16:46:13 +02:00
|
|
|
if isinstance(value, bool) or not isinstance(value, int) or value < 1:
|
2026-04-24 17:19:19 +02:00
|
|
|
raise ValueError(f"g={value!r} must be a positive integer for codec {vcodec!r}")
|
2026-04-24 16:46:13 +02:00
|
|
|
continue
|
2026-04-30 14:23:48 +02:00
|
|
|
if key not in supported_options:
|
2026-04-22 21:13:21 +02:00
|
|
|
continue
|
2026-04-30 14:23:48 +02:00
|
|
|
opt = supported_options[key]
|
|
|
|
|
label = f"extra_options[{key!r}]" if key in config.extra_options else key
|
|
|
|
|
_check_option_value(vcodec, label, value, opt)
|
2026-04-22 21:13:21 +02:00
|
|
|
|
|
|
|
|
|
2026-04-24 16:43:54 +02:00
|
|
|
def check_video_encoder_config_pyav(config: VideoEncoderConfig) -> None:
|
2026-04-22 21:13:21 +02:00
|
|
|
"""Verify *config* is compatible with the bundled FFmpeg build.
|
|
|
|
|
|
2026-04-30 14:23:48 +02:00
|
|
|
Checks pixel format, abstract tuning-field compatibility, and each merged
|
|
|
|
|
encoder option from :meth:`~lerobot.datasets.video_utils.VideoEncoderConfig.get_codec_options`
|
|
|
|
|
against PyAV (including numeric ``extra_options`` present in that dict).
|
2026-04-22 21:13:21 +02:00
|
|
|
No-op when ``config.vcodec`` isn't in the local FFmpeg build.
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
ValueError: on the first incompatibility encountered.
|
|
|
|
|
"""
|
|
|
|
|
vcodec = config.vcodec
|
|
|
|
|
options = _get_codec_options_by_name(vcodec)
|
|
|
|
|
if not options:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"Codec %r is not available in the bundled FFmpeg build; ",
|
|
|
|
|
vcodec,
|
|
|
|
|
)
|
|
|
|
|
return
|
2026-04-30 14:23:48 +02:00
|
|
|
_check_pixel_format(config.vcodec, config.pix_fmt)
|
|
|
|
|
_check_codec_options(config.vcodec, config.get_codec_options(), config)
|