innertube_rs/models/
transcript.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
5#[serde(rename_all = "camelCase")]
6pub struct TranscriptTrack {
7 pub language_code: String,
8 pub name: String,
9 pub kind: Option<String>,
10 pub base_url: String,
11 pub is_translatable: bool,
12 pub vss_id: Option<String>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize, Default)]
18#[serde(rename_all = "camelCase")]
19pub struct TranscriptSegment {
20 pub start_ms: u64,
21 pub duration_ms: u64,
22 pub end_ms: u64,
23 pub text: String,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, Default)]
28#[serde(rename_all = "camelCase")]
29pub struct Transcript {
30 pub video_id: String,
31 pub language: String,
32 pub segments: Vec<TranscriptSegment>,
33}
34
35impl Transcript {
36 fn format_srt_time(ms: u64) -> String {
38 let total_seconds = ms / 1000;
39 let millis = ms % 1000;
40 let seconds = total_seconds % 60;
41 let minutes = (total_seconds / 60) % 60;
42 let hours = total_seconds / 3600;
43 format!("{:02}:{:02}:{:02},{:03}", hours, minutes, seconds, millis)
44 }
45
46 fn format_vtt_time(ms: u64) -> String {
48 let total_seconds = ms / 1000;
49 let millis = ms % 1000;
50 let seconds = total_seconds % 60;
51 let minutes = (total_seconds / 60) % 60;
52 let hours = total_seconds / 3600;
53 format!("{:02}:{:02}:{:02}.{:03}", hours, minutes, seconds, millis)
54 }
55
56 pub fn to_srt(&self) -> String {
58 let mut out = String::new();
59 for (i, seg) in self.segments.iter().enumerate() {
60 out.push_str(&format!("{}\n", i + 1));
61 out.push_str(&format!(
62 "{} --> {}\n",
63 Self::format_srt_time(seg.start_ms),
64 Self::format_srt_time(seg.end_ms)
65 ));
66 out.push_str(&format!("{}\n\n", seg.text.trim()));
67 }
68 out
69 }
70
71 pub fn to_vtt(&self) -> String {
73 let mut out = String::from("WEBVTT\n\n");
74 for (i, seg) in self.segments.iter().enumerate() {
75 out.push_str(&format!("{}\n", i + 1));
76 out.push_str(&format!(
77 "{} --> {}\n",
78 Self::format_vtt_time(seg.start_ms),
79 Self::format_vtt_time(seg.end_ms)
80 ));
81 out.push_str(&format!("{}\n\n", seg.text.trim()));
82 }
83 out
84 }
85
86 pub fn to_plain_text(&self) -> String {
88 self.segments
89 .iter()
90 .map(|s| s.text.trim())
91 .filter(|t| !t.is_empty())
92 .collect::<Vec<_>>()
93 .join("\n")
94 }
95}