aboutsummaryrefslogtreecommitdiff
path: root/stream/src/webvtt.rs
blob: ec2639816446fcd238bdaccf8e3d3f3be62f2a8d (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
/*
    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 <metamuffin.org>
*/
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<LocalTrack>,
    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(())
}