use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use crate::ffmpeg::{self, ClipInfo, FfmpegError};
use crate::speech::Segment;
mod transcript;
const OVERVIEW_SECONDS: f64 = 600.0;
const OVERVIEW_FRAMES_MIN: u32 = 8;
const OVERVIEW_FRAME_SIDE: u32 = 512;
const WINDOW_FRAMES_PER_SECOND: f64 = 2.0;
const WINDOW_FRAME_SIDE: u32 = 768;
const FRAMES_MAX: u32 = 24;
const PICTURE_CELLS: usize = 4;
const COVERAGE_SLACK: f64 = 1.0;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
Video,
Audio,
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum Window {
Overview,
Span { start: f64, length: Option<f64> },
}
pub(crate) struct Reading {
pub(crate) path: PathBuf,
pub(crate) text: String,
strip: Option<ffmpeg::Frames>,
}
pub(crate) struct Picture {
pub(crate) data_uri: String,
pub(crate) size: (u32, u32),
frames: Vec<usize>,
}
impl Picture {
pub(crate) fn frames_text(&self) -> String {
frame_numbers(&self.frames)
}
}
impl Reading {
fn frames(&self) -> &[(PathBuf, f64)] {
self.strip.as_ref().map_or(&[], ffmpeg::Frames::frames)
}
pub(crate) async fn pictures(&self) -> Vec<Picture> {
let mut pictures = Vec::with_capacity(self.frames().len().div_ceil(PICTURE_CELLS));
for (index, group) in self.frames().chunks(PICTURE_CELLS).enumerate() {
if let Some(picture) = picture(group, index * PICTURE_CELLS + 1).await {
pictures.push(picture);
}
}
pictures
}
}
async fn picture(group: &[(PathBuf, f64)], first: usize) -> Option<Picture> {
let mut cells = Vec::with_capacity(group.len());
for (index, (jpeg, _)) in group.iter().enumerate() {
match tokio::fs::read(jpeg).await {
Ok(bytes) => cells.push((index, first + index, bytes)),
Err(error) => tracing::warn!(
path = %jpeg.display(),
%error,
"A frame could not be read"
),
}
}
crate::util::with_block_in_place(|| compose(&cells))
}
fn compose(cells: &[(usize, usize, Vec<u8>)]) -> Option<Picture> {
let mut frames = Vec::with_capacity(cells.len());
for (cell, number, jpeg) in cells {
match image::load_from_memory(jpeg) {
Ok(frame) => frames.push((*cell, *number, frame)),
Err(error) => tracing::warn!(frame = number, %error, "A frame could not be decoded"),
}
}
let (width, height) = frames
.iter()
.fold((0u32, 0u32), |(width, height), (_, _, frame)| {
(width.max(frame.width()), height.max(frame.height()))
});
if width == 0 || height == 0 {
return None;
}
let mut canvas = image::RgbImage::from_pixel(width * 2, height * 2, image::Rgb([0, 0, 0]));
for (cell, _, frame) in &frames {
#[expect(
clippy::cast_possible_truncation,
reason = "a picture's cell index is 0..4"
)]
let (column, row) = ((cell % 2) as u32, (cell / 2) as u32);
image::imageops::replace(
&mut canvas,
&frame.to_rgb8(),
i64::from(column * width),
i64::from(row * height),
);
}
let mut jpeg = Vec::new();
if let Err(error) =
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, ffmpeg::FRAME_QUALITY)
.encode_image(&canvas)
{
tracing::warn!(%error, "A picture could not be encoded");
return None;
}
Some(Picture {
data_uri: crate::util::jpeg_data_uri(&jpeg),
size: canvas.dimensions(),
frames: frames.iter().map(|(_, number, _)| *number).collect(),
})
}
fn pictures_attached(pictures: &[Picture]) -> String {
let list = pictures
.iter()
.enumerate()
.map(|(index, picture)| format!("picture {}: {}", index + 1, picture.frames_text()))
.collect::<Vec<_>>()
.join("; ");
format!(
"The frames are attached as {} {} of four, left to right then the next row — {list}.",
pictures.len(),
plural(pictures.len(), "picture", "pictures"),
)
}
fn frame_numbers(frames: &[usize]) -> String {
let mut runs: Vec<(usize, usize)> = Vec::new();
for &frame in frames {
match runs.last_mut() {
Some((_, last)) if *last + 1 == frame => *last = frame,
_ => runs.push((frame, frame)),
}
}
let list = runs
.iter()
.map(|(first, last)| {
if first == last {
first.to_string()
} else {
format!("{first}–{last}")
}
})
.collect::<Vec<_>>()
.join(", ");
format!("{} {list}", plural(frames.len(), "frame", "frames"))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum MediaError {
Unavailable(String),
Undecodable,
Malformed(String),
NoTracks,
SoundTrack(String),
Busy,
Stopping,
TimedOut,
}
impl MediaError {
fn detail(&self) -> String {
match self {
Self::SoundTrack(reason) => reason.clone(),
other => other.message(),
}
}
pub(crate) fn unreadable(&self) -> String {
format!("The clip could not be read: {}.", self.message())
}
pub(crate) fn message(&self) -> String {
match self {
Self::Unavailable(reason) => {
format!("the product's own ffmpeg is not usable ({reason})")
}
Self::Undecodable => "the file could not be decoded as media".to_string(),
Self::Malformed(reason) => {
format!("the product's own media call was malformed ({reason})")
}
Self::NoTracks => "the file holds neither a picture nor a sound track".to_string(),
Self::SoundTrack(reason) => {
format!("the sound track could not be recognised ({reason})")
}
Self::Busy => "another read is still transcribing this file".to_string(),
Self::Stopping => crate::speech::Interrupted.to_string(),
Self::TimedOut => "the decode ran past its time bound".to_string(),
}
}
}
impl From<FfmpegError> for MediaError {
fn from(error: FfmpegError) -> Self {
match error {
FfmpegError::Unavailable(reason) => Self::Unavailable(reason),
FfmpegError::NoVideoTrack | FfmpegError::NoAudioTrack | FfmpegError::Undecodable => {
Self::Undecodable
}
FfmpegError::InvalidOptions(reason) => Self::Malformed(reason.to_string()),
FfmpegError::TimedOut => Self::TimedOut,
}
}
}
pub(crate) fn is_media_path(path: &Path) -> bool {
crate::util::is_video_extension(path) || crate::util::is_audio_extension(path)
}
pub(crate) async fn read(path: &Path, window: Window) -> Result<Reading, MediaError> {
let info = ffmpeg::probe(path).await?;
let kind = kind_of(&info)?;
let duration = info.duration.as_secs_f64();
let span = window.span(duration);
if let Some(start) = starts_late(window, duration) {
return Ok(Reading {
path: path.to_path_buf(),
text: past_the_end(path, kind, &info, start),
strip: None,
});
}
let frame_pass = Box::pin(async {
match kind {
Kind::Video => Some(frames(path, window, info.picture_length().as_secs_f64()).await),
Kind::Audio => None,
}
});
let track = Box::pin(async {
if info.has_audio {
Some(transcript::regions(path, &info).await)
} else {
None
}
});
let (frame_pass, track) = tokio::join!(frame_pass, track);
let (strip, frame_failure) = match frame_pass {
Some(Ok(strip)) => (strip, None),
Some(Err(error)) => {
tracing::warn!(
path = %path.display(),
reason = %error.message(),
"A clip's frames could not be taken"
);
(None, Some(error))
}
None => (None, None),
};
let sound = match track {
Some(transcribed) => match transcribed.failure {
Some(error) if transcribed.reached <= 0.0 => Sound::Failed(error),
stopped => Sound::Heard {
regions: transcribed.regions,
reached: transcribed.reached,
stopped,
},
},
None => Sound::Absent,
};
let text = render(&Timeline {
path,
kind,
info: &info,
window,
span,
frames: strip.as_ref().map_or(&[], ffmpeg::Frames::frames),
frame_failure: frame_failure.as_ref(),
sound: &sound,
words: !crate::audio::voice::is_transcription_disabled(),
});
Ok(Reading {
path: path.to_path_buf(),
text,
strip,
})
}
pub(crate) async fn inline(reading: &Reading) -> (String, bool) {
let pictures = reading.pictures().await;
let mut text = reading.text.clone();
if !pictures.is_empty() {
let _ = write!(text, "\n{}", pictures_attached(&pictures));
for picture in &pictures {
let _ = write!(text, "\n[IMAGE:{}]", picture.data_uri);
}
}
(text, !pictures.is_empty())
}
fn kind_of(info: &ClipInfo) -> Result<Kind, MediaError> {
match (info.dimensions.is_some(), info.has_audio) {
(true, _) => Ok(Kind::Video),
(false, true) => Ok(Kind::Audio),
(false, false) => Err(MediaError::NoTracks),
}
}
#[derive(Debug, Clone, Copy)]
struct Span {
start: f64,
end: f64,
}
impl Span {
fn length(self) -> f64 {
(self.end - self.start).max(0.0)
}
}
impl Window {
fn span(self, duration: f64) -> Span {
match self {
Self::Overview => Span {
start: 0.0,
end: duration.min(OVERVIEW_SECONDS),
},
Self::Span { start, length } => {
let start = start.clamp(0.0, duration);
let end = length.map_or(duration, |length| start + length.max(0.0));
Span {
start,
end: end.min(duration),
}
}
}
}
fn frame_span(self, duration: f64) -> Span {
match self {
Self::Overview => Span {
start: 0.0,
end: duration,
},
Self::Span { .. } => self.span(duration),
}
}
}
struct FramePlan {
marks: Vec<f64>,
side: u32,
}
impl FramePlan {
fn of(window: Window, picture: f64) -> Self {
let span = window.frame_span(picture);
let length = span.length();
let (side, per_second, min) = match window {
Window::Overview => (OVERVIEW_FRAME_SIDE, 1.0, OVERVIEW_FRAMES_MIN),
Window::Span { .. } => (WINDOW_FRAME_SIDE, WINDOW_FRAMES_PER_SECOND, 1),
};
let empty = length <= 0.0;
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a clip's length in whole frames fits u32 by the count's own ceiling"
)]
let count = if empty {
0
} else {
((length * per_second) as u32).clamp(min, FRAMES_MAX)
};
let step = if count == 0 {
0.0
} else {
span.length() / f64::from(count)
};
let mut marks: Vec<f64> = (0..count)
.map(|index| span.start + f64::from(index) * step)
.collect();
if marks.is_empty() && matches!(window, Window::Overview) {
marks.push(span.start);
}
Self { marks, side }
}
}
async fn frames(
path: &Path,
window: Window,
picture: f64,
) -> Result<Option<ffmpeg::Frames>, MediaError> {
let plan = FramePlan::of(window, picture);
if plan.marks.is_empty() {
return Ok(None);
}
Ok(Some(
ffmpeg::extract_frames(
path,
ffmpeg::FrameOptions {
marks: &plan.marks,
max_side: plan.side,
},
)
.await?,
))
}
enum Sound {
Heard {
regions: Vec<Segment>,
reached: f64,
stopped: Option<MediaError>,
},
Absent,
Failed(MediaError),
}
struct Line {
start: f64,
end: f64,
text: String,
}
struct Timeline<'a> {
path: &'a Path,
kind: Kind,
info: &'a ClipInfo,
window: Window,
span: Span,
frames: &'a [(PathBuf, f64)],
frame_failure: Option<&'a MediaError>,
sound: &'a Sound,
words: bool,
}
fn header(path: &Path, kind: Kind, info: &ClipInfo, frames: usize) -> String {
let size = info
.dimensions
.map(|(width, height)| format!(", {width}x{height}"))
.unwrap_or_default();
let duration = clock(info.duration.as_secs_f64());
match kind {
Kind::Video => format!(
"Video {} — {duration}{size}, {frames} {}",
path.display(),
plural(frames, "frame", "frames"),
),
Kind::Audio => format!("Audio {} — {duration}{size}", path.display()),
}
}
fn past_the_end(path: &Path, kind: Kind, info: &ClipInfo, start: f64) -> String {
format!(
"{}\nThe read starts at {}, past the end of the {} file.",
header(path, kind, info, 0),
clock(start),
clock(info.duration.as_secs_f64()),
)
}
fn starts_late(window: Window, duration: f64) -> Option<f64> {
match window {
Window::Span { start, .. } if start >= duration && duration > 0.0 => Some(start),
_ => None,
}
}
fn render(timeline: &Timeline<'_>) -> String {
let mut out = header(
timeline.path,
timeline.kind,
timeline.info,
timeline.frames.len(),
);
if let Some(error) = timeline.frame_failure {
let _ = write!(out, "\nThe frames could not be taken: {}.", error.detail());
}
match timeline.sound {
Sound::Absent => out.push_str("\nThis video has no sound track."),
Sound::Failed(error @ MediaError::SoundTrack(_)) => {
let _ = write!(
out,
"\nThe sound track could not be recognised: {}.",
error.detail()
);
}
Sound::Failed(error) => {
let _ = write!(
out,
"\nThe sound track could not be read: {}.",
error.message()
);
}
Sound::Heard {
stopped: Some(error),
..
} if !timeline.read_through() => {
let _ = write!(
out,
"\nThe sound track could not be read past {} of {}: {}.",
clock(timeline.heard()),
clock(timeline.track()),
error.detail()
);
}
Sound::Heard { .. } => {}
}
let lines = timeline.lines();
if timeline.sound.is_heard() && lines.is_empty() && timeline.read_through() {
out.push_str("\nNothing was heard in this stretch.");
}
let mut frames = timeline
.frames
.iter()
.map(|(path, at)| (path.as_path(), *at))
.enumerate()
.peekable();
let mut lines = lines.into_iter().peekable();
while frames.peek().is_some() || lines.peek().is_some() {
let take_frame = match (frames.peek(), lines.peek()) {
(Some((_, (_, at))), Some(line)) => *at <= line.start,
(Some(_), None) => true,
(None, _) => false,
};
if take_frame {
if let Some((index, (_, at))) = frames.next() {
let _ = write!(out, "\n{} [frame {}]", mark(at), index + 1);
}
} else if let Some(line) = lines.next() {
let (start, end) = (clock(line.start), clock(line.end));
if start == end {
let _ = write!(out, "\n{start} {}", line.text);
} else {
let _ = write!(out, "\n{start}–{end} {}", line.text);
}
}
}
if let Some(cap) = timeline.coverage_note() {
let _ = write!(out, "\n{cap}");
}
out
}
impl Timeline<'_> {
fn heard(&self) -> f64 {
match self.sound {
Sound::Heard { reached, .. } => *reached,
Sound::Absent | Sound::Failed(_) => self.span.start,
}
}
fn lines(&self) -> Vec<Line> {
let Sound::Heard { regions, .. } = self.sound else {
return Vec::new();
};
let mut lines: Vec<Line> = Vec::new();
for region in regions {
let start = region.start.max(self.span.start);
let end = region.end.min(self.span.end);
if end <= start {
continue;
}
let text = match (®ion.text, region.label.as_deref()) {
(Some(_), _) if !self.words => "(Speech)".to_string(),
(Some(text), _) => text.trim().to_string(),
(None, Some(label)) if crate::speech::is_speech_label(label) => {
crate::speech::UNTRANSCRIBED.to_string()
}
(None, Some(label)) if crate::speech::is_silence_label(label) => continue,
(None, Some(label)) => format!("({label})"),
(None, None) => continue,
};
match lines.last_mut() {
Some(last) if last.text == text && last.end >= start => last.end = end,
_ => lines.push(Line { start, end, text }),
}
}
lines
}
fn coverage_note(&self) -> Option<String> {
if !self.sound.is_heard() || self.sound.stopped() {
return None;
}
let track = self.track();
let available = self.span.end.min(track);
let capped =
matches!(self.window, Window::Overview) && self.span.end < track - COVERAGE_SLACK;
(!self.read_through() || capped).then(|| {
format!(
"The transcript covers 0:00–{} of {} — read further with offset/limit.",
clock(self.heard().min(available)),
clock(track)
)
})
}
fn read_through(&self) -> bool {
self.heard() >= self.span.end.min(self.track()) - COVERAGE_SLACK
}
fn track(&self) -> f64 {
self.info.sound_length().as_secs_f64()
}
}
impl Sound {
fn is_heard(&self) -> bool {
matches!(self, Self::Heard { .. })
}
fn stopped(&self) -> bool {
matches!(
self,
Self::Heard {
stopped: Some(_),
..
}
)
}
}
fn plural(count: usize, one: &'static str, many: &'static str) -> &'static str {
if count == 1 { one } else { many }
}
fn clock(seconds: f64) -> String {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a clip's length in whole seconds fits u64"
)]
spelled(seconds.max(0.0) as u64)
}
fn mark(seconds: f64) -> String {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a clip's length in tenths of a second fits u64"
)]
let tenths = (seconds.max(0.0) * 10.0).round() as u64;
let whole = spelled(tenths / 10);
match tenths % 10 {
0 => whole,
part => format!("{whole}.{part}"),
}
}
fn spelled(total: u64) -> String {
let (hours, minutes, seconds) = (total / 3600, total % 3600 / 60, total % 60);
if hours > 0 {
format!("{hours}:{minutes:02}:{seconds:02}")
} else {
format!("{minutes}:{seconds:02}")
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
fn info(seconds: u64, dimensions: Option<(u32, u32)>, has_audio: bool) -> ClipInfo {
ClipInfo {
duration: Duration::from_secs(seconds),
dimensions,
has_audio,
picture: dimensions.map(|_| Duration::from_secs(seconds)),
sound: has_audio.then_some(Duration::from_secs(seconds)),
bytes: 1024,
}
}
#[test]
fn an_overview_spreads_its_frames_across_the_whole_clip() {
let plan = FramePlan::of(Window::Overview, 3.0);
assert_eq!(plan.marks.len(), OVERVIEW_FRAMES_MIN as usize);
assert_eq!(plan.side, OVERVIEW_FRAME_SIDE);
let plan = FramePlan::of(Window::Overview, 2530.0);
assert_eq!(plan.marks.len(), FRAMES_MAX as usize);
assert!(plan.marks[0] < 1.0);
assert!(plan.marks[23] > 2400.0);
}
#[test]
fn a_window_samples_its_own_stretch() {
let window = Window::Span {
start: 20.0,
length: Some(5.0),
};
let plan = FramePlan::of(window, 60.0);
assert_eq!(plan.marks.len(), 10);
assert_eq!(plan.side, WINDOW_FRAME_SIDE);
assert!((plan.marks[0] - 20.0).abs() < 1e-9);
assert!((plan.marks[9] - 24.5).abs() < 1e-9);
let past_the_pictures = FramePlan::of(
Window::Span {
start: 7.0,
length: Some(3.0),
},
5.0,
);
assert!(past_the_pictures.marks.is_empty());
}
#[test]
fn a_window_longer_than_the_ceiling_still_spreads() {
let window = Window::Span {
start: 0.0,
length: Some(600.0),
};
let plan = FramePlan::of(window, 900.0);
assert_eq!(plan.marks.len(), FRAMES_MAX as usize);
assert!((plan.marks[1] - 25.0).abs() < 1e-9);
}
#[test]
fn an_overview_transcript_stops_at_ten_minutes_while_its_frames_cover_the_clip() {
let span = Window::Overview.span(2530.0);
assert!((span.end - OVERVIEW_SECONDS).abs() < f64::EPSILON);
assert!((Window::Overview.frame_span(2530.0).end - 2530.0).abs() < f64::EPSILON);
}
#[test]
fn every_failure_says_something_different() {
let messages = [
MediaError::Unavailable("not installed".to_string()),
MediaError::Undecodable,
MediaError::Malformed("no frames were asked for".to_string()),
MediaError::NoTracks,
MediaError::SoundTrack("timed out".to_string()),
MediaError::Busy,
MediaError::Stopping,
MediaError::TimedOut,
]
.map(|error| error.message());
let distinct: std::collections::HashSet<&String> = messages.iter().collect();
assert_eq!(distinct.len(), messages.len(), "{messages:?}");
}
#[test]
fn a_decode_failure_is_not_a_failed_recognition() {
let clip = info(60, Some((640, 360)), true);
let undecodable = rendered(
&clip,
Window::Overview,
&[],
&Sound::Failed(MediaError::Undecodable),
true,
);
assert!(
undecodable.ends_with(
"The sound track could not be read: the file could not be decoded as media."
),
"{undecodable}"
);
let unrecognised = rendered(
&clip,
Window::Overview,
&[],
&Sound::Failed(MediaError::SoundTrack("timed out".to_string())),
true,
);
assert!(
unrecognised.ends_with("The sound track could not be recognised: timed out."),
"{unrecognised}"
);
}
#[test]
fn a_mark_reads_as_a_clock() {
assert_eq!(clock(0.0), "0:00");
assert_eq!(clock(67.9), "1:07");
assert_eq!(clock(3723.0), "1:02:03");
assert_eq!(mark(30.0), "0:30");
assert_eq!(mark(5.5), "0:05.5");
assert_eq!(mark(59.96), "1:00");
}
fn frame_strip(marks: &[f64]) -> Vec<(PathBuf, f64)> {
marks
.iter()
.map(|at| (PathBuf::from(format!("frame-{at}.jpg")), *at))
.collect()
}
fn heard(start: f64, end: f64, text: Option<&str>, label: Option<&str>) -> Segment {
Segment {
start,
end,
text: text.map(str::to_string),
label: label.map(str::to_string),
}
}
fn rendered(
clip: &ClipInfo,
window: Window,
strip: &[(PathBuf, f64)],
sound: &Sound,
words: bool,
) -> String {
let span = window.span(clip.duration.as_secs_f64());
render(&Timeline {
path: Path::new("/clip.mp4"),
kind: Kind::Video,
info: clip,
window,
span,
frames: strip,
frame_failure: None,
sound,
words,
})
}
#[test]
fn the_timeline_is_one_strip_in_time_order() {
let clip = info(60, Some((640, 360)), true);
let strip = frame_strip(&[0.0, 30.0]);
let sound = Sound::Heard {
regions: vec![
heard(0.0, 5.0, Some("Hello there."), None),
heard(5.0, 12.0, None, Some("Music")),
heard(12.0, 20.0, None, Some("Speech")),
heard(20.0, 30.0, None, Some("Silence")),
heard(30.0, 60.0, None, Some("Silence")),
],
stopped: None,
reached: 60.0,
};
assert_eq!(
rendered(&clip, Window::Overview, &strip, &sound, true),
"Video /clip.mp4 — 1:00, 640x360, 2 frames\n\
0:00 [frame 1]\n\
0:00–0:05 Hello there.\n\
0:05–0:12 (Music)\n\
0:12–0:20 [untranscribed speech]\n\
0:30 [frame 2]"
);
}
#[test]
fn with_transcription_off_speech_keeps_its_place_and_the_labels_fill_in() {
let clip = info(60, Some((640, 360)), true);
let sound = Sound::Heard {
regions: vec![
heard(0.0, 5.0, Some("Hello there."), None),
heard(5.0, 12.0, None, Some("Music")),
],
stopped: None,
reached: 60.0,
};
let text = rendered(&clip, Window::Overview, &frame_strip(&[0.0]), &sound, false);
assert!(text.contains("\n0:00 [frame 1]\n"), "{text}");
assert!(text.contains("\n0:00–0:05 (Speech)\n"), "{text}");
assert!(text.contains("\n0:05–0:12 (Music)"), "{text}");
assert!(
!text.contains("Nothing was heard in this stretch."),
"{text}"
);
assert!(!text.contains("Hello there."), "{text}");
}
#[test]
fn a_capped_overview_says_how_much_it_covered_and_a_silent_clip_says_so() {
let clip = info(2530, Some((640, 360)), true);
let strip = frame_strip(&[0.0]);
let sound = Sound::Heard {
regions: vec![
heard(0.0, 10.0, Some("Hi."), None),
heard(10.0, 2530.0, None, Some("Music")),
],
stopped: None,
reached: 2530.0,
};
let text = rendered(&clip, Window::Overview, &strip, &sound, true);
assert!(
text.ends_with(
"The transcript covers 0:00–10:00 of 42:10 — read further with offset/limit."
),
"{text}"
);
let text = rendered(&clip, Window::Overview, &strip, &Sound::Absent, true);
assert!(text.contains("\nThis video has no sound track."), "{text}");
}
#[test]
fn a_pass_that_stopped_early_says_where_it_stopped() {
let clip = info(60, Some((640, 360)), true);
let strip = frame_strip(&[0.0]);
let sound = Sound::Heard {
regions: vec![heard(0.0, 10.0, Some("Hi."), None)],
stopped: None,
reached: 10.0,
};
let text = rendered(&clip, Window::Overview, &strip, &sound, true);
assert!(
text.ends_with(
"The transcript covers 0:00–0:10 of 1:00 — read further with offset/limit."
),
"{text}"
);
}
#[test]
fn a_stretch_the_pass_never_reached_is_not_called_silent() {
let clip = info(60, Some((640, 360)), true);
let stopped_short = Sound::Heard {
regions: Vec::new(),
stopped: None,
reached: 10.0,
};
let text = rendered(
&clip,
Window::Overview,
&frame_strip(&[0.0]),
&stopped_short,
true,
);
assert!(
!text.contains("Nothing was heard in this stretch."),
"{text}"
);
let silent = Sound::Heard {
regions: Vec::new(),
stopped: None,
reached: 60.0,
};
let text = rendered(&clip, Window::Overview, &frame_strip(&[0.0]), &silent, true);
assert!(
text.contains("Nothing was heard in this stretch."),
"{text}"
);
}
#[test]
fn a_track_that_could_not_be_read_to_the_end_keeps_its_lines_and_says_where() {
let clip = info(2530, Some((640, 360)), true);
let sound = Sound::Heard {
regions: vec![heard(0.0, 300.0, Some("Hi."), None)],
stopped: Some(MediaError::SoundTrack(
"the piece could not be decoded".to_string(),
)),
reached: 300.0,
};
let text = rendered(&clip, Window::Overview, &frame_strip(&[0.0]), &sound, true);
assert!(text.contains("\n0:00–5:00 Hi."), "{text}");
assert!(
text.contains(
"The sound track could not be read past 5:00 of 42:10: the piece could not be \
decoded."
),
"{text}"
);
assert!(!text.contains("The transcript covers"), "{text}");
assert!(
!text.contains("Nothing was heard in this stretch."),
"{text}"
);
}
#[test]
fn a_track_that_stops_before_its_container_needs_no_note() {
let mut clip = info(60, Some((640, 360)), true);
clip.sound = Some(Duration::from_secs(20));
let sound = Sound::Heard {
regions: vec![heard(0.0, 20.0, None, Some("Music"))],
stopped: None,
reached: 20.0,
};
let text = rendered(&clip, Window::Overview, &frame_strip(&[0.0]), &sound, true);
assert!(!text.contains("The transcript covers"), "{text}");
}
fn frame_jpeg(colour: [u8; 3]) -> Vec<u8> {
let mut jpeg = Vec::new();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, ffmpeg::FRAME_QUALITY)
.encode_image(&image::RgbImage::from_pixel(4, 2, image::Rgb(colour)))
.unwrap();
jpeg
}
fn cells(frames: &[Vec<u8>]) -> Vec<(usize, usize, Vec<u8>)> {
frames
.iter()
.enumerate()
.map(|(index, jpeg)| (index, index + 1, jpeg.clone()))
.collect()
}
#[test]
fn four_frames_go_into_one_picture_in_reading_order() {
let colours = [[255, 0, 0], [0, 255, 0], [0, 0, 255]];
let picture = compose(&cells(&colours.map(frame_jpeg))).unwrap();
assert_eq!(picture.size, (8, 4));
assert_eq!(picture.frames_text(), "frames 1–3");
let image = image::load_from_memory(&uri_bytes(&picture.data_uri))
.unwrap()
.to_rgb8();
for (index, colour) in colours.iter().enumerate() {
let (column, row) = (
u32::try_from(index % 2).unwrap(),
u32::try_from(index / 2).unwrap(),
);
assert!(
near(image.get_pixel(column * 4 + 1, row * 2 + 1).0, *colour),
"cell {index}: {:?}",
image.get_pixel(column * 4 + 1, row * 2 + 1).0
);
}
assert!(
near(image.get_pixel(5, 3).0, [0, 0, 0]),
"{:?}",
image.get_pixel(5, 3).0
);
}
#[test]
fn a_frame_that_cannot_be_decoded_leaves_its_cell_blank() {
let cells = [
(0, 1, frame_jpeg([255, 0, 0])),
(1, 2, b"not a JPEG".to_vec()),
(2, 3, frame_jpeg([0, 255, 0])),
];
let picture = compose(&cells).unwrap();
assert_eq!(picture.frames_text(), "frames 1, 3");
let image = image::load_from_memory(&uri_bytes(&picture.data_uri))
.unwrap()
.to_rgb8();
assert!(near(image.get_pixel(1, 1).0, [255, 0, 0]));
assert!(near(image.get_pixel(5, 1).0, [0, 0, 0]));
assert!(near(image.get_pixel(1, 3).0, [0, 255, 0]));
}
fn near(actual: [u8; 3], expected: [u8; 3]) -> bool {
actual
.iter()
.zip(expected)
.all(|(actual, expected)| actual.abs_diff(expected) < 40)
}
fn uri_bytes(uri: &str) -> Vec<u8> {
use base64::Engine as _;
base64::engine::general_purpose::STANDARD
.decode(uri.split_once(',').expect("a data URI").1)
.unwrap()
}
#[test]
fn the_attached_line_names_the_frames_each_picture_holds() {
let picture = |frames: &[usize]| Picture {
data_uri: String::new(),
size: (0, 0),
frames: frames.to_vec(),
};
assert_eq!(
pictures_attached(&[
picture(&[1, 2, 3, 4]),
picture(&[5, 6, 7, 8]),
picture(&[9, 10])
]),
"The frames are attached as 3 pictures of four, left to right then the next row — \
picture 1: frames 1–4; picture 2: frames 5–8; picture 3: frames 9–10."
);
assert_eq!(
pictures_attached(&[picture(&[7])]),
"The frames are attached as 1 picture of four, left to right then the next row — \
picture 1: frame 7."
);
}
}