add thumbnail feature
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
JMARyA 2024-12-16 23:45:15 +01:00
parent 0d0265ab14
commit 764bef6457
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
5 changed files with 113 additions and 11 deletions

View file

@ -2,10 +2,13 @@ use serde_json::json;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use tokio::io::AsyncReadExt;
use walkdir::WalkDir;
use func::is_video_file;
pub use video::Video;
use crate::meta;
mod func;
pub mod user;
mod video;
@ -52,6 +55,64 @@ impl Library {
.unwrap()
}
pub async fn get_thumbnail(&self, video: &Video) -> Option<Vec<u8>> {
// get custom thumbnail
let res: Option<(Option<Vec<u8>>,)> =
sqlx::query_as("SELECT custom FROM video_thumbnail WHERE id = $1")
.bind(video.id)
.fetch_optional(&self.conn)
.await
.unwrap();
if let Some(res) = res {
if let Some(data) = res.0 {
log::info!("Returned Custom Thumbnail");
return Some(data);
}
} else {
let half_time = video.duration / 2.5;
let precomputed = meta::extract_video_thumbnail(&video.path, half_time);
sqlx::query(
"INSERT INTO video_thumbnail (\"id\", time, precomputed) VALUES ($1, $2, $3)",
)
.bind(video.id)
.bind(half_time)
.bind(precomputed)
.execute(&self.conn)
.await
.unwrap();
log::info!("Computed Thumbnail");
}
// get sidecar thumbnail
let path = std::path::Path::new(&video.path);
let parent = path.parent().unwrap();
let thumbnail_path = path.file_stem().unwrap().to_str().unwrap();
let thumbnail_path = parent.join(thumbnail_path);
let thumbnail_path = thumbnail_path.to_str().unwrap();
for ext in ["jpg", "jpeg", "png", "avif"] {
if let Ok(mut file) = tokio::fs::File::open(format!("{thumbnail_path}.{ext}")).await {
let mut content = Vec::with_capacity(1024);
file.read_to_end(&mut content).await.unwrap();
log::info!("Returned Sidecar Thumbnail");
return Some(content);
}
}
// get precomputed thumbnail
let res: (Vec<u8>,) =
sqlx::query_as("SELECT precomputed FROM video_thumbnail WHERE id = $1")
.bind(video.id)
.fetch_one(&self.conn)
.await
.unwrap();
log::info!("Returned Precomputed Thumbnail");
return Some(res.0);
}
// YT
pub async fn get_channel_name_yt(&self, id: &str) -> String {