/* 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 */ use crate::LOCAL_VIDEO_TRANSCODING_TASKS; use jellybase::{ cache::{async_cache_file, CachePath}, common::stream::{StreamContainer, 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, format: &StreamFormatInfo, container: StreamContainer, input: impl FnOnce(ChildStdin), ) -> anyhow::Result { async_cache_file( &["frag-tc", key, &format!("{format:?}")], move |mut output| async move { let _permit = LOCAL_VIDEO_TRANSCODING_TASKS.acquire().await?; debug!("transcoding fragment with {format:?}"); let mut args = Vec::::new(); match format.codec.as_str() { "V_AVC" => {} _ => unreachable!(), } info!("encoding with {:?}", args.join(" ")); let container = match container { StreamContainer::WebM => "webm", StreamContainer::Matroska => "matroska", StreamContainer::WebVTT => "vtt", StreamContainer::MPEG4 => "mp4", StreamContainer::JVTT => unreachable!(), }; let mut proc = Command::new("ffmpeg") .stdin(Stdio::piped()) .stdout(Stdio::piped()) .args(["-f", "matroska", "-i", "pipe:0"]) .args(args) .args(["-f", container, "pipe:1"]) .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 }