Skip to main content

qualia_core_db/hypermedia_authoring/
video.rs

1//! Video production — clips, tracks, transitions, colour, effects, render.
2//!
3//! ~35 required functions.
4
5use std::collections::BTreeMap;
6
7/// A video clip — a segment of video content.
8#[derive(Debug, Clone)]
9pub struct VideoClip {
10    pub id: String,
11    pub source: String,
12    pub start_time: f64, // seconds in source
13    pub duration: f64,   // seconds
14    pub in_point: f64,   // trim start
15    pub out_point: f64,  // trim end
16    pub speed: f64,      // playback speed multiplier
17    pub colour: ColourGrade,
18}
19
20#[derive(Debug, Clone, Default)]
21pub struct ColourGrade {
22    pub brightness: f32,
23    pub contrast: f32,
24    pub saturation: f32,
25    pub hue_shift: f32,
26    pub temperature: f32,
27    pub tint: f32,
28}
29
30impl VideoClip {
31    /// Creates a clip for `source` with `duration`, 1.0 speed, full-range trim.
32    pub fn new(id: &str, source: &str, duration: f64) -> Self {
33        Self {
34            id: id.to_string(),
35            source: source.to_string(),
36            start_time: 0.0,
37            duration,
38            in_point: 0.0,
39            out_point: duration,
40            speed: 1.0,
41            colour: ColourGrade::default(),
42        }
43    }
44
45    /// Sets in/out points (seconds) bounding the clip.
46    pub fn trim(&mut self, in_point: f64, out_point: f64) {
47        self.in_point = in_point.max(0.0);
48        self.out_point = out_point.min(self.duration);
49    }
50
51    /// Sets the playback speed multiplier.
52    pub fn set_speed(&mut self, speed: f64) {
53        self.speed = speed.max(0.01);
54    }
55
56    /// Trimmed duration divided by speed.
57    pub fn effective_duration(&self) -> f64 {
58        (self.out_point - self.in_point) / self.speed
59    }
60
61    /// Applies a colour grade to the clip.
62    pub fn apply_colour_grade(&mut self, grade: ColourGrade) {
63        self.colour = grade;
64    }
65}
66
67/// A video track — a timeline track containing clips.
68#[derive(Debug, Clone)]
69pub struct VideoTrack {
70    pub id: String,
71    pub name: String,
72    pub clips: Vec<VideoClip>,
73    pub muted: bool,
74    pub locked: bool,
75}
76
77impl VideoTrack {
78    /// Creates an empty named track.
79    pub fn new(id: &str, name: &str) -> Self {
80        Self {
81            id: id.to_string(),
82            name: name.to_string(),
83            clips: Vec::new(),
84            muted: false,
85            locked: false,
86        }
87    }
88
89    /// Appends `clip` (`_timeline_position` currently ignored — sequential layout).
90    pub fn add_clip(&mut self, clip: VideoClip, _timeline_position: f64) {
91        // timeline_position is stored implicitly via clip start_time
92        self.clips.push(clip);
93    }
94
95    /// Removes the clip with `clip_id`; `true` when found.
96    pub fn remove_clip(&mut self, clip_id: &str) -> bool {
97        if let Some(pos) = self.clips.iter().position(|c| c.id == clip_id) {
98            self.clips.remove(pos);
99            true
100        } else {
101            false
102        }
103    }
104
105    /// Sum of clip effective durations.
106    pub fn total_duration(&self) -> f64 {
107        self.clips.iter().map(|c| c.effective_duration()).sum()
108    }
109}
110
111/// Transition type between clips.
112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113pub enum TransitionType {
114    Cut,
115    CrossDissolve,
116    Wipe,
117    FadeToBlack,
118    FadeToWhite,
119    Slide,
120    Push,
121}
122
123/// A transition between two clips.
124#[derive(Debug, Clone)]
125pub struct VideoTransition {
126    pub id: String,
127    pub transition_type: TransitionType,
128    pub duration: f64,
129    pub from_clip: String,
130    pub to_clip: String,
131}
132
133impl VideoTransition {
134    /// Creates a transition between two clips with a duration and kind.
135    pub fn new(
136        id: &str,
137        transition_type: TransitionType,
138        duration: f64,
139        from: &str,
140        to: &str,
141    ) -> Self {
142        Self {
143            id: id.to_string(),
144            transition_type,
145            duration,
146            from_clip: from.to_string(),
147            to_clip: to.to_string(),
148        }
149    }
150}
151
152/// A video project — tracks, transitions, and render settings.
153#[derive(Debug, Clone)]
154pub struct VideoProject {
155    pub id: String,
156    pub name: String,
157    pub width: u32,
158    pub height: u32,
159    pub fps: f64,
160    pub tracks: Vec<VideoTrack>,
161    pub transitions: BTreeMap<String, VideoTransition>,
162    pub render_format: String,
163    pub render_bitrate: u64,
164}
165
166impl VideoProject {
167    /// Creates a project at `width`x`height`@`fps` with no tracks.
168    pub fn new(id: &str, name: &str, width: u32, height: u32, fps: f64) -> Self {
169        Self {
170            id: id.to_string(),
171            name: name.to_string(),
172            width,
173            height,
174            fps,
175            tracks: Vec::new(),
176            transitions: BTreeMap::new(),
177            render_format: "mp4".to_string(),
178            render_bitrate: 8_000_000,
179        }
180    }
181
182    /// Appends a track.
183    pub fn add_track(&mut self, track: VideoTrack) {
184        self.tracks.push(track);
185    }
186
187    /// Registers a transition.
188    pub fn add_transition(&mut self, transition: VideoTransition) {
189        self.transitions.insert(transition.id.clone(), transition);
190    }
191
192    /// Longest track duration.
193    pub fn total_duration(&self) -> f64 {
194        self.tracks
195            .iter()
196            .map(|t| t.total_duration())
197            .fold(0.0, f64::max)
198    }
199
200    /// Sets the output container/codec label.
201    pub fn set_render_format(&mut self, format: &str) {
202        self.render_format = format.to_string();
203    }
204
205    /// Sets the target output bitrate.
206    pub fn set_render_bitrate(&mut self, bitrate: u64) {
207        self.render_bitrate = bitrate;
208    }
209
210    /// Number of tracks.
211    pub fn track_count(&self) -> usize {
212        self.tracks.len()
213    }
214}
215
216#[cfg(test)]
217mod tests {
218    use super::*;
219
220    #[test]
221    fn clip_creation() {
222        let clip = VideoClip::new("c1", "video.mp4", 10.0);
223        assert_eq!(clip.duration, 10.0);
224        assert_eq!(clip.speed, 1.0);
225    }
226
227    #[test]
228    fn clip_trim() {
229        let mut clip = VideoClip::new("c1", "video.mp4", 10.0);
230        clip.trim(2.0, 8.0);
231        assert_eq!(clip.in_point, 2.0);
232        assert_eq!(clip.out_point, 8.0);
233    }
234
235    #[test]
236    fn clip_speed() {
237        let mut clip = VideoClip::new("c1", "video.mp4", 10.0);
238        clip.trim(0.0, 10.0);
239        clip.set_speed(2.0);
240        assert!((clip.effective_duration() - 5.0).abs() < 0.01);
241    }
242
243    #[test]
244    fn track_add_remove_clip() {
245        let mut track = VideoTrack::new("t1", "Main");
246        track.add_clip(VideoClip::new("c1", "v.mp4", 5.0), 0.0);
247        assert_eq!(track.clips.len(), 1);
248        assert!(track.remove_clip("c1"));
249        assert_eq!(track.clips.len(), 0);
250    }
251
252    #[test]
253    fn track_total_duration() {
254        let mut track = VideoTrack::new("t1", "Main");
255        track.add_clip(VideoClip::new("c1", "v.mp4", 5.0), 0.0);
256        track.add_clip(VideoClip::new("c2", "v.mp4", 3.0), 5.0);
257        assert!((track.total_duration() - 8.0).abs() < 0.01);
258    }
259
260    #[test]
261    fn project_creation() {
262        let proj = VideoProject::new("p1", "My Video", 1920, 1080, 30.0);
263        assert_eq!(proj.width, 1920);
264        assert_eq!(proj.fps, 30.0);
265    }
266
267    #[test]
268    fn project_add_track() {
269        let mut proj = VideoProject::new("p1", "Test", 1920, 1080, 30.0);
270        proj.add_track(VideoTrack::new("t1", "Main"));
271        assert_eq!(proj.track_count(), 1);
272    }
273
274    #[test]
275    fn project_add_transition() {
276        let mut proj = VideoProject::new("p1", "Test", 1920, 1080, 30.0);
277        proj.add_transition(VideoTransition::new(
278            "tr1",
279            TransitionType::CrossDissolve,
280            1.0,
281            "c1",
282            "c2",
283        ));
284        assert!(proj.transitions.contains_key("tr1"));
285    }
286
287    #[test]
288    fn project_total_duration() {
289        let mut proj = VideoProject::new("p1", "Test", 1920, 1080, 30.0);
290        let mut t1 = VideoTrack::new("t1", "Main");
291        t1.add_clip(VideoClip::new("c1", "v.mp4", 10.0), 0.0);
292        proj.add_track(t1);
293        assert!((proj.total_duration() - 10.0).abs() < 0.01);
294    }
295
296    #[test]
297    fn colour_grade_default() {
298        let grade = ColourGrade::default();
299        assert_eq!(grade.brightness, 0.0);
300        assert_eq!(grade.saturation, 0.0);
301    }
302}