PulseCanvas / app / visionOS / VisionVideoExportView.swift
| 1 | import SwiftUI |
| 2 | import UniformTypeIdentifiers |
| 3 | |
| 4 | struct VisionVideoExportView: View { |
| 5 | @ObservedObject var studio: VisionStudio |
| 6 | let close: () -> Void |
| 7 | @StateObject private var exporter = SocialExportController() |
| 8 | @State private var saveRequest: VisionVideoSaveRequest? |
| 9 | @State private var readyURL: URL? |
| 10 | @State private var saved = false |
| 11 | @State private var saveError: String? |
| 12 | var body: some View { |
| 13 | VStack(alignment: .leading, spacing: 22) { |
| 14 | Label("Export video", systemImage: "film").font(.title2.bold()) |
| 15 | if !exporter.isExporting && readyURL == nil { |
| 16 | Picker("Canvas", selection: $exporter.aspect) { |
| 17 | ForEach(SocialAspect.allCases) { Text($0.title).tag($0) } |
| 18 | } |
| 19 | Picker("Resolution", selection: $exporter.resolution) { |
| 20 | Text("720p").tag(720); Text("1080p").tag(1080); Text("4K").tag(2160) |
| 21 | }.pickerStyle(.segmented) |
| 22 | Picker("Frame rate", selection: $exporter.fps) { |
| 23 | ForEach([24, 30, 60], id: \.self) { Text("\($0) fps").tag($0) } |
| 24 | }.pickerStyle(.segmented) |
| 25 | let size = exporter.aspect.dimensions(resolution: exporter.resolution) |
| 26 | Text("\(size.width) × \(size.height) · Full soundtrack included") |
| 27 | .foregroundStyle(.secondary) |
| 28 | } |
| 29 | if exporter.isExporting || readyURL != nil { |
| 30 | if let image = exporter.framePreview { |
| 31 | Image(decorative: image, scale: 1).resizable().scaledToFit() |
| 32 | .frame(maxWidth: .infinity, maxHeight: 250).clipShape(RoundedRectangle(cornerRadius: 16)) |
| 33 | } |
| 34 | ProgressView(value: readyURL == nil ? exporter.progress : 1) |
| 35 | Text(saved ? "Video saved" : readyURL != nil ? "Video ready to save" : exporter.status).font(.headline) |
| 36 | if let remaining = exporter.estimatedSecondsRemaining { |
| 37 | Text("About \(Duration.seconds(remaining).formatted(.time(pattern: .minuteSecond))) remaining") |
| 38 | .foregroundStyle(.secondary).monospacedDigit() |
| 39 | } |
| 40 | } |
| 41 | if let saveError { Text(saveError).foregroundStyle(.red) } |
| 42 | if !exporter.isExporting && exporter.exportedURL == nil && exporter.didFail { |
| 43 | Text(exporter.status).foregroundStyle(.red) |
| 44 | } |
| 45 | HStack { |
| 46 | Button(exporter.isExporting ? "Cancel export" : "Done") { |
| 47 | if exporter.isExporting { exporter.cancel() } else { close() } |
| 48 | } |
| 49 | Spacer() |
| 50 | if let readyURL { |
| 51 | Button("Render another video") { self.readyURL = nil; saved = false } |
| 52 | Button(saved ? "Save another copy" : "Save video", systemImage: "tray.and.arrow.down") { |
| 53 | do { saveRequest = try VisionVideoSaveRequest(url: readyURL) } |
| 54 | catch { saveError = "The rendered video is unavailable: \(error.localizedDescription)" } |
| 55 | }.buttonStyle(.borderedProminent) |
| 56 | } else if !exporter.isExporting { |
| 57 | Button("Render video") { startExport() }.buttonStyle(.borderedProminent) |
| 58 | .disabled(studio.audioURL == nil) |
| 59 | } |
| 60 | } |
| 61 | }.padding(32).frame(width: 660, height: 570) |
| 62 | .interactiveDismissDisabled(exporter.isExporting) |
| 63 | .sheet(item: $saveRequest) { request in |
| 64 | VisionVideoSavePicker(request: request) { result in |
| 65 | saveRequest = nil |
| 66 | if let result { |
| 67 | switch result { |
| 68 | case .success: saved = true; saveError = nil |
| 69 | case .failure(let error): saveError = "Couldn't save video: \(error.localizedDescription)" |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | .onAppear { |
| 75 | readyURL = VisionVideoFile.recoverPreviousRender() |
| 76 | exporter.aspect = studio.project.exportAspect.flatMap(SocialAspect.init(rawValue:)) ?? .wide |
| 77 | exporter.resolution = studio.project.exportResolution ?? 1080 |
| 78 | exporter.fps = min(60, studio.project.exportFPS ?? 60) |
| 79 | } |
| 80 | } |
| 81 | private func startExport() { |
| 82 | studio.suspend() |
| 83 | studio.saveExportFormat(aspect: exporter.aspect.rawValue, resolution: exporter.resolution, fps: exporter.fps) |
| 84 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent("Vision-video-\(UUID().uuidString)") |
| 85 | do { try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) } |
| 86 | catch { saveError = error.localizedDescription; return } |
| 87 | saveError = nil; saved = false; readyURL = nil |
| 88 | let audio = studio.audioURL |
| 89 | let cuts = studio.segments |
| 90 | let looks = studio.project.looks.mapValues(\.preset) |
| 91 | Task { |
| 92 | var keepCompletedFile = false |
| 93 | defer { if !keepCompletedFile { try? FileManager.default.removeItem(at: folder) } } |
| 94 | await exporter.export(audioURL: audio, render: studio.render, |
| 95 | to: folder.appendingPathComponent("PulseCanvas Video.mp4"), segments: cuts, appearances: looks) |
| 96 | if let url = exporter.exportedURL { |
| 97 | do { readyURL = try VisionVideoFile.retain(url) } |
| 98 | catch { |
| 99 | readyURL = url; keepCompletedFile = true |
| 100 | saveError = "The video can be saved, but couldn't be cached for later: \(error.localizedDescription)" |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |