aboutsummaryrefslogtreecommitdiff
path: root/remuxer/src/mpeg4.rs
blob: da66fe2d8b6af23b6cebd9ee09dc7d5ef599c7ee (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
/*
    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 anyhow::Result;
use std::{
    fs::{remove_file, File},
    io::{copy, Read, Write},
    process::{Command, Stdio},
    random::random,
};

pub fn matroska_to_mpeg4(
    mut input: impl Read + Send + 'static,
    mut output: impl Write,
) -> Result<()> {
    let path = format!("/tmp/jellything-tc-hack-{:016x}", random::<u64>());
    let args = format!("-f matroska -i pipe:0 -copyts -c copy -f mp4 {path}");
    let mut child = Command::new("ffmpeg")
        .args(args.split(" "))
        .stdin(Stdio::piped())
        .stderr(Stdio::inherit())
        .spawn()?;

    let mut stdin = child.stdin.take().unwrap();
    copy(&mut input, &mut stdin)?;
    drop(stdin);
    child.wait()?.exit_ok()?;
    copy(&mut File::open(&path)?, &mut output)?;
    remove_file(path)?;

    Ok(())
}