video_subtitle/types.rs
1//! 跨模块共享的领域类型与时间换算。
2
3/// 一条带起止时间的字幕片段。
4///
5/// 时间轴统一使用**毫秒**,与 SRT 写入模块一致;
6/// Whisper 原始输出为 centisecond(1 cs = 10 ms),经 [`centis_to_ms`] 转换后填入本结构。
7#[derive(Debug, Clone)]
8pub struct Caption {
9 /// 字幕开始时间(毫秒,含)。
10 pub start_ms: u64,
11 /// 字幕结束时间(毫秒,不含或含取决于播放器;本库保证 `end_ms > start_ms`)。
12 pub end_ms: u64,
13 /// 字幕正文(已去除首尾空白)。
14 pub text: String,
15}
16
17impl Caption {
18 /// 构造一条字幕片段。
19 ///
20 /// # 参数
21 ///
22 /// - `start_ms` / `end_ms`:毫秒时间戳
23 /// - `text`:任意可转为 [`String`] 的文本
24 pub fn new(start_ms: u64, end_ms: u64, text: impl Into<String>) -> Self {
25 Self {
26 start_ms,
27 end_ms,
28 text: text.into(),
29 }
30 }
31}
32
33/// 将 Whisper 时间戳从 **centisecond**(10 ms 为单位)转为 **毫秒**。
34///
35/// Whisper API 中 `start_timestamp` / `end_timestamp` 返回值的单位为 centisecond;
36/// 负数会被钳制为 0。
37///
38/// # 示例
39///
40/// ```
41/// use video_subtitle::types::centis_to_ms;
42/// assert_eq!(centis_to_ms(100), 1_000); // 100 cs = 1 s
43/// ```
44pub fn centis_to_ms(cs: i64) -> u64 {
45 (cs.max(0) as u64) * 10
46}