weirdnet-media 0.2.0

Weirdnet media processing for clients and nodes: WAV probing and PCM in pure Rust, 16 kHz resampling, silence-aware chunking, long-audio transcription plans, long-text speech plans, SRT/VTT, video frame planning, and a hardened ffmpeg runner with a test fake.
Documentation
//! SRT and WebVTT from transcript segments.

use crate::transcribe::Segment;

/// `HH:MM:SS,mmm`.
pub fn srt_time(ms: u64) -> String {
    let (h, m, s, f) = parts(ms);
    format!("{h:02}:{m:02}:{s:02},{f:03}")
}

/// `HH:MM:SS.mmm`.
pub fn vtt_time(ms: u64) -> String {
    let (h, m, s, f) = parts(ms);
    format!("{h:02}:{m:02}:{s:02}.{f:03}")
}

fn parts(ms: u64) -> (u64, u64, u64, u64) {
    (ms / 3_600_000, ms / 60_000 % 60, ms / 1000 % 60, ms % 1000)
}

/// Text on non-empty lines (a blank line would end the cue).
fn lines(text: &str) -> String {
    text.lines().map(str::trim).filter(|l| !l.is_empty()).collect::<Vec<_>>().join("\n")
}

/// SubRip. Segments with no text are skipped; cues are numbered from 1.
pub fn srt(segments: &[Segment]) -> String {
    let mut out = String::new();
    let mut n = 0;
    for s in segments {
        let text = lines(&s.text.replace("-->", "->"));
        if text.is_empty() {
            continue;
        }
        n += 1;
        out.push_str(&format!("{n}\n{} --> {}\n{text}\n\n", srt_time(s.start_ms), srt_time(s.end_ms.max(s.start_ms))));
    }
    out
}

/// WebVTT. Segments with no text are skipped. A [`Segment::coarse`] cue is preceded by a `NOTE`
/// saying it spans a whole piece, so a reader knows its timing is approximate.
pub fn vtt(segments: &[Segment]) -> String {
    let mut out = String::from("WEBVTT\n\n");
    for s in segments {
        let text = lines(&s.text.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;"));
        if text.is_empty() {
            continue;
        }
        if s.coarse {
            out.push_str("NOTE coarse: this cue spans a whole piece of audio\n\n");
        }
        out.push_str(&format!("{} --> {}\n{text}\n\n", vtt_time(s.start_ms), vtt_time(s.end_ms.max(s.start_ms))));
    }
    out
}