async setup for using ffmpeg in the slide model

This commit is contained in:
Chris Cochrun 2024-04-10 11:42:01 -05:00
parent 76b4c87d3a
commit 64095d6fed
2 changed files with 18 additions and 24 deletions

View file

@ -1,4 +1,5 @@
use dirs; use dirs;
use std::error::Error;
use std::fs; use std::fs;
use std::io::{self, Write}; use std::io::{self, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -6,19 +7,7 @@ use std::process::Command;
use std::str; use std::str;
use tracing::debug; use tracing::debug;
pub async fn bg_from_video(video: &Path) -> PathBuf { pub async fn bg_from_video(video: &Path, screenshot: &Path) -> Result<(), Box<dyn Error>> {
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");
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()])

View file

@ -319,27 +319,32 @@ impl slide_model::SlideModel {
.append(self.get_role(SlideRoles::VideoThumbnail)); .append(self.get_role(SlideRoles::VideoThumbnail));
let thread = self.qt_thread(); 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) = if let Some(slide) =
self.as_mut().rust_mut().slides.get_mut(index as usize) self.as_mut().rust_mut().slides.get_mut(index as usize)
{ {
if !slide.video_background.is_empty() { if !slide.video_background.is_empty() {
let runtime = tokio::runtime::Runtime::new().unwrap();
let path = let path =
PathBuf::from(slide.video_background.to_string()); PathBuf::from(slide.video_background.to_string());
runtime.spawn(async move { let screenshot = ffmpeg::bg_path_from_video(&path);
let video = ffmpeg::bg_from_video(&path).await; let screenshot_string = QString::from(
let video = QString::from( screenshot.to_str().unwrap()
video.to_str().unwrap()
).insert(0, &QString::from("file://")).to_owned(); ).insert(0, &QString::from("file://")).to_owned();
slide.video_thumbnail = video; slide.video_thumbnail = screenshot_string;
thread.queue(move |mut slide| let runtime = tokio::runtime::Runtime::new().unwrap();
slide.as_mut().data_changed( runtime.spawn(async move {
model_index, let video = ffmpeg::bg_from_video(&path, &screenshot).await;
model_index, let res = thread.queue(move |mut slide_model|
slide_model.as_mut().data_changed(
&model_index,
&model_index,
&vector_roles, &vector_roles,
) )
); );
match res {
Ok(o) => debug!("success making video background!"),
Err(error) => error!(?error, "Error making video background!")
}
}); });
} }
} }