using std::thread for ffmpeg calls since it takes long on big videos

This commit is contained in:
Chris Cochrun 2024-04-10 22:24:57 -05:00
parent effede9da1
commit 5df0302be9
2 changed files with 26 additions and 22 deletions

View file

@ -7,7 +7,10 @@ use std::process::Command;
use std::str; use std::str;
use tracing::debug; use tracing::debug;
pub async fn bg_from_video(video: &Path, screenshot: &Path) -> Result<(), Box<dyn Error>> { pub fn bg_from_video(
video: &Path,
screenshot: &Path,
) -> Result<(), Box<dyn Error>> {
if !screenshot.exists() { if !screenshot.exists() {
let output_duration = Command::new("ffprobe") let output_duration = Command::new("ffprobe")
.args(&["-i", &video.to_string_lossy()]) .args(&["-i", &video.to_string_lossy()])
@ -82,32 +85,41 @@ pub fn bg_path_from_video(video: &Path) -> PathBuf {
screenshot screenshot
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
#[test] #[test]
fn test_bg_video_creation() { fn test_bg_video_creation() {
let video = Path::new("/home/chris/vids/All WebDev Sucks and you know it.webm"); let video = Path::new(
"/home/chris/vids/All WebDev Sucks and you know it.webm",
);
let screenshot = bg_path_from_video(video); let screenshot = bg_path_from_video(video);
let screenshot_string = screenshot.to_str().expect("Should be thing"); let screenshot_string =
screenshot.to_str().expect("Should be thing");
assert_eq!(screenshot_string, "/home/chris/.local/share/lumina/thumbnails/All WebDev Sucks and you know it.png"); assert_eq!(screenshot_string, "/home/chris/.local/share/lumina/thumbnails/All WebDev Sucks and you know it.png");
let runtime = tokio::runtime::Runtime::new().unwrap(); // let runtime = tokio::runtime::Runtime::new().unwrap();
let future = bg_from_video(video, &screenshot); let result = bg_from_video(video, &screenshot);
let result = runtime.block_on(future); // let result = runtime.block_on(future);
match result { match result {
Ok(o) => assert_eq!(screenshot.exists(), true), Ok(o) => assert_eq!(screenshot.exists(), true),
Err(e) => debug_assert!(false, "There was an error in the runtime future. {:?}", e) Err(e) => debug_assert!(
false,
"There was an error in the runtime future. {:?}",
e
),
} }
} }
#[test] #[test]
fn test_bg_not_same() { fn test_bg_not_same() {
let video = Path::new("/home/chris/vids/All WebDev Sucks and you know it.webm"); let video = Path::new(
"/home/chris/vids/All WebDev Sucks and you know it.webm",
);
let screenshot = bg_path_from_video(video); let screenshot = bg_path_from_video(video);
let screenshot_string = screenshot.to_str().expect("Should be thing"); let screenshot_string =
screenshot.to_str().expect("Should be thing");
assert_ne!(screenshot_string, "/home/chris/.local/share/lumina/thumbnails/All WebDev Sucks and you know it.webm"); assert_ne!(screenshot_string, "/home/chris/.local/share/lumina/thumbnails/All WebDev Sucks and you know it.webm");
} }
} }

View file

@ -331,9 +331,9 @@ impl slide_model::SlideModel {
screenshot.to_str().unwrap() screenshot.to_str().unwrap()
).insert(0, &QString::from("file://")).to_owned(); ).insert(0, &QString::from("file://")).to_owned();
slide.video_thumbnail = screenshot_string; slide.video_thumbnail = screenshot_string;
let runtime = tokio::runtime::Runtime::new().unwrap(); // let runtime = tokio::runtime::Runtime::new().unwrap();
runtime.spawn(async move { std::thread::spawn(move || {
let result = ffmpeg::bg_from_video(&path, &screenshot).await; let result = ffmpeg::bg_from_video(&path, &screenshot);
match result { match result {
Ok(_o) => debug!("Success making video background!"), Ok(_o) => debug!("Success making video background!"),
Err(error) => error!(?error, "Error making video background") Err(error) => error!(?error, "Error making video background")
@ -426,15 +426,7 @@ impl slide_model::SlideModel {
self.as_mut().rust_mut().slides.push(slide); self.as_mut().rust_mut().slides.push(slide);
self.as_mut().end_insert_rows(); self.as_mut().end_insert_rows();
} }
let thread = self.qt_thread(); self.as_mut().add_video_thumbnail(index);
thread::spawn(move || {
thread
.queue(move |slidemodel| {
slidemodel.add_video_thumbnail(index);
})
.unwrap();
});
// self.as_mut().add_video_thumbnail(index);
} }
fn insert_slide( fn insert_slide(