/* 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) 2024 metamuffin */ use anyhow::{anyhow, Context, Result}; use jellybase::CONF; use jellycommon::{stream::StreamSpec, LocalTrack, Node}; use jellyremuxer::extract::extract_track; use jellytranscoder::subtitles::{parse_subtitles, write_webvtt}; use tokio::io::{AsyncWriteExt, DuplexStream}; pub async fn vtt_stream( json: bool, node: Node, local_tracks: Vec, spec: StreamSpec, mut b: DuplexStream, ) -> Result<()> { // TODO cache // TODO should use fragments too? big films take too long... let tracki = *spec.tracks.get(0).ok_or(anyhow!("no track selected"))?; let local_track = local_tracks.get(0).ok_or(anyhow!("no tracks"))?.clone(); let track = &node.public.media.unwrap().tracks[tracki]; let cp = local_track.codec_private.clone(); let blocks = tokio::task::spawn_blocking(move || extract_track(CONF.media_path.clone(), local_track)) .await??; let subtitles = parse_subtitles(&track.codec, cp, blocks)?; let output = if json { serde_json::to_string(&subtitles)? } else { write_webvtt(node.public.title.clone().unwrap_or_default(), subtitles) .context("writing webvtt")? }; tokio::task::spawn(async move { let _ = b.write_all(output.as_bytes()).await; }); Ok(()) }