/* 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; use std::fmt::Write; // TODO discontinued for now. since this should be snippetized aswell. pub fn webvtt_from_ass_blocks( title: String, _codec_private: Vec, blocks: Vec<(u64, u64, Vec)>, ) -> anyhow::Result { let mut out = String::new(); writeln!(out, "WEBVTT - {title}")?; // TODO ensure title does not contain "-->" writeln!(out)?; for (pts, dur, block) in blocks { let block = String::from_utf8(block)?; let text = convert_block(&block).ok_or(anyhow!("bad ass xD"))?; writeln!(out, "{} --> {}", format_time(pts), format_time(pts + dur))?; writeln!(out, "- {text}")?; writeln!(out)?; } Ok(out) } fn format_time(t: u64) -> String { let t = t as f64 / 1_000_000.; // let mmm = (t.fract() * 1000.).floor(); // let mmm = ((t * 60.).fract() * 60.).floor(); // let mmm = ((t / 60).fract() * 60.).floor(); // format!("{hh:4}:{mm:02}:{ss:02}.{mmm:03}") format!("{t}") } fn convert_block(s: &str) -> Option<&str> { // ReadOrder, Layer, Style, Name, MarginL, MarginR, MarginV, Effect, Text let (_read_order, s) = s.split_once(',')?; let (_layer, s) = s.split_once(',')?; let (_style, s) = s.split_once(',')?; let (_name, s) = s.split_once(',')?; let (_marginl, s) = s.split_once(',')?; let (_marginr, s) = s.split_once(',')?; let (_marginv, s) = s.split_once(',')?; let (_effect, text) = s.split_once(',')?; Some(text) }