Created
August 22, 2023 02:37
-
-
Save vi/40c0c18e869f811b08231e0a98a5a473 to your computer and use it in GitHub Desktop.
Converter from twitchchatdownloader to srt.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S cargo +nightly -Zscript | |
//! Convert Twitch subtitles exported as CSV from https://www.twitchchatdownloader.com to srt format | |
//! | |
//! ```cargo | |
//! [package] | |
//! edition="2021" | |
//! [dependencies] | |
//! csv = "1" | |
//! anyhow="1" | |
//! srtlib = "0.1.0" | |
//! unicode-segmentation="1" | |
//! ``` | |
pub fn main() -> anyhow::Result<()> { | |
use unicode_segmentation::UnicodeSegmentation; | |
let subs = Vec::with_capacity(1000); | |
let mut num = 1; | |
let mut subs = srtlib::Subtitles::new_from_vec(subs); | |
let mut r = csv::Reader::from_reader(std::io::stdin()); | |
let header = r.headers()?; | |
anyhow::ensure!(header == &csv::StringRecord::from(vec!["time", "user_name", "user_color", "message"])); | |
let enef = ||anyhow::anyhow!("No enough fields in a csv line"); | |
for l in r.records() { | |
let l = l?; | |
let t : i32 = l.get(0).ok_or_else(enef)?.parse()?; | |
let name : &str = &l.get(1).ok_or_else(enef)?; | |
let comment : &str = &l.get(3).ok_or_else(enef)?; | |
let mut start_time = srtlib::Timestamp::new(0,0,0,0); | |
start_time.add_seconds(t); | |
let mut end_time = start_time.clone(); | |
end_time.add_seconds(1); | |
end_time.add_seconds((comment.graphemes(true).count() / 20) as i32 ); | |
let s = srtlib::Subtitle::new( | |
num, | |
start_time, | |
end_time, | |
format!("[{}] {}", name, comment) | |
); | |
subs.push(s); | |
num+=1; | |
} | |
println!("{}", subs.to_string()); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment