EDLExporter.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import Foundation
  2. /// Exports a playlist as a CMX 3600 Edit Decision List (EDL).
  3. /// EDLs are widely used in professional audio/video production
  4. /// and can be imported by Pro Tools, Audition, Resolve, and many others.
  5. struct EDLExporter: DAWExporter {
  6. static let formatID = "edl"
  7. static let formatName = "Edit Decision List"
  8. static let fileExtension = "edl"
  9. static func export(playlist: Playlist, to url: URL, options: ExportOptions) throws {
  10. let entries = playlist.sortedEntries
  11. let sampleRate = options.targetSampleRate ?? 44100
  12. let fps = 30.0 // Standard frame rate for timecode
  13. var lines: [String] = []
  14. // EDL header
  15. lines.append("TITLE: \(playlist.name)")
  16. lines.append("FCM: NON-DROP FRAME")
  17. lines.append("")
  18. var timelinePosition: TimeInterval = 0
  19. for (index, entry) in entries.enumerated() {
  20. guard let track = entry.track, track.hasLocalFile else { continue }
  21. let editNumber = String(format: "%03d", index + 1)
  22. let reelName = sanitizeReelName(track.title)
  23. let startOffset = entry.startOffset
  24. let effectiveDuration = entry.effectiveDuration
  25. // Source IN/OUT
  26. let sourceIn = formatTimecode(startOffset, fps: fps)
  27. let sourceOut = formatTimecode(startOffset + effectiveDuration, fps: fps)
  28. // Record IN/OUT (timeline position)
  29. let recordIn = formatTimecode(timelinePosition, fps: fps)
  30. let recordOut = formatTimecode(timelinePosition + effectiveDuration, fps: fps)
  31. // Transition type
  32. let transition: String
  33. if entry.crossfadeDuration > 0 {
  34. let crossfadeFrames = Int(entry.crossfadeDuration * fps)
  35. transition = "D \(String(format: "%03d", crossfadeFrames))" // Dissolve
  36. } else {
  37. transition = "C" // Cut
  38. }
  39. // EDL edit entry: EDIT# REEL TRACK TRANSITION SOURCE_IN SOURCE_OUT RECORD_IN RECORD_OUT
  40. lines.append("\(editNumber) \(reelName) AA/V \(transition) \(sourceIn) \(sourceOut) \(recordIn) \(recordOut)")
  41. // Source file comment
  42. lines.append("* FROM CLIP NAME: \(track.title)")
  43. let relativePath = "\(options.audioFilesRelativePath)/\(track.fileURL.lastPathComponent)"
  44. lines.append("* SOURCE FILE: \(relativePath)")
  45. if !track.artist.isEmpty {
  46. lines.append("* ARTIST: \(track.artist)")
  47. }
  48. if let bpm = track.bpm {
  49. lines.append("* BPM: \(String(format: "%.1f", bpm))")
  50. }
  51. if let key = track.musicalKey {
  52. lines.append("* KEY: \(key)")
  53. }
  54. // Gain adjustment
  55. if entry.gainAdjustment != 0 {
  56. lines.append("* GAIN: \(String(format: "%.1f", entry.gainAdjustment)) dB")
  57. }
  58. lines.append("")
  59. // Add cue point markers
  60. if options.includeCuePoints {
  61. for cuePoint in track.cuePoints.sorted() {
  62. let markerTime = formatTimecode(
  63. timelinePosition + cuePoint.timestamp - startOffset,
  64. fps: fps
  65. )
  66. let name = cuePoint.name.isEmpty ? cuePoint.type.rawValue : cuePoint.name
  67. lines.append("* MARKER: \(markerTime) \(name)")
  68. }
  69. if !track.cuePoints.isEmpty {
  70. lines.append("")
  71. }
  72. }
  73. timelinePosition += effectiveDuration
  74. if index + 1 < entries.count {
  75. timelinePosition -= entries[index + 1].crossfadeDuration
  76. }
  77. }
  78. let content = lines.joined(separator: "\n") + "\n"
  79. try content.write(to: url, atomically: true, encoding: .utf8)
  80. }
  81. // MARK: - Timecode Formatting
  82. /// Format seconds as SMPTE timecode HH:MM:SS:FF.
  83. private static func formatTimecode(_ seconds: TimeInterval, fps: Double) -> String {
  84. let totalSeconds = max(0, seconds)
  85. let hours = Int(totalSeconds) / 3600
  86. let minutes = (Int(totalSeconds) % 3600) / 60
  87. let secs = Int(totalSeconds) % 60
  88. let frames = Int((totalSeconds - Double(Int(totalSeconds))) * fps)
  89. return String(format: "%02d:%02d:%02d:%02d", hours, minutes, secs, frames)
  90. }
  91. /// Sanitize a track title to a valid reel name (max 8 chars, alphanumeric).
  92. private static func sanitizeReelName(_ name: String) -> String {
  93. let sanitized = name
  94. .components(separatedBy: CharacterSet.alphanumerics.inverted)
  95. .joined()
  96. let truncated = String(sanitized.prefix(8))
  97. return truncated.isEmpty ? "REEL001" : truncated.uppercased().padding(toLength: 8, withPad: " ", startingAt: 0)
  98. }
  99. }