1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! Cue points for marking significant moments in a subtitle track.
//!
//! Cue points are time-stamped annotations attached to a subtitle stream that
//! flag events such as chapter boundaries, advertisement breaks, or explicit
//! subtitle positions. They complement subtitle cues but are distinct from
//! them: a cue point carries a *type* and optional *label* rather than
//! displayable text.
#![allow(dead_code)]
/// The semantic category of a cue point.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CueType {
/// Start of a chapter or named section.
Chapter,
/// An advertisement break boundary.
AdBreak,
/// A bookmark set interactively by the user.
Bookmark,
/// A scene transition detected automatically.
SceneChange,
/// A forced subtitle display event.
ForcedSubtitle,
/// Any application-defined custom event.
Custom(String),
}
impl CueType {
/// Return a human-readable label for the cue type.
#[must_use]
pub fn label(&self) -> &str {
match self {
Self::Chapter => "Chapter",
Self::AdBreak => "AdBreak",
Self::Bookmark => "Bookmark",
Self::SceneChange => "SceneChange",
Self::ForcedSubtitle => "ForcedSubtitle",
Self::Custom(s) => s.as_str(),
}
}
}
/// A single cue point: a typed, timestamped annotation on the subtitle track.
///
/// # Example
///
/// ```
/// use oximedia_subtitle::cue_point::{CuePoint, CueType};
///
/// let cp = CuePoint::new(CueType::Chapter, 5000)
/// .with_label("Act 1");
/// assert_eq!(cp.label.as_deref(), Some("Act 1"));
/// ```
#[derive(Debug, Clone)]
pub struct CuePoint {
/// Semantic type of this cue point.
pub cue_type: CueType,
/// Timestamp of the cue point in milliseconds.
pub timestamp_ms: i64,
/// Optional human-readable label.
pub label: Option<String>,
/// Optional duration in milliseconds (e.g. for ad breaks).
pub duration_ms: Option<i64>,
}
impl CuePoint {
/// Create a new cue point at the given timestamp.
#[must_use]
pub fn new(cue_type: CueType, timestamp_ms: i64) -> Self {
Self {
cue_type,
timestamp_ms,
label: None,
duration_ms: None,
}
}
/// Attach a label to this cue point.
#[must_use]
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
/// Attach a duration to this cue point.
#[must_use]
pub fn with_duration(mut self, duration_ms: i64) -> Self {
self.duration_ms = Some(duration_ms);
self
}
/// Return `true` if the cue point falls within `[from_ms, to_ms)`.
#[must_use]
pub fn in_range(&self, from_ms: i64, to_ms: i64) -> bool {
self.timestamp_ms >= from_ms && self.timestamp_ms < to_ms
}
}
/// An ordered, mutable collection of [`CuePoint`]s.
///
/// # Example
///
/// ```
/// use oximedia_subtitle::cue_point::{CuePoint, CuePointList, CueType};
///
/// let mut list = CuePointList::new();
/// list.add(CuePoint::new(CueType::Chapter, 0));
/// list.add(CuePoint::new(CueType::AdBreak, 30_000));
/// list.add(CuePoint::new(CueType::Chapter, 60_000));
///
/// let hits = list.in_range(25_000, 65_000);
/// assert_eq!(hits.len(), 2);
/// ```
#[derive(Debug, Default)]
pub struct CuePointList {
points: Vec<CuePoint>,
}
impl CuePointList {
/// Create an empty list.
#[must_use]
pub fn new() -> Self {
Self { points: Vec::new() }
}
/// Append a cue point. The list is kept sorted by timestamp.
pub fn add(&mut self, cue: CuePoint) {
let pos = self
.points
.partition_point(|p| p.timestamp_ms <= cue.timestamp_ms);
self.points.insert(pos, cue);
}
/// Return the number of cue points.
#[must_use]
pub fn len(&self) -> usize {
self.points.len()
}
/// Returns `true` if the list contains no cue points.
#[must_use]
pub fn is_empty(&self) -> bool {
self.points.is_empty()
}
/// Return all cue points whose timestamp falls within `[from_ms, to_ms)`.
#[must_use]
pub fn in_range(&self, from_ms: i64, to_ms: i64) -> Vec<&CuePoint> {
self.points
.iter()
.filter(|p| p.in_range(from_ms, to_ms))
.collect()
}
/// Return all cue points of a specific type.
#[must_use]
pub fn by_type(&self, cue_type: &CueType) -> Vec<&CuePoint> {
self.points
.iter()
.filter(|p| &p.cue_type == cue_type)
.collect()
}
/// Return the cue point immediately at or before the given timestamp,
/// if any.
#[must_use]
pub fn latest_at(&self, timestamp_ms: i64) -> Option<&CuePoint> {
self.points
.iter()
.rev()
.find(|p| p.timestamp_ms <= timestamp_ms)
}
/// Remove all cue points of the specified type.
pub fn remove_type(&mut self, cue_type: &CueType) {
self.points.retain(|p| &p.cue_type != cue_type);
}
/// Return an iterator over all cue points in timestamp order.
pub fn iter(&self) -> impl Iterator<Item = &CuePoint> {
self.points.iter()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_list() -> CuePointList {
let mut list = CuePointList::new();
list.add(CuePoint::new(CueType::Chapter, 0).with_label("Intro"));
list.add(CuePoint::new(CueType::AdBreak, 30_000).with_duration(15_000));
list.add(CuePoint::new(CueType::Chapter, 60_000).with_label("Act 1"));
list.add(CuePoint::new(CueType::SceneChange, 45_000));
list.add(CuePoint::new(CueType::Bookmark, 90_000).with_label("Fav"));
list
}
#[test]
fn test_list_len() {
let list = make_list();
assert_eq!(list.len(), 5);
}
#[test]
fn test_list_is_sorted() {
let list = make_list();
let ts: Vec<i64> = list.iter().map(|p| p.timestamp_ms).collect();
let mut sorted = ts.clone();
sorted.sort_unstable();
assert_eq!(ts, sorted);
}
#[test]
fn test_in_range_basic() {
let list = make_list();
let hits = list.in_range(0, 31_000);
// should include 0 (Chapter) and 30_000 (AdBreak), not 45_000+
assert_eq!(hits.len(), 2);
}
#[test]
fn test_in_range_exclusive_end() {
let list = make_list();
// 30_000 should NOT be in [0, 30_000)
let hits = list.in_range(0, 30_000);
assert!(!hits.iter().any(|p| p.timestamp_ms == 30_000));
}
#[test]
fn test_in_range_empty() {
let list = make_list();
let hits = list.in_range(200_000, 300_000);
assert!(hits.is_empty());
}
#[test]
fn test_by_type_chapter() {
let list = make_list();
let chapters = list.by_type(&CueType::Chapter);
assert_eq!(chapters.len(), 2);
}
#[test]
fn test_by_type_no_match() {
let list = make_list();
let custom = list.by_type(&CueType::Custom("foo".to_string()));
assert!(custom.is_empty());
}
#[test]
fn test_latest_at() {
let list = make_list();
let cp = list.latest_at(50_000).expect("should succeed in test");
assert_eq!(cp.timestamp_ms, 45_000);
}
#[test]
fn test_latest_at_none() {
let list = make_list();
assert!(list.latest_at(-1).is_none());
}
#[test]
fn test_remove_type() {
let mut list = make_list();
list.remove_type(&CueType::Chapter);
assert!(list.by_type(&CueType::Chapter).is_empty());
// Others unaffected
assert_eq!(list.len(), 3);
}
#[test]
fn test_cue_point_in_range() {
let cp = CuePoint::new(CueType::Bookmark, 5000);
assert!(cp.in_range(4000, 6000));
// in_range is [from, to): from_ms=5000 and timestamp_ms=5000 => 5000 >= 5000 is true
assert!(cp.in_range(5000, 6000));
assert!(!cp.in_range(5001, 6000));
assert!(!cp.in_range(6000, 9000));
}
#[test]
fn test_cue_type_label_custom() {
let t = CueType::Custom("my-event".to_string());
assert_eq!(t.label(), "my-event");
}
#[test]
fn test_cue_type_label_standard() {
assert_eq!(CueType::Chapter.label(), "Chapter");
assert_eq!(CueType::AdBreak.label(), "AdBreak");
assert_eq!(CueType::SceneChange.label(), "SceneChange");
}
#[test]
fn test_with_duration() {
let cp = CuePoint::new(CueType::AdBreak, 0).with_duration(30_000);
assert_eq!(cp.duration_ms, Some(30_000));
}
#[test]
fn test_empty_list() {
let list = CuePointList::new();
assert!(list.is_empty());
assert!(list.latest_at(9999).is_none());
assert!(list.in_range(0, 1000).is_empty());
}
}