aboutsummaryrefslogtreecommitdiff
path: root/transcoder/src/thumbnail.rs
blob: 9207c3055fc26013891341f99e9b4fe7ff328e5c (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
35
36
37
38
use anyhow::{Context, Result};
use jellycache::{Cache, HashKey};
use log::info;
use std::{
    io::Read,
    path::Path,
    process::{Command, Stdio},
};

pub fn create_thumbnail(cache: &Cache, path: &Path, time: f64) -> Result<String> {
    cache
        .store(
            format!("media/thumbnail/{}-{}.image", HashKey(path), time as i64),
            move || {
                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();
                let mut output = Vec::new();
                stdout.read_to_end(&mut output)?;

                proc.wait().unwrap().exit_ok()?;
                info!("done");
                Ok(output)
            },
        )
        .context("creating thumbnail")
}