Browse documentation
aner.media

Aner Media

Coordinate audio and video through one native demux session, preserve shared timestamps and route bounded analysis events.

ImplementedAner 0.1.0 or newer

aner.media opens selected audio and video tracks through one native demux session. It preserves a common media clock while handing PCM blocks to audio, signal and speech analysis and RGB frames to vision. It requires installed FFmpeg libraries. See MEDIA_ANALYSIS_API.md for every callable.

aner
import aner.audio
import aner.video
import aner.media

let session = media.open("recording.mkv")
let audio_block = audio.block_slot()
let video_frame = video.frame_slot()
var status = session.read_into(audio_block, video_frame)
while status == "audio" || status == "video" || status == "timeout" {
    if status == "audio" {
        print(audio_block.time())
    }
    if status == "video" {
        print(video_frame.pts_ns())
    }
    status = session.read_into(audio_block, video_frame)
}
if status == "failed" {
    print(session.error())
}
session.close()

One demux owner and two clocks of interest

The session owns one input and its selected decoders. audio_stream: -1 and video_stream: -1 choose the best available track of each type. Use a nonnegative container stream index to select a specific track; -2 disables that media type. At least one selected audio/video track must exist. No synthetic audio is invented for a video only source.

normalize_pts: true uses one common container/selected-track origin. It never normalizes each track independently. A file whose video starts at 1.0 seconds and audio starts at 1.25 seconds therefore retains a 250 ms audio delay after normalization. session.origin_pts_ns() reports the retained origin when has_origin() is true. With normalize_pts: false, output keeps source media timestamps. Missing PTS remain explicitly unknown.

Decoded blocks share session/source IDs. Audio first_sample indexes the selected audio track and need not equal presentation seconds multiplied by sample rate. audio_block.time() gives its presentation anchor; audio_anchor.timeline(seconds) maps source sample feature or speech timestamps onto that clock. Preserve the matching anchor when handing work to another worker. Host receipt timestamps measure processing arrival, not capture or lip synchronization accuracy.

Events follow bounded demux/decoder progress. Delayed video frames can emerge after later timestamped audio events, so the combined event sequence is not a presentation time sorting guarantee. Each event retains its own PTS. A visualization or application can order retained results explicitly rather than changing the recorded timestamps.

Reusable slots and status

session.read_into(audio_block, video_frame, timeout_ms: 1000) returns audio, video, timeout, end, cancelled, closed or failed. Only the slot selected by an audio/video event is valid; the other is cleared. Timeout, EOF and failure clear both slots. A timeout does not imply silence or a missing video frame. Native cancellation is available through the C++ API; the current source language call has no external cancellation token parameter.

An audio event is Float32 interleaved PCM with actual rate/channel layout. A video event is packed CPU RGB8 compatible with vision.prepare_into. There is no implicit GPU residency or whole recording tensor allocation. Midstream rate/channel changes are rejected rather than reinterpreting existing processing state.

Use audio_block.freeze() for retained immutable audio. media.freeze_video(video_frame, memory_mb: 64) creates an independent immutable video snapshot. Video copies share an aggregate quota per reusable slot, so repeated copies cannot evade the configured retained byte bound. Changing that quota while copies remain alive is rejected. Media fills clear any old video.Source snapshot association; retained media frames never charge another source's hidden quota. Existing copies remain valid after slot reuse and session close.

Close explicitly to release the demuxer, codecs and transport. Aliases refer to the same mutable session. Concurrent read calls serialize; close requests interrupt in flight library I/O cooperatively. Decoder state is flushed at EOF so delayed frames are delivered before end.

Budgets and remote input

Audio events use the configured audio value limits; the default is 256 MiB per slot with 100 million frames and 3600 seconds as native value bounds. Decoder audio blocks are capped before PCM allocation. Video RGB output and encoded packets have separate 64 MiB defaults. The caller's video slot can impose a smaller bound. Decoder reference frames, demux buffers, conversions and caller retained snapshots are additional memory. Neither the slot budget nor a timeout is a hard whole process or hard real time guarantee.

Local file paths and clean unauthenticated HTTP(S)/RTSP(S) endpoints are accepted. URL userinfo, queries and fragments are rejected; no authorization header adapter is supplied by this session API. Encrypted transports enable TLS verification. Insecure protocols remain insecure when explicitly selected. Underlying protocol access is restricted, errors do not include endpoint credentials, and the runtime uses linked libraries rather than a shell or another language runtime.

The module coordinates timestamps from one input; it does not synchronize independent USB camera/microphone clocks, infer a shared clock between separately opened URLs, provide WebRTC negotiation, or record synchronized A/V containers. Use a common clock recording when cross modal timing matters. Native tests generate a local FFV1/PCM Matroska fixture and verify raw/normalized PTS, nonzero A/V offset, selected tracks, EOF drain and immutable frame ownership without capturing devices.

See media_sync.aner for status handling and analysis integration.

See the complete callable index for the native module and method name catalogue.

Guides and references for aner.media

Aner documentation · Reference and guides