aboutsummaryrefslogtreecommitdiff
path: root/transcoder/src/thumbnail.rs
blob: c8bfb1c274e1313363214d36d7fb0e6b6b137853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use crate::LOCAL_IMAGE_TRANSCODING_TASKS;
use jellybase::cache::{async_cache_file, CachePath};
use log::info;
use std::{path::Path, process::Stdio};
use tokio::{io::copy, process::Command};

pub async fn create_thumbnail(path: &Path, time: f64) -> anyhow::Result<CachePath> {
    async_cache_file(
        &["thumb", path.to_str().unwrap(), &format!("{time}")],
        move |mut output| async move {
            let _permit = LOCAL_IMAGE_TRANSCODING_TASKS.acquire().await?;
            info!("creating thumbnail of {path:?} at {time}s",);

            let mut proc = Command::new("ffmpeg")
                .stdout(Stdio::piped())
                .args(["-ss", &format!("{time}")])
                .args(["-f", "matroska", "-i", path.to_str().unwrap()])
                .args(["-frames:v", "1"])
                .args(["-c:v", "qoi"])
                .args(["-f", "image2"])
                .args(["-update", "1"])
                .arg("pipe:1")
                .spawn()?;

            let mut stdout = proc.stdout.take().unwrap();
            copy(&mut stdout, &mut output).await?;

            proc.wait().await.unwrap().exit_ok()?;
            info!("done");
            Ok(())
        },
    )
    .await
}