oximedia-edl 0.1.8

Edit Decision List (EDL) parser and generator for media workflows
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! EDL-to-timeline conversion.
//!
//! This module converts EDL events into a simplified timeline model
//! consisting of clips placed on tracks with in/out points.
//! Dissolves are handled by creating overlapping clips on the same track.

use crate::error::{EdlError, EdlResult};
use crate::event::{EditType, EdlEvent};
use crate::timecode::EdlFrameRate;

/// Configuration for EDL-to-timeline conversion.
#[derive(Debug, Clone)]
pub struct EdlToTimelineConfig {
    /// Frame rate to use for the output timeline.
    pub frame_rate: EdlFrameRate,
    /// Whether to create separate tracks for video and audio.
    pub separate_audio_video: bool,
    /// Whether to handle dissolves by creating overlapping clips.
    pub expand_dissolves: bool,
    /// Default track name for video clips when none is specified.
    pub default_video_track: String,
    /// Default track name for audio clips when none is specified.
    pub default_audio_track: String,
}

impl Default for EdlToTimelineConfig {
    fn default() -> Self {
        Self {
            frame_rate: EdlFrameRate::Fps25,
            separate_audio_video: true,
            expand_dissolves: true,
            default_video_track: "V1".to_string(),
            default_audio_track: "A1".to_string(),
        }
    }
}

impl EdlToTimelineConfig {
    /// Create a new config with a specific frame rate.
    #[must_use]
    pub fn with_frame_rate(mut self, frame_rate: EdlFrameRate) -> Self {
        self.frame_rate = frame_rate;
        self
    }

    /// Set whether to separate audio and video tracks.
    #[must_use]
    pub fn with_separate_audio_video(mut self, separate: bool) -> Self {
        self.separate_audio_video = separate;
        self
    }

    /// Set whether to expand dissolves into overlapping clips.
    #[must_use]
    pub fn with_expand_dissolves(mut self, expand: bool) -> Self {
        self.expand_dissolves = expand;
        self
    }
}

/// A clip placed on a timeline track.
#[derive(Debug, Clone, PartialEq)]
pub struct TimelineClip {
    /// Unique identifier for this clip (derived from EDL event number).
    pub id: u32,
    /// Source reel/clip name.
    pub reel: String,
    /// Optional descriptive clip name.
    pub clip_name: Option<String>,
    /// Source in point (frames).
    pub source_in_frames: u64,
    /// Source out point (frames).
    pub source_out_frames: u64,
    /// Record/timeline in point (frames).
    pub timeline_in_frames: u64,
    /// Record/timeline out point (frames).
    pub timeline_out_frames: u64,
    /// The edit type that placed this clip.
    pub edit_type: EditType,
    /// Whether this clip is the outgoing side of a dissolve.
    pub is_dissolve_outgoing: bool,
    /// Whether this clip is the incoming side of a dissolve.
    pub is_dissolve_incoming: bool,
    /// Transition duration in frames (for dissolves/wipes), if any.
    pub transition_frames: Option<u32>,
}

impl TimelineClip {
    /// Duration of this clip on the timeline, in frames.
    #[must_use]
    pub fn duration_frames(&self) -> u64 {
        self.timeline_out_frames
            .saturating_sub(self.timeline_in_frames)
    }

    /// Source duration in frames.
    #[must_use]
    pub fn source_duration_frames(&self) -> u64 {
        self.source_out_frames.saturating_sub(self.source_in_frames)
    }
}

/// A named track in the timeline.
#[derive(Debug, Clone)]
pub struct TimelineTrack {
    /// Track name (e.g. "V1", "A1").
    pub name: String,
    /// Whether this is a video track.
    pub is_video: bool,
    /// Clips on this track, ordered by timeline_in_frames.
    pub clips: Vec<TimelineClip>,
}

impl TimelineTrack {
    /// Create a new empty track.
    #[must_use]
    pub fn new(name: impl Into<String>, is_video: bool) -> Self {
        Self {
            name: name.into(),
            is_video,
            clips: Vec::new(),
        }
    }

    /// Total duration covered by clips on this track (max timeline_out - min timeline_in).
    #[must_use]
    pub fn span_frames(&self) -> u64 {
        if self.clips.is_empty() {
            return 0;
        }
        let min_in = self
            .clips
            .iter()
            .map(|c| c.timeline_in_frames)
            .min()
            .unwrap_or(0);
        let max_out = self
            .clips
            .iter()
            .map(|c| c.timeline_out_frames)
            .max()
            .unwrap_or(0);
        max_out.saturating_sub(min_in)
    }

    /// Number of clips on this track.
    #[must_use]
    pub fn clip_count(&self) -> usize {
        self.clips.len()
    }

    /// Sort clips by timeline in point.
    pub fn sort_clips(&mut self) {
        self.clips.sort_by_key(|c| c.timeline_in_frames);
    }
}

