Skip to content

media

Class info

Classes

Name Children Inherits
Probe
framesheet.media
What the container says about the (first) video track.

    🛈 DocStrings

    Container probing for video inputs (no ffmpeg): format, codec, size, frame rate.

    Reads just enough of ISO BMFF (mp4/mov), Matroska/WebM and GIF/WebP headers. Everything is best effort: unknown fields are None and the browser's values win where both exist. Ported 1:1 in js/src/media.ts.

    Probe dataclass

    What the container says about the (first) video track.

    Source code in src/framesheet/media.py
    @dataclass
    class Probe:
        """What the container says about the (first) video track."""
    
        container: str
        codec: str | None = None
        width: int | None = None
        height: int | None = None
        fps: float | None = None
        frame_count: int | None = None
        duration: float | None = None
    
        @property
        def mime(self) -> str:
            return MIME_TYPES[self.container]
    
        @property
        def animated_image(self) -> bool:
            return self.container in {"gif", "webp"}
    

    probe

    probe(data: bytes, container: str | None = None) -> Probe
    

    Parse the container header (container defaults to :func:sniff).

    Source code in src/framesheet/media.py
    def probe(data: bytes, container: str | None = None) -> Probe:
        """Parse the container header (``container`` defaults to :func:`sniff`)."""
        kind = sniff(data) or container
        if kind is None:
            msg = "Not a supported video or animated image (mp4, mov, webm, mkv, gif, webp)"
            raise ValueError(msg)
        result = Probe(container=kind)
        try:
            if kind in {"mp4", "mov"}:
                _mp4(data, result)
            elif kind in {"webm", "mkv"}:
                _matroska(data, result)
            elif kind == "gif":
                result.codec = "gif"
                result.width, result.height = struct.unpack_from("<HH", data, 6)
            elif kind == "webp":
                result.codec = "webp"
                _webp(data, result)
        except struct.error, IndexError, ValueError, UnicodeDecodeError:
            pass
        if result.fps is not None:
            result.fps = round(result.fps, 3)
        if result.duration is not None:
            result.duration = round(result.duration, 4)
        return result
    

    sniff

    sniff(data: bytes) -> str | None
    

    Container from magic bytes (None if not a supported video/animated image).

    Source code in src/framesheet/media.py
    def sniff(data: bytes) -> str | None:
        """Container from magic bytes (``None`` if not a supported video/animated image)."""
        if data[4:8] == b"ftyp":
            return "mov" if data[8:12] == b"qt  " else "mp4"
        if data[:4] == b"\x1a\x45\xdf\xa3":
            return "webm" if b"webm" in data[:64] else "mkv"
        if data[:6] in {b"GIF87a", b"GIF89a"}:
            return "gif"
        if data[:4] == b"RIFF" and data[8:12] == b"WEBP":
            return "webp"
        return None