aboutsummaryrefslogtreecommitdiff
path: root/transcoder/src/fragment.rs
blob: b88339cc91a113b0e88ee4858f52fc9a86fe0438 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
    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) 2025 metamuffin <metamuffin.org>
*/

use crate::LOCAL_VIDEO_TRANSCODING_TASKS;
use jellybase::{
    cache::{async_cache_file, CachePath},
    common::stream::StreamFormatInfo,
};
use log::{debug, info};
use std::process::Stdio;
use tokio::{
    io::copy,
    process::{ChildStdin, Command},
};

// TODO odd video resolutions can cause errors when transcoding to YUV42{0,2}
// TODO with an implementation that cant handle it (SVT-AV1 is such an impl).

pub async fn transcode(
    key: &str,
    enc: &StreamFormatInfo,
    input: impl FnOnce(ChildStdin),
) -> anyhow::Result<CachePath> {
    async_cache_file(
        &["frag-tc", key, &format!("{enc:?}")],
        move |mut output| async move {
            let _permit = LOCAL_VIDEO_TRANSCODING_TASKS.acquire().await?;
            debug!("transcoding fragment with {enc:?}");

            let mut args = Vec::<String>::new();
            // match enc {
            //     EncodingProfile::Video {
            //         codec,
            //         preset,
            //         bitrate,
            //         width,
            //     } => {
            //         if let Some(width) = width {
            //             args.push("-vf".to_string());
            //             args.push(format!("scale={width}:-1"));
            //         }
            //         args.push("-c:v".to_string());
            //         args.push(codec.to_string());
            //         if let Some(preset) = preset {
            //             args.push("-preset".to_string());
            //             args.push(format!("{preset}"));
            //         }
            //         args.push("-b:v".to_string());
            //         args.push(format!("{bitrate}"));
            //     }
            //     EncodingProfile::Audio {
            //         codec,
            //         bitrate,
            //         sample_rate,
            //         channels,
            //     } => {
            //         if let Some(channels) = channels {
            //             args.push("-ac".to_string());
            //             args.push(format!("{channels}"))
            //         }
            //         if let Some(sample_rate) = sample_rate {
            //             args.push("-ar".to_string());
            //             args.push(format!("{sample_rate}"))
            //         }
            //         args.push("-c:a".to_string());
            //         args.push(codec.to_string());
            //         args.push("-b:a".to_string());
            //         args.push(format!("{bitrate}"));
            //     }
            //     EncodingProfile::Subtitles { codec } => {
            //         args.push("-c:s".to_string());
            //         args.push(codec.to_string());
            //     }
            // };
            info!("encoding with {:?}", args.join(" "));

            let mut proc = Command::new("ffmpeg")
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .args(["-f", "matroska", "-i", "pipe:0"])
                .args(args)
                .args(["-f", "webm", "pipe:1"])
                .spawn()?;
            // let mut proc = Command::new("cat")
            //     .stdin(Stdio::piped())
            //     .stdout(Stdio::piped())
            //     .spawn()?;

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

            input(stdin);
            copy(&mut stdout, &mut output).await?;

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