From 727be96686a2c6c5747b26be15933e11c9cab9c6 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 28 Jan 2024 17:55:11 +0100 Subject: clean up some code + subrip support? --- transcoder/src/subtitles.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'transcoder/src') diff --git a/transcoder/src/subtitles.rs b/transcoder/src/subtitles.rs index 84db5a9..9118ebc 100644 --- a/transcoder/src/subtitles.rs +++ b/transcoder/src/subtitles.rs @@ -3,11 +3,30 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2024 metamuffin */ -use anyhow::anyhow; +use anyhow::{anyhow, bail, Context}; use jellycommon::jhls::SubtitleCue; use std::fmt::Write; -// TODO discontinued for now. since this should be snippetized aswell. +pub fn parse_subtitles( + codec: &str, + codec_private: Option>, + blocks: Vec<(u64, u64, Vec)>, +) -> anyhow::Result> { + match codec { + "D_WEBVTT/SUBTITLES" => parse_webvtt_blocks(blocks), + "S_HDMV/PGS" => bail!("no HDMV/PGS yet"), + "S_HDMV/TEXTST" => bail!("no HDMV/PGS yet"), + "S_ARISUB" => bail!("no arisub yet"), + "S_TEXT/UTF8" => parse_subrip_blocks(blocks), + "S_VOBSUB" => bail!("no vobsub yet"), + "S_TEXT/ASS" => parse_ass_blocks( + codec_private.ok_or(anyhow!("ass without CodecPrivate"))?, + blocks, + ), + x => bail!("unknown sub codec {x:?}"), + } + .context(anyhow!("parsing {codec} subtitles")) +} pub fn write_webvtt(title: String, subtitles: Vec) -> anyhow::Result { let mut out = String::new(); @@ -39,12 +58,12 @@ pub fn parse_webvtt_blocks(blocks: Vec<(u64, u64, Vec)>) -> anyhow::Result)>) -> anyhow::Result> { +pub fn parse_subrip_blocks(blocks: Vec<(u64, u64, Vec)>) -> anyhow::Result> { let mut out = Vec::new(); for (pts, dur, block) in blocks { - let _content = String::from_utf8_lossy(&block).trim().to_string(); + let content = String::from_utf8_lossy(&block).trim().to_string(); out.push(SubtitleCue { - content: "PGS stub".to_string(), + content, start: pts as f64 / 1000., end: (pts + dur) as f64 / 1000., }); -- cgit v1.2.3-70-g09d2