From 64095d6fed097a186df11cbf6185d6027e4d3013 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Wed, 10 Apr 2024 11:42:01 -0500 Subject: [PATCH] async setup for using ffmpeg in the slide model --- src/rust/ffmpeg.rs | 15 ++------------- src/rust/slide_model.rs | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/rust/ffmpeg.rs b/src/rust/ffmpeg.rs index 846df8c..da9a82f 100644 --- a/src/rust/ffmpeg.rs +++ b/src/rust/ffmpeg.rs @@ -1,4 +1,5 @@ use dirs; +use std::error::Error; use std::fs; use std::io::{self, Write}; use std::path::{Path, PathBuf}; @@ -6,19 +7,7 @@ use std::process::Command; use std::str; use tracing::debug; -pub async fn bg_from_video(video: &Path) -> PathBuf { - let video = PathBuf::from(video); - debug!(?video); - let mut data_dir = dirs::data_local_dir().unwrap(); - data_dir.push("lumina"); - data_dir.push("thumbnails"); - if !data_dir.exists() { - fs::create_dir(&data_dir) - .expect("Could not create thumbnails dir"); - } - let mut screenshot = data_dir.clone(); - screenshot.push(video.file_name().unwrap()); - screenshot.set_extension("png"); +pub async fn bg_from_video(video: &Path, screenshot: &Path) -> Result<(), Box> { if !screenshot.exists() { let output_duration = Command::new("ffprobe") .args(&["-i", &video.to_string_lossy()]) diff --git a/src/rust/slide_model.rs b/src/rust/slide_model.rs index b0d8111..c9502b5 100644 --- a/src/rust/slide_model.rs +++ b/src/rust/slide_model.rs @@ -319,27 +319,32 @@ impl slide_model::SlideModel { .append(self.get_role(SlideRoles::VideoThumbnail)); let thread = self.qt_thread(); - let model_index = &self.as_ref().index(index, 0, &QModelIndex::default()); + let model_index = self.as_ref().index(index, 0, &QModelIndex::default()).clone(); if let Some(slide) = self.as_mut().rust_mut().slides.get_mut(index as usize) { if !slide.video_background.is_empty() { - let runtime = tokio::runtime::Runtime::new().unwrap(); let path = PathBuf::from(slide.video_background.to_string()); + let screenshot = ffmpeg::bg_path_from_video(&path); + let screenshot_string = QString::from( + screenshot.to_str().unwrap() + ).insert(0, &QString::from("file://")).to_owned(); + slide.video_thumbnail = screenshot_string; + let runtime = tokio::runtime::Runtime::new().unwrap(); runtime.spawn(async move { - let video = ffmpeg::bg_from_video(&path).await; - let video = QString::from( - video.to_str().unwrap() - ).insert(0, &QString::from("file://")).to_owned(); - slide.video_thumbnail = video; - thread.queue(move |mut slide| - slide.as_mut().data_changed( - model_index, - model_index, + let video = ffmpeg::bg_from_video(&path, &screenshot).await; + let res = thread.queue(move |mut slide_model| + slide_model.as_mut().data_changed( + &model_index, + &model_index, &vector_roles, ) ); + match res { + Ok(o) => debug!("success making video background!"), + Err(error) => error!(?error, "Error making video background!") + } }); } }