/// A complete timeline produced from EDL conversion.
#[derive(Debug, Clone)]
pub struct Timeline {
    /// Title (from the EDL).
    pub title: Option<String>,
    /// Frame rate.
    pub frame_rate: EdlFrameRate,
    /// Tracks in the timeline.
    pub tracks: Vec<TimelineTrack>,
}

impl Timeline {
    /// Total number of clips across all tracks.
    #[must_use]
    pub fn total_clips(&self) -> usize {
        self.tracks.iter().map(|t| t.clip_count()).sum()
    }

    /// Total number of tracks.
    #[must_use]
    pub fn track_count(&self) -> usize {
        self.tracks.len()
    }

    /// Get a track by name.
    #[must_use]
    pub fn get_track(&self, name: &str) -> Option<&TimelineTrack> {
        self.tracks.iter().find(|t| t.name == name)
    }

    /// Get a mutable track by name.
    pub fn get_track_mut(&mut self, name: &str) -> Option<&mut TimelineTrack> {
        self.tracks.iter_mut().find(|t| t.name == name)
    }

    /// Overall timeline duration (max of all track spans).
    #[must_use]
    pub fn duration_frames(&self) -> u64 {
        self.tracks
            .iter()
            .map(|t| {
                t.clips
                    .iter()
                    .map(|c| c.timeline_out_frames)
                    .max()
                    .unwrap_or(0)
            })
            .max()
            .unwrap_or(0)
    }
}

/// Convert an EDL event list into a `Timeline`.
///
/// # Errors
///
/// Returns `EdlError` if timecode conversion fails.
pub fn convert_to_timeline(
    events: &[EdlEvent],
    title: Option<String>,
    config: &EdlToTimelineConfig,
) -> EdlResult<Timeline> {
    let mut timeline = Timeline {
        title,
        frame_rate: config.frame_rate,
        tracks: Vec::new(),
    };

    for event in events {
        let track_name = determine_track_name(event, config);
        let is_video = event.track.has_video();

        // Ensure track exists
        if timeline.get_track(&track_name).is_none() {
            timeline
                .tracks
                .push(TimelineTrack::new(&track_name, is_video));
        }

        if config.expand_dissolves && event.edit_type == EditType::Dissolve {
            // For a dissolve, the incoming clip extends earlier by the transition duration.
            // We represent this as a normal clip whose timeline-in is pulled back.
            let trans_dur = event.transition_duration.unwrap_or(0) as u64;

            let clip = TimelineClip {
                id: event.number,
                reel: event.reel.clone(),
                clip_name: event.clip_name.clone(),
                source_in_frames: event.source_in.to_frames(),
                source_out_frames: event.source_out.to_frames(),
                timeline_in_frames: event.record_in.to_frames().saturating_sub(trans_dur),
                timeline_out_frames: event.record_out.to_frames(),
                edit_type: event.edit_type,
                is_dissolve_outgoing: false,
                is_dissolve_incoming: true,
                transition_frames: event.transition_duration,
            };

            let track = timeline
                .get_track_mut(&track_name)
                .ok_or_else(|| EdlError::ValidationError("Track not found".to_string()))?;
            track.clips.push(clip);
        } else {
            let clip = TimelineClip {
                id: event.number,
                reel: event.reel.clone(),
                clip_name: event.clip_name.clone(),
                source_in_frames: event.source_in.to_frames(),
                source_out_frames: event.source_out.to_frames(),
                timeline_in_frames: event.record_in.to_frames(),
                timeline_out_frames: event.record_out.to_frames(),
                edit_type: event.edit_type,
                is_dissolve_outgoing: false,
                is_dissolve_incoming: false,
                transition_frames: event.transition_duration,
            };

            let track = timeline
                .get_track_mut(&track_name)
                .ok_or_else(|| EdlError::ValidationError("Track not found".to_string()))?;
            track.clips.push(clip);
        }
    }

    // Sort clips on every track
    for track in &mut timeline.tracks {
        track.sort_clips();
    }

    Ok(timeline)
}

/// Determine which track name an event should be placed on.
fn determine_track_name(event: &EdlEvent, config: &EdlToTimelineConfig) -> String {
    if config.separate_audio_video {
        if event.track.has_video() {
            config.default_video_track.clone()
        } else {
            config.default_audio_track.clone()
        }
    } else {
        // Put everything on the video track
        config.default_video_track.clone()
    }
}

