aboutsummaryrefslogtreecommitdiff
path: root/transcoder/src/snippet.rs
blob: 56bd5239887529a09dc3fbf0faa655185777bc3a (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/

use jellybase::{cache::async_cache_file, AssetLocationExt};
use jellycommon::AssetLocation;
use std::process::Stdio;
use tokio::{fs::File, process::Command};

#[derive(Debug)]
pub enum Encoding {
    Video {
        codec: &'static str,
        preset: u8,
        bitrate: usize,
        width: usize,
    },
}

pub async fn transcode(asset: AssetLocation, enc: Encoding) -> anyhow::Result<AssetLocation> {
    let original_path = asset.path();
    let asset = asset.clone();
    Ok(async_cache_file(
        &[
            "snip-tc",
            original_path.as_os_str().to_str().unwrap(),
            &format!("{enc:?}"),
        ],
        move |output| async move {
            let args = match enc {
                Encoding::Video {
                    codec,
                    preset,
                    bitrate,
                    width,
                } => [
                    "-vf".to_string(),
                    format!("scale={width}:-1"),
                    "-c:v".to_string(),
                    codec.to_string(),
                    "-preset".to_string(),
                    format!("{preset}"),
                    "-b:v".to_string(),
                    format!("{bitrate}"),
                ]
                .to_vec(),
            };
            let mut proc = Command::new("ffmpeg")
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .args(&["-f", "mkv", "-i", "pipe:0"])
                .args(args)
                .args(&["-f", "mkv", "pipe:1"])
                .spawn()?;

            let mut stdin = proc.stdin.take().unwrap();
            let mut stdout = proc.stdout.take().unwrap();

            Ok(())
        },
    )
    .await?)
}