app: cut miniatures for the clips grid

Each card's stage shows a frame of the clip now, cut once with ffmpeg
into a thumbs folder beside the clips and reused from there. The pass
runs one clip at a time on a thread, skips what is already on disk and
as new as its clip, and poll drops each finished picture into its card
as it lands. Deleting a clip takes its miniature with it.

The frame is the clip's first: an export starts on the moment the clip
was cut for, so the picture shows the action without a seek. A clip
re-exported over the same name keeps its old picture until restart,
because the image cache keys a file by its path alone.
This commit is contained in:
vhaudiquet committed 2026-11-22 18:52:57 +01:00
1 parent 3f82f87a81
commit 83ca4071d4
5 files changed
+196 -25

No files matched your search

+101 -2
View File
@@ -28,6 +28,102 @@ pub struct Clip {
pub exported: Option<SystemTime>,
/// Bytes on disk.
pub bytes: u64,
/// The miniature cut from the video, when one is on disk and as new as the clip.
pub thumb: Option<PathBuf>,
}
/// Where the clips' miniatures live, beside the clips.
pub fn thumbs_dir() -> PathBuf {
let dir = clips_dir().join("thumbs");
let _ = std::fs::create_dir_all(&dir);
dir
}
/// Where `clip`'s miniature lives. A clip that names no file is demo data: its miniature
/// stays in the temp folder, so the clips folder keeps only what the user exported.
pub fn thumb_path(clip: &Clip) -> PathBuf {
if clip.path.as_os_str().is_empty() {
std::env::temp_dir()
.join("leaguerecorder-demo")
.join("thumbs")
.join(format!("{}.jpg", clip.name))
} else {
thumbs_dir().join(format!("{}.jpg", clip.name))
}
}
/// The miniature when it is on disk and as new as the clip: a stale one reads as none, so
/// the thumbnail pass cuts it again. The texture under an old path is cached by path alone,
/// so a clip re-exported over the same name keeps its old picture until the app restarts.
pub fn thumb_of(clip: &Clip) -> Option<PathBuf> {
let thumb = thumb_path(clip);
let at = std::fs::metadata(&thumb).ok()?.modified().ok()?;
let clip_at = clip.exported?;
(at >= clip_at).then_some(thumb)
}
/// The video a miniature is cut from. A clip that names no file is demo data: its video is
/// synthesized once into the temp folder, so a snapshot has something to show.
pub fn source_of(clip: &Clip) -> Option<PathBuf> {
if !clip.path.as_os_str().is_empty() {
return clip.path.is_file().then(|| clip.path.clone());
}
let demo = std::env::temp_dir()
.join("leaguerecorder-demo")
.join(format!("{}.mp4", clip.name));
if demo.is_file() {
return Some(demo);
}
let _ = std::fs::create_dir_all(demo.parent()?);
let Ok(child) = FfmpegCommand::new()
.arg("-y")
.arg("-f")
.arg("lavfi")
.arg("-i")
.arg("testsrc2=size=640x360:rate=30:duration=2")
.arg("-c:v")
.arg("libx264")
.arg("-preset")
.arg("ultrafast")
.arg("-pix_fmt")
.arg("yuv420p")
.arg(demo.to_string_lossy().to_string())
.spawn()
else {
return None;
};
let mut child = child;
child.wait().ok()?.success().then_some(demo)
}
/// Cut the first frame of `video` into `thumb`, at card size. True when the file is there
/// after: the frame lands beside a `.part.jpg` file that only a good cut renames on.
pub fn make_thumb(video: &Path, thumb: &Path) -> bool {
if let Some(parent) = thumb.parent() {
let _ = std::fs::create_dir_all(parent);
}
if ffmpeg_is_installed() {
let part = thumb.with_extension("part.jpg");
let cut = FfmpegCommand::new()
.arg("-y")
.arg("-i")
.arg(video.to_string_lossy().to_string())
.arg("-frames:v")
.arg("1")
.arg("-vf")
.arg("scale=640:-2")
.arg("-q:v")
.arg("4")
.arg(part.to_string_lossy().to_string())
.spawn()
.and_then(|mut child| child.wait());
let ok = cut.map(|status| status.success()).unwrap_or(false) && part.is_file();
if ok && std::fs::rename(&part, thumb).is_ok() {
return true;
}
let _ = std::fs::remove_file(&part);
}
false
}
/// Every clip in the clips folder, newest export first. Only `.mp4` counts: the folder is
@@ -44,12 +140,15 @@ pub fn list_clips() -> Vec<Clip> {
return None;
}
let meta = entry.metadata().ok()?;
Some(Clip {
let mut clip = Clip {
name: path.file_stem()?.to_string_lossy().into_owned(),
path: path.clone(),
exported: meta.modified().ok(),
bytes: meta.len(),
})
thumb: None,
};
clip.thumb = thumb_of(&clip);
Some(clip)
})
.collect();
clips.sort_by_key(|clip| std::cmp::Reverse(clip.exported));