/// Helper to create a simple timeline from an EDL with default config.
///
/// # Errors
///
/// Returns `EdlError` if conversion fails.
pub fn convert_edl_to_timeline(edl: &crate::Edl) -> EdlResult<Timeline> {
    let config = EdlToTimelineConfig::default().with_frame_rate(edl.frame_rate);
    convert_to_timeline(&edl.events, edl.title.clone(), &config)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::audio::AudioChannel;
    use crate::event::{EditType, TrackType};
    use crate::timecode::{EdlFrameRate, EdlTimecode};

    fn tc(h: u8, m: u8, s: u8, f: u8) -> EdlTimecode {
        EdlTimecode::new(h, m, s, f, EdlFrameRate::Fps25).expect("valid timecode")
    }

    fn make_cut(num: u32, reel: &str, rec_in: EdlTimecode, rec_out: EdlTimecode) -> EdlEvent {
        EdlEvent::new(
            num,
            reel.to_string(),
            TrackType::Video,
            EditType::Cut,
            rec_in,
            rec_out,
            rec_in,
            rec_out,
        )
    }

    fn make_dissolve(
        num: u32,
        reel: &str,
        rec_in: EdlTimecode,
        rec_out: EdlTimecode,
        trans_dur: u32,
    ) -> EdlEvent {
        let mut ev = EdlEvent::new(
            num,
            reel.to_string(),
            TrackType::Video,
            EditType::Dissolve,
            rec_in,
            rec_out,
            rec_in,
            rec_out,
        );
        ev.set_transition_duration(trans_dur);
        ev
    }

    #[test]
    fn test_basic_conversion() {
        let events = vec![
            make_cut(1, "A001", tc(1, 0, 0, 0), tc(1, 0, 5, 0)),
            make_cut(2, "A002", tc(1, 0, 5, 0), tc(1, 0, 10, 0)),
        ];
        let config = EdlToTimelineConfig::default();
        let tl = convert_to_timeline(&events, Some("Test".to_string()), &config)
            .expect("conversion should succeed");

        assert_eq!(tl.title.as_deref(), Some("Test"));
        assert_eq!(tl.total_clips(), 2);
        assert_eq!(tl.track_count(), 1);
    }

    #[test]
    fn test_clips_on_correct_track() {
        let events = vec![make_cut(1, "A001", tc(1, 0, 0, 0), tc(1, 0, 5, 0))];
        let config = EdlToTimelineConfig::default();
        let tl = convert_to_timeline(&events, None, &config).expect("conversion should succeed");

        let track = tl.get_track("V1").expect("V1 track should exist");
        assert!(track.is_video);
        assert_eq!(track.clip_count(), 1);
        assert_eq!(track.clips[0].reel, "A001");
    }

    #[test]
    fn test_audio_event_separate_track() {
        let audio_event = EdlEvent::new(
            1,
            "AUD01".to_string(),
            TrackType::Audio(AudioChannel::A1),
            EditType::Cut,
            tc(1, 0, 0, 0),
            tc(1, 0, 5, 0),
            tc(1, 0, 0, 0),
            tc(1, 0, 5, 0),
        );
        let config = EdlToTimelineConfig::default();
        let tl =
            convert_to_timeline(&[audio_event], None, &config).expect("conversion should succeed");

        assert_eq!(tl.track_count(), 1);
        let track = tl.get_track("A1").expect("A1 track should exist");
        assert!(!track.is_video);
        assert_eq!(track.clip_count(), 1);
    }

    #[test]
    fn test_dissolve_expands_overlap() {
        let events = vec![
            make_cut(1, "A001", tc(1, 0, 0, 0), tc(1, 0, 10, 0)),
            make_dissolve(2, "A002", tc(1, 0, 10, 0), tc(1, 0, 20, 0), 25), // 1-second dissolve at 25fps
        ];
        let config = EdlToTimelineConfig::default();
        let tl = convert_to_timeline(&events, None, &config).expect("conversion should succeed");

        let track = tl.get_track("V1").expect("V1 track should exist");
        assert_eq!(track.clip_count(), 2);

        // The dissolve clip should have its timeline_in pulled back by 25 frames
        let dissolve_clip = &track.clips[1];
        assert!(dissolve_clip.is_dissolve_incoming);
        let expected_in = tc(1, 0, 10, 0).to_frames() - 25;
        assert_eq!(dissolve_clip.timeline_in_frames, expected_in);
    }

    #[test]
    fn test_dissolve_without_expansion() {
        let events = vec![make_dissolve(
            1,
            "A001",
            tc(1, 0, 0, 0),
            tc(1, 0, 10, 0),
            25,
        )];
        let config = EdlToTimelineConfig::default().with_expand_dissolves(false);
        let tl = convert_to_timeline(&events, None, &config).expect("conversion should succeed");

        let track = tl.get_track("V1").expect("V1 track should exist");
        let clip = &track.clips[0];
        assert!(!clip.is_dissolve_incoming);
        // Timeline in should NOT be pulled back
        assert_eq!(clip.timeline_in_frames, tc(1, 0, 0, 0).to_frames());
    }

    #[test]
    fn test_mixed_video_and_audio_separate() {
        let video = make_cut(1, "V01", tc(1, 0, 0, 0), tc(1, 0, 5, 0));
        let audio = EdlEvent::new(
            2,
            "AUD01".to_string(),
            TrackType::Audio(AudioChannel::A1),
            EditType::Cut,
            tc(1, 0, 0, 0),
            tc(1, 0, 5, 0),
            tc(1, 0, 0, 0),
            tc(1, 0, 5, 0),
        );
        let config = EdlToTimelineConfig::default();
        let tl =
            convert_to_timeline(&[video, audio], None, &config).expect("conversion should succeed");

        assert_eq!(tl.track_count(), 2);
        assert!(tl.get_track("V1").is_some());
        assert!(tl.get_track("A1").is_some());
    }

    #[test]
    fn test_mixed_without_separate() {
        let video = make_cut(1, "V01", tc(1, 0, 0, 0), tc(1, 0, 5, 0));
        let audio = EdlEvent::new(
            2,
            "AUD01".to_string(),
            TrackType::Audio(AudioChannel::A1),
            EditType::Cut,
            tc(1, 0, 0, 0),
            tc(1, 0, 5, 0),
            tc(1, 0, 0, 0),
            tc(1, 0, 5, 0),
        );
        let config = EdlToTimelineConfig::default().with_separate_audio_video(false);
        let tl =
            convert_to_timeline(&[video, audio], None, &config).expect("conversion should succeed");

        assert_eq!(tl.track_count(), 1);
        assert_eq!(tl.total_clips(), 2);
    }

    #[test]
    fn test_clip_duration() {
        let clip = TimelineClip {
            id: 1,
            reel: "A001".to_string(),
            clip_name: None,
            source_in_frames: 0,
            source_out_frames: 125,
            timeline_in_frames: 100,
            timeline_out_frames: 225,
            edit_type: EditType::Cut,
            is_dissolve_outgoing: false,
            is_dissolve_incoming: false,
            transition_frames: None,
        };
        assert_eq!(clip.duration_frames(), 125);
        assert_eq!(clip.source_duration_frames(), 125);
    }

    #[test]
    fn test_timeline_duration() {
        let events = vec![
            make_cut(1, "A001", tc(1, 0, 0, 0), tc(1, 0, 5, 0)),
            make_cut(2, "A002", tc(1, 0, 5, 0), tc(1, 0, 10, 0)),
        ];
        let config = EdlToTimelineConfig::default();
        let tl = convert_to_timeline(&events, None, &config).expect("conversion should succeed");

        let expected_dur = tc(1, 0, 10, 0).to_frames();
        assert_eq!(tl.duration_frames(), expected_dur);
    }

    #[test]
    fn test_track_span() {
        let events = vec![
            make_cut(1, "A001", tc(1, 0, 0, 0), tc(1, 0, 5, 0)),
            make_cut(2, "A002", tc(1, 0, 10, 0), tc(1, 0, 15, 0)),
        ];
        let config = EdlToTimelineConfig::default();
        let tl = convert_to_timeline(&events, None, &config).expect("conversion should succeed");

        let track = tl.get_track("V1").expect("V1 should exist");
        let span = track.span_frames();
        let expected = tc(1, 0, 15, 0).to_frames() - tc(1, 0, 0, 0).to_frames();
        assert_eq!(span, expected);
    }

    #[test]
    fn test_empty_events() {
        let config = EdlToTimelineConfig::default();
        let tl = convert_to_timeline(&[], None, &config).expect("conversion should succeed");
        assert_eq!(tl.total_clips(), 0);
        assert_eq!(tl.track_count(), 0);
        assert_eq!(tl.duration_frames(), 0);
    }

    #[test]
    fn test_convert_edl_helper() {
        let mut edl = crate::Edl::new(crate::EdlFormat::Cmx3600);
        edl.set_frame_rate(EdlFrameRate::Fps25);
        edl.set_title("Helper Test".to_string());

        let ev = make_cut(1, "A001", tc(1, 0, 0, 0), tc(1, 0, 5, 0));
        edl.add_event(ev).expect("add_event should succeed");

        let tl = convert_edl_to_timeline(&edl).expect("conversion should succeed");
        assert_eq!(tl.title.as_deref(), Some("Helper Test"));
        assert_eq!(tl.total_clips(), 1);
    }

    #[test]
    fn test_clip_name_propagated() {
        let mut ev = make_cut(1, "A001", tc(1, 0, 0, 0), tc(1, 0, 5, 0));
        ev.set_clip_name("interview.mov".to_string());

        let config = EdlToTimelineConfig::default();
        let tl = convert_to_timeline(&[ev], None, &config).expect("conversion should succeed");

        let track = tl.get_track("V1").expect("V1 should exist");
        assert_eq!(track.clips[0].clip_name.as_deref(), Some("interview.mov"));
    }
}