/** * ISO BMFF (fMP4) box parsing utilities for working with fragmented MP4 data. * * This module provides lightweight, Buffer-based utilities for inspecting ISO Base Media File Format (ISO BMFF) structures commonly found in fragmented MP4 (fMP4) * streams. It enables locating specific box types, splitting fragments into their moof/mdat components, detecting keyframe (sync sample) segments by parsing the TRUN * sample flags, and identifying audio track presence in initialization segments. * * These utilities operate on complete Buffers and are independent of FFmpeg processes or streaming pipelines. * * @module */ import type { Nullable } from "../util.ts"; /** * ISO BMFF box header size in bytes: 4 bytes big-endian size + 4 bytes ASCII type. * * @category FFmpeg */ export declare const BOX_HEADER_SIZE = 8; /** * TRUN fullbox flags bit indicating that a `data_offset` field follows the sample_count in the box header. * * @category FFmpeg */ export declare const TRUN_FLAG_DATA_OFFSET = 1; /** * TRUN fullbox flags bit indicating that a `first_sample_flags` field follows (after `data_offset` if present). When this flag is set, the first sample's flags are * stored in a dedicated header field rather than in the per-sample entries - the common arrangement for fragments emitted with FFmpeg's `frag_keyframe` movflag. * * @category FFmpeg */ export declare const TRUN_FLAG_FIRST_SAMPLE_FLAGS = 4; /** * TRUN fullbox flags bit indicating that each sample entry carries a 4-byte duration field. * * @category FFmpeg */ export declare const TRUN_FLAG_SAMPLE_DURATION = 256; /** * TRUN fullbox flags bit indicating that each sample entry carries a 4-byte size field. * * @category FFmpeg */ export declare const TRUN_FLAG_SAMPLE_SIZE = 512; /** * TRUN fullbox flags bit indicating that each sample entry carries a 4-byte sample_flags field. * * @category FFmpeg */ export declare const TRUN_FLAG_SAMPLE_FLAGS = 1024; /** * Sample-flags bit indicating a non-sync (non-keyframe) sample. When this bit is clear (0), the sample is a sync sample / IDR frame / keyframe. * * @category FFmpeg */ export declare const SAMPLE_FLAG_NON_SYNC = 65536; /** * Handler-type code for audio tracks in ISO BMFF `hdlr` boxes: ASCII `"soun"` encoded as a 32-bit big-endian integer. Compared against the `handler_type` field of * each track's `hdlr` fullbox to identify the audio track during init-segment inspection. * * @category FFmpeg */ export declare const HDLR_TYPE_SOUN = 1936684398; /** * Describes the location of an ISO BMFF box within a buffer. * * @property offset - The byte offset of the box start (including the header). * @property size - The total box size in bytes (including the header). * * @category FFmpeg */ export interface FMp4Box { offset: number; size: number; } /** * Locates the first ISO BMFF box of a given type within a byte range. * * Walks the standard box headers (4-byte big-endian size + 4-byte ASCII type) starting at `start` and ending at `end`. Returns the offset and size of the first * matching box. Returns `null` when no box of the requested type is found in range, and also when the walk encounters a box whose declared size is invalid - below * the header size, extending past the search range, or an extended/open-ended size (64-bit size field, uncommon in fMP4 livestream contexts and unsupported here) - * since a malformed size makes it unsafe to keep walking; the two cases are indistinguishable from the return value alone. * * @param buffer - The buffer containing ISO BMFF box data. * @param type - The 4-character ASCII box type to search for (e.g. "moof", "traf", "trun"). Must be exactly 4 characters. * @param start - Optional. The byte offset to begin searching from. Defaults to 0. * @param end - Optional. The byte offset to stop searching at. Defaults to the buffer length. * * @returns The box location, or `null` if not found. * * @category FFmpeg */ export declare function findBox(buffer: Buffer, type: string, start?: number, end?: number): Nullable; /** * Determines whether an fMP4 segment contains a keyframe (sync sample) by parsing the TRUN sample flags. * * Traverses the box hierarchy `moof -> traf -> trun` and inspects the sample flags to determine if the first sample is a sync sample (keyframe/IDR frame). Checks * `first_sample_flags` first (the common case for fragments generated with `frag_keyframe`), then falls back to per-sample flags if available. Returns `false` if the * box structure cannot be parsed or if the flags indicate a non-sync sample. * * @param segment - A buffer containing a complete fMP4 segment (typically a moof+mdat pair). * * @returns `true` if the segment's first sample is a sync sample (keyframe), `false` otherwise. * * @category FFmpeg */ export declare function isKeyframe(segment: Buffer): boolean; /** * Determines whether an fMP4 initialization segment contains an audio track by inspecting the handler type in each track's media handler box. * * Traverses the box hierarchy `moov -> trak -> mdia -> hdlr` for every track in the init segment and checks the handler_type field for "soun" (0x736F756E). This is the * standard ISO BMFF mechanism for identifying track media types - "soun" for audio, "vide" for video, "subt" for subtitles, etc. * * @param initSegment - A buffer containing a complete fMP4 initialization segment (typically ftyp + moov). * * @returns `true` if the init segment contains at least one audio track, `false` otherwise. * * @category FFmpeg */ export declare function hasAudioTrack(initSegment: Buffer): boolean; /** * Splits an fMP4 fragment into its moof and mdat components. * * Locates the `mdat` box and returns everything before it as the moof portion (which includes the moof box and any preceding metadata boxes) and everything from the * mdat box to the end of the fragment as the mdat portion. The returned buffers are subarray views into the original buffer, so no data is copied. Returns `null` if * the mdat box cannot be found. * * @param fragment - A buffer containing a complete fMP4 fragment. * * @returns An object with `moof` and `mdat` sub-buffers, or `null` if the structure cannot be parsed. * * @category FFmpeg */ export declare function splitMoofMdat(fragment: Buffer): Nullable<{ mdat: Buffer; moof: Buffer; }>; //# sourceMappingURL=fmp4.d.ts.map