| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::fmt::Write as _; |
| 9 | use std::path::{Path, PathBuf}; |
| 10 | |
| 11 | use super::{Item, Timeline}; |
| 12 | use crate::project::LaneKind; |
| 13 | |
| 14 | |
| 15 | fn 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 | |
| 22 | fn 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 | |
| 49 | pub 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 | |
| 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 | } |