forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

edl.rs104 lines, 3.7 KiBHistoryRawDownload
1//! CMX3600 EDL, the classic cut list, written for any editor that reads one.
2//! Video is V1 only, audio is A1 (`A`) and A2 (`A2`), a dissolve is `D` with
3//! its length, and every event is followed by its `FROM CLIP NAME`. Markers
4//! are `LOC` comments. Timecode counts whole frames at the rounded rate and
5//! the record starts at 01:00:00:00. Speed changes and anything that is not a
6//! cut or a dissolve are not written.
7
8use std::fmt::Write as _;
9use std::path::{Path, PathBuf};
10
11use super::{Item, Timeline};
12use crate::project::LaneKind;
13
14/// `HH:MM:SS:FF` for `frames` at `nf` whole frames a second.
15fn timecode(frames: u32, nf: u32) -> String {
16 let f = frames % nf;
17 let s = frames / nf;
18 format!("{:02}:{:02}:{:02}:{:02}", s / 3600 % 100, s / 60 % 60, s % 60, f)
19}
20
21/// A reel name: the file's stem in capitals and digits, eight at most, unique in the list.
22fn reel_for(reels: &mut Vec<(PathBuf, String)>, path: &Path) -> String {
23 if let Some((_, r)) = reels.iter().find(|(p, _)| p == path) {
24 return r.clone();
25 }
26 let stem: String = path
27 .file_stem()
28 .map(|s| {
29 s.to_string_lossy()
30 .chars()
31 .filter(|c| c.is_ascii_alphanumeric())
32 .map(|c| c.to_ascii_uppercase())
33 .take(8)
34 .collect()
35 })
36 .unwrap_or_default();
37 let base = if stem.is_empty() { "BL".to_string() } else { stem };
38 let mut name = base.clone();
39 let mut n = 1;
40 while reels.iter().any(|(_, r)| *r == name) {
41 name = format!("{}{n:02}", base.chars().take(6).collect::<String>());
42 n += 1;
43 }
44 reels.push((path.to_path_buf(), name.clone()));
45 name
46}
47
48/// The CMX3600 EDL for `t`.
49pub fn write(t: &Timeline) -> String {
50 let nf = t.fps.round().max(1.0) as u32;
51 let base = 3600 * nf;
52 let rec = |frame: u32| timecode(frame + base, nf);
53 let mut reels: Vec<(PathBuf, String)> = Vec::new();
54 // (record start, track order, event line, clip name) for every event, sorted below.
55 let mut events: Vec<(u32, usize, String, String)> = Vec::new();
56
57 for (order, track) in t.tracks.iter().enumerate() {
58 let chan = match (track.kind, track.ord) {
59 (LaneKind::Audio, 0) => "A".to_string(),
60 (LaneKind::Audio, 1) => "A2".to_string(),
61 (LaneKind::Audio, _) => continue,
62 (_, 0) => "V".to_string(),
63 _ => continue,
64 };
65 let mut pending: Option<u32> = None;
66 for item in &track.items {
67 let c = match item {
68 Item::Gap(_) => {
69 pending = None;
70 continue;
71 }
72 Item::Clip(c) => c,
73 };
74 let reel = reel_for(&mut reels, &c.path);
75 let edit = match pending.take() {
76 Some(n) => format!("D {n:03}"),
77 None => "C".to_string(),
78 };
79 let line = format!(
80 "{:<8} {:<5} {:<8} {} {} {} {}",
81 reel,
82 chan,
83 edit,
84 timecode(c.in_frame, nf),
85 timecode(c.out_frame, nf),
86 rec(c.start),
87 rec(c.start + c.frames),
88 );
89 events.push((c.start, order, line, c.file_name()));
90 pending = c.transition.map(|(_, n)| n);
91 }
92 }
93 events.sort_by_key(|e| (e.0, e.1));
94
95 let mut out = format!("TITLE: {}\nFCM: NON-DROP FRAME\n\n", t.name);
96 for (i, (_, _, line, name)) in events.iter().enumerate() {
97 let _ = writeln!(out, "{:03} {line}", i + 1);
98 let _ = writeln!(out, "* FROM CLIP NAME: {name}");
99 }
100 for m in &t.markers {
101 let _ = writeln!(out, "* LOC: {} {:<6} {}", rec(m.frame), m.color.name().to_uppercase(), m.name);
102 }
103 out
104}