oximedia-conform 0.2.1

Professional media conforming system for OxiMedia - EDL/XML/AAF timeline reconstruction
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
//! Common types for oximedia-conform.

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Frame rate representation.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum FrameRate {
    /// 23.976 fps (24000/1001).
    Fps23976,
    /// 24 fps.
    Fps24,
    /// 25 fps (PAL).
    Fps25,
    /// 29.97 fps (30000/1001) drop-frame.
    Fps2997DF,
    /// 29.97 fps (30000/1001) non-drop-frame.
    Fps2997NDF,
    /// 30 fps.
    Fps30,
    /// 50 fps.
    Fps50,
    /// 59.94 fps (60000/1001).
    Fps5994,
    /// 60 fps.
    Fps60,
    /// Custom frame rate.
    Custom(f64),
}

impl FrameRate {
    /// Get the frame rate as a floating-point number.
    #[must_use]
    pub fn as_f64(self) -> f64 {
        match self {
            Self::Fps23976 => 24000.0 / 1001.0,
            Self::Fps24 => 24.0,
            Self::Fps25 => 25.0,
            Self::Fps2997DF | Self::Fps2997NDF => 30000.0 / 1001.0,
            Self::Fps30 => 30.0,
            Self::Fps50 => 50.0,
            Self::Fps5994 => 60000.0 / 1001.0,
            Self::Fps60 => 60.0,
            Self::Custom(fps) => fps,
        }
    }

    /// Get the frame rate as a rational (numerator, denominator).
    #[must_use]
    pub const fn as_rational(self) -> (u32, u32) {
        match self {
            Self::Fps23976 => (24000, 1001),
            Self::Fps24 => (24, 1),
            Self::Fps25 => (25, 1),
            Self::Fps2997DF | Self::Fps2997NDF => (30000, 1001),
            Self::Fps30 => (30, 1),
            Self::Fps50 => (50, 1),
            Self::Fps5994 => (60000, 1001),
            Self::Fps60 => (60, 1),
            Self::Custom(_) => (0, 0), // Not representable
        }
    }

    /// Check if this is a drop-frame rate.
    #[must_use]
    pub const fn is_drop_frame(self) -> bool {
        matches!(self, Self::Fps2997DF)
    }
}

/// Timecode representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Timecode {
    /// Hours (0-23).
    pub hours: u8,
    /// Minutes (0-59).
    pub minutes: u8,
    /// Seconds (0-59).
    pub seconds: u8,
    /// Frames (0-fps-1).
    pub frames: u8,
}

impl Timecode {
    /// Create a new timecode.
    #[must_use]
    pub const fn new(hours: u8, minutes: u8, seconds: u8, frames: u8) -> Self {
        Self {
            hours,
            minutes,
            seconds,
            frames,
        }
    }

    /// Convert to total frames.
    #[must_use]
    pub fn to_frames(self, fps: FrameRate) -> u64 {
        let fps_int = fps.as_f64() as u64;
        let hours_frames = u64::from(self.hours) * 3600 * fps_int;
        let minutes_frames = u64::from(self.minutes) * 60 * fps_int;
        let seconds_frames = u64::from(self.seconds) * fps_int;
        hours_frames + minutes_frames + seconds_frames + u64::from(self.frames)
    }

    /// Create from total frames.
    #[must_use]
    pub fn from_frames(frames: u64, fps: FrameRate) -> Self {
        let fps_int = fps.as_f64() as u64;
        let hours = (frames / (3600 * fps_int)) as u8;
        let remaining = frames % (3600 * fps_int);
        let minutes = (remaining / (60 * fps_int)) as u8;
        let remaining = remaining % (60 * fps_int);
        let seconds = (remaining / fps_int) as u8;
        let frames = (remaining % fps_int) as u8;
        Self::new(hours, minutes, seconds, frames)
    }

    /// Parse from string in format "HH:MM:SS:FF".
    ///
    /// # Errors
    ///
    /// Returns an error if the format is invalid.
    pub fn parse(s: &str) -> Result<Self, String> {
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() != 4 {
            return Err(format!("Invalid timecode format: {s}"));
        }

        let hours = parts[0]
            .parse::<u8>()
            .map_err(|_| format!("Invalid hours: {}", parts[0]))?;
        let minutes = parts[1]
            .parse::<u8>()
            .map_err(|_| format!("Invalid minutes: {}", parts[1]))?;
        let seconds = parts[2]
            .parse::<u8>()
            .map_err(|_| format!("Invalid seconds: {}", parts[2]))?;
        let frames = parts[3]
            .parse::<u8>()
            .map_err(|_| format!("Invalid frames: {}", parts[3]))?;

        Ok(Self::new(hours, minutes, seconds, frames))
    }
}

impl std::fmt::Display for Timecode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{:02}:{:02}:{:02}:{:02}",
            self.hours, self.minutes, self.seconds, self.frames
        )
    }
}

/// Media file reference.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MediaFile {
    /// Unique identifier.
    pub id: uuid::Uuid,
    /// File path.
    pub path: PathBuf,
    /// File name.
    pub filename: String,
    /// Duration in seconds.
    pub duration: Option<f64>,
    /// Start timecode.
    pub timecode_start: Option<Timecode>,
    /// Width in pixels.
    pub width: Option<u32>,
    /// Height in pixels.
    pub height: Option<u32>,
    /// Frame rate.
    pub fps: Option<FrameRate>,
    /// File size in bytes.
    pub size: Option<u64>,
    /// MD5 checksum.
    pub md5: Option<String>,
    /// `XXHash` checksum.
    pub xxhash: Option<String>,
    /// Additional metadata (JSON).
    pub metadata: Option<String>,
    /// When the file was cataloged.
    pub cataloged_at: chrono::DateTime<chrono::Utc>,
}

