M3UExporter.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Foundation
  2. /// Exports a playlist as an M3U/M3U8 playlist file.
  3. /// M3U is a simple, widely-supported playlist format.
  4. struct M3UExporter: DAWExporter {
  5. static let formatID = "m3u"
  6. static let formatName = "M3U Playlist"
  7. static let fileExtension = "m3u"
  8. static func export(playlist: Playlist, to url: URL, options: ExportOptions) throws {
  9. let entries = playlist.sortedEntries
  10. var lines: [String] = []
  11. // Extended M3U header
  12. lines.append("#EXTM3U")
  13. lines.append("#PLAYLIST:\(playlist.name)")
  14. lines.append("")
  15. for entry in entries {
  16. guard let track = entry.track, track.hasLocalFile else { continue }
  17. let duration = Int(track.duration)
  18. let displayTitle: String
  19. if track.artist.isEmpty {
  20. displayTitle = track.title
  21. } else {
  22. displayTitle = "\(track.artist) - \(track.title)"
  23. }
  24. // EXTINF line: duration, artist - title
  25. lines.append("#EXTINF:\(duration),\(displayTitle)")
  26. // Additional metadata as comments
  27. if let bpm = track.bpm {
  28. lines.append("#EXTBPM:\(String(format: "%.1f", bpm))")
  29. }
  30. if let key = track.musicalKey {
  31. lines.append("#EXTKEY:\(key)")
  32. }
  33. // File path (relative if copying, absolute otherwise)
  34. if options.copyAudioFiles {
  35. lines.append("\(options.audioFilesRelativePath)/\(track.fileURL.lastPathComponent)")
  36. } else {
  37. lines.append(track.filePath)
  38. }
  39. lines.append("")
  40. }
  41. let content = lines.joined(separator: "\n")
  42. try content.write(to: url, atomically: true, encoding: .utf8)
  43. }
  44. }