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
|
/*
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) 2023 metamuffin <metamuffin.org>
*/
use anyhow::{anyhow, bail, Context, Result};
use jellybase::CONF;
use jellycommon::{stream::StreamSpec, LocalTrack, Node};
use jellyremuxer::extract::extract_track;
use jellytranscoder::subtitles::webvtt_from_ass_blocks;
use tokio::io::{AsyncWriteExt, DuplexStream};
pub async fn webvtt_stream(
node: Node,
local_tracks: Vec<LocalTrack>,
spec: StreamSpec,
mut b: DuplexStream,
) -> Result<()> {
// TODO cache
// TODO should use snippets 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];
match track.codec.as_str() {
"S_TEXT/UTF8" => bail!("no subrip yet"),
"S_VOBSUB" => bail!("no vobsub yet"),
"S_TEXT/ASS" => {
let codec_private = local_track
.codec_private
.clone()
.ok_or(anyhow!("ASS is missing required codec private data"))?;
let ass_blocks = tokio::task::spawn_blocking(move || {
extract_track(CONF.library_path.clone(), local_track)
})
.await??;
let webvtt = webvtt_from_ass_blocks(
node.public.title.clone().unwrap_or_default(),
codec_private,
ass_blocks,
)
.context("transcoding subtitles")?;
tokio::task::spawn(async move {
let _ = b.write_all(webvtt.as_bytes()).await;
});
}
_ => bail!("unknown sub codec"),
};
Ok(())
}
|