impl MediaFile {
    /// Create a new media file reference.
    #[must_use]
    pub fn new(path: PathBuf) -> Self {
        let filename = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("")
            .to_string();

        Self {
            id: uuid::Uuid::new_v4(),
            path,
            filename,
            duration: None,
            timecode_start: None,
            width: None,
            height: None,
            fps: None,
            size: None,
            md5: None,
            xxhash: None,
            metadata: None,
            cataloged_at: chrono::Utc::now(),
        }
    }
}

/// Clip reference from EDL/XML.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClipReference {
    /// Clip identifier (reel name, clip name, etc.).
    pub id: String,
    /// Source file name or pattern.
    pub source_file: Option<String>,
    /// Source in timecode.
    pub source_in: Timecode,
    /// Source out timecode.
    pub source_out: Timecode,
    /// Record in timecode.
    pub record_in: Timecode,
    /// Record out timecode.
    pub record_out: Timecode,
    /// Track type (video, audio).
    pub track: TrackType,
    /// Frame rate.
    pub fps: FrameRate,
    /// Additional metadata.
    pub metadata: std::collections::HashMap<String, String>,
}

/// Track type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TrackType {
    /// Video track.
    Video,
    /// Audio track.
    Audio,
    /// Both audio and video.
    AudioVideo,
}

/// Clip match result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClipMatch {
    /// The clip reference being matched.
    pub clip: ClipReference,
    /// The matched media file.
    pub media: MediaFile,
    /// Match score (0.0 - 1.0).
    pub score: f64,
    /// Match method used.
    pub method: MatchMethod,
    /// Additional match details.
    pub details: String,
}

/// Match method.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MatchMethod {
    /// Exact filename match.
    ExactFilename,
    /// Fuzzy filename match.
    FuzzyFilename,
    /// Timecode match.
    Timecode,
    /// Content hash match.
    ContentHash,
    /// Duration match.
    Duration,
    /// Combined match (multiple methods).
    Combined,
}

impl std::fmt::Display for MatchMethod {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::ExactFilename => "Exact Filename",
            Self::FuzzyFilename => "Fuzzy Filename",
            Self::Timecode => "Timecode",
            Self::ContentHash => "Content Hash",
            Self::Duration => "Duration",
            Self::Combined => "Combined",
        };
        write!(f, "{s}")
    }
}

/// A clip parsed from an NLE project file (FCPXML, AAF, etc.).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConformClip {
    /// Clip name.
    pub name: String,
    /// Offset in the timeline (seconds from timeline start).
    pub offset_s: f64,
    /// Duration of the clip in the timeline (seconds).
    pub duration_s: f64,
    /// Source in-point (seconds from source start).
    pub src_in_s: f64,
    /// Source out-point (seconds from source start).
    pub src_out_s: f64,
}

/// A sequence (timeline) within an NLE project.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConformSequence {
    /// Sequence name.
    pub name: String,
    /// Frame rate of the sequence.
    pub frame_rate: f32,
    /// Ordered list of clips in the sequence.
    pub clips: Vec<ConformClip>,
}

/// A project parsed from an NLE project file.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConformProject {
    /// Project name.
    pub name: String,
    /// Sequences / timelines in this project.
    pub sequences: Vec<ConformSequence>,
}

/// Output format for conformed sequence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OutputFormat {
    /// Concatenated MP4 file.
    Mp4,
    /// Matroska container.
    Matroska,
    /// Frame sequence (DPX).
    FrameSequenceDpx,
    /// Frame sequence (TIFF).
    FrameSequenceTiff,
    /// Frame sequence (PNG).
    FrameSequencePng,
    /// EDL with updated paths.
    Edl,
    /// Final Cut Pro XML.
    FcpXml,
    /// Adobe Premiere XML.
    PremiereXml,
    /// AAF file.
    Aaf,
}

impl std::fmt::Display for OutputFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Mp4 => "MP4",
            Self::Matroska => "Matroska",
            Self::FrameSequenceDpx => "DPX Sequence",
            Self::FrameSequenceTiff => "TIFF Sequence",
            Self::FrameSequencePng => "PNG Sequence",
            Self::Edl => "EDL",
            Self::FcpXml => "FCP XML",
            Self::PremiereXml => "Premiere XML",
            Self::Aaf => "AAF",
        };
        write!(f, "{s}")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_frame_rate_conversion() {
        assert!((FrameRate::Fps25.as_f64() - 25.0).abs() < f64::EPSILON);
        assert!((FrameRate::Fps2997NDF.as_f64() - 29.97).abs() < 0.01);
    }

    #[test]
    fn test_timecode_frames_conversion() {
        let tc = Timecode::new(1, 0, 0, 0);
        let frames = tc.to_frames(FrameRate::Fps25);
        assert_eq!(frames, 90000); // 1 hour * 3600 seconds * 25 fps

        let tc2 = Timecode::from_frames(frames, FrameRate::Fps25);
        assert_eq!(tc, tc2);
    }

    #[test]
    fn test_timecode_parsing() {
        let tc = Timecode::parse("01:23:45:12").expect("tc should be valid");
        assert_eq!(tc.hours, 1);
        assert_eq!(tc.minutes, 23);
        assert_eq!(tc.seconds, 45);
        assert_eq!(tc.frames, 12);
    }

    #[test]
    fn test_timecode_display() {
        let tc = Timecode::new(1, 23, 45, 12);
        assert_eq!(tc.to_string(), "01:23:45:12");
    }

    #[test]
    fn test_timecode_comparison() {
        let tc1 = Timecode::new(1, 0, 0, 0);
        let tc2 = Timecode::new(1, 0, 0, 1);
        assert!(tc1 < tc2);
    }
}