01 — Open and probe a video

The smallest useful dasVideo step: open an MPEG-1 file and read back what it is — its size, frame rate, duration, and whether it carries an audio track. No decoding yet, no window; just the opaque VideoPlayer? handle and the cheap info getters. Everything later builds on this.

The library

open_probe_tut.das is a tiny library: one function that opens a clip, copies its info into a plain struct, and closes it again. video_open returns null on failure, so the caller checks ok before trusting the rest. The defer guarantees video_close runs however we leave the function — the canonical open/close idiom you’ll see in every tutorial.

require video
require daslib/defer

// A plain, copyable snapshot of a clip's stream info. `ok` is false if the file
// couldn't be opened — the caller checks it before trusting the other fields.
struct VideoInfo {
    ok         : bool
    width      : int
    height     : int
    framerate  : double
    duration   : double
    has_audio  : bool
    samplerate : int
}

// Open `path`, read its info, and close it again. video_open returns null on
// failure; the defer guarantees video_close runs no matter how we leave the
// function — the canonical open/close idiom.
def public probe_video(path : string) : VideoInfo {
    var player = video_open(path)
    if (player == null) return VideoInfo()
    defer() { video_close(player) }
    return VideoInfo(
        ok = true,
        width = video_width(player),
        height = video_height(player),
        framerate = video_framerate(player),
        duration = video_duration(player),
        has_audio = video_has_audio(player),
        samplerate = video_samplerate(player))
}

Note the getters are all valid on a freshly-opened player — you don’t have to decode a frame first to learn the stream’s shape. video_has_audio / video_samplerate describe the audio stream without enabling it (enabling audio is a later step).

Self-verifying

Each tutorial ships a headless test that doubles as its runnable demo and its CI gate. This one probes the bundled sample.mpg (an ffmpeg testsrc 64×48 clip with a 44100 Hz tone) and checks the reported info against its known shape.

require open_probe_tut

[export]
def main() {
    let info = probe_video("tutorials/01_open_probe/sample.mpg")
    verify(info.ok, "the sample clip opens")
    verify(info.width == 64, "width is 64")
    verify(info.height == 48, "height is 48")
    verify(info.duration > 0.0lf, "duration is positive")
    verify(info.has_audio, "the sample has an audio track")
    verify(info.samplerate == 44100, "audio is 44100 Hz")
    print("tutorial 01 ok: {info.width}x{info.height} @ {info.framerate} fps, {info.duration} s, audio {info.samplerate} Hz\n")
}

Running it

Run it from the repo root — the test opens its sample with a repo-root-relative path, matching the convention the other tests use:

cd <dasVideo>
daslang -load_module . tutorials/01_open_probe/test_open_probe.das

prints:

tutorial 01 ok: 64x48 @ 25 fps, 1.96 s, audio 44100 Hz

Next

02 — Decode frames and borrow pixels starts decoding: the frame loop, the borrow model (get_data hands you the pixels without copying), and video_rewind to loop.