Skip to content

Commit bf06df2

Browse files
committed
Fix silent failure causing audio-only exports (Issue #38)
Previously, when video output configuration was incompatible with the asset, setupVideoOutput would silently return and the export would continue with only audio tracks. This caused intermittent reports of videos exporting without video content. Changes: - Add validation to detect when video setup fails for assets with video tracks - Fail fast with clear error: "Video output configuration is not compatible" - Track setup errors and surface them through completion handler - Document issue and common causes in README troubleshooting section - Remove unused Photos import Fixes #38
1 parent 39a5e2e commit bf06df2

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

Sources/NextLevelSessionExporter.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ open class NextLevelSessionExporter: NSObject, @unchecked Sendable {
167167
fileprivate var _duration: TimeInterval = 0
168168
fileprivate var _lastSamplePresentationTime: CMTime = .invalid
169169

170+
fileprivate var _videoSetupFailed: Bool = false
171+
fileprivate var _videoSetupError: Error?
172+
170173
// MARK: - object lifecycle
171174

172175
/// Initializes a session with an asset to export.
@@ -253,6 +256,8 @@ extension NextLevelSessionExporter {
253256
}
254257

255258
self._progress = 0
259+
self._videoSetupFailed = false
260+
self._videoSetupError = nil
256261

257262
do {
258263
self._reader = try AVAssetReader(asset: asset)
@@ -306,6 +311,17 @@ extension NextLevelSessionExporter {
306311
self.setupAudioOutput(withAsset: asset)
307312
self.setupAudioInput()
308313

314+
// Validate that video setup succeeded if video tracks exist (Issue #38)
315+
let videoTracks = asset.tracks(withMediaType: AVMediaType.video)
316+
if videoTracks.count > 0 && self._videoSetupFailed {
317+
print("NextLevelSessionExporter, video tracks exist but video setup failed - aborting export")
318+
DispatchQueue.main.async {
319+
let error = self._videoSetupError ?? NextLevelSessionExporterError.setupFailure("Video output could not be configured")
320+
self._completionHandler?(.failure(error))
321+
}
322+
return
323+
}
324+
309325
// export
310326

311327
self._writer?.startWriting()
@@ -314,7 +330,7 @@ extension NextLevelSessionExporter {
314330

315331
let dispatchGroup = DispatchGroup()
316332

317-
let videoTracks = asset.tracks(withMediaType: AVMediaType.video)
333+
// Reuse videoTracks from validation above (Issue #38)
318334
if let videoInput = self._videoInput,
319335
let videoOutput = self._videoOutput,
320336
videoTracks.count > 0 {
@@ -395,7 +411,10 @@ extension NextLevelSessionExporter {
395411
self._videoInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: self.videoOutputConfiguration)
396412
self._videoInput?.expectsMediaDataInRealTime = self.expectsMediaDataInRealTime
397413
} else {
398-
print("Unsupported output configuration")
414+
// Mark video setup as failed - this will cause the export to fail with a clear error
415+
self._videoSetupFailed = true
416+
self._videoSetupError = NextLevelSessionExporterError.invalidConfiguration("Video output configuration is not compatible with this asset. The writer cannot apply the specified video settings.")
417+
print("NextLevelSessionExporter, video output configuration is not supported by the writer")
399418
return
400419
}
401420

@@ -740,6 +759,9 @@ extension NextLevelSessionExporter {
740759
self._progressHandler = nil
741760
self._renderHandler = nil
742761
self._completionHandler = nil
762+
763+
self._videoSetupFailed = false
764+
self._videoSetupError = nil
743765
}
744766

745767
}

0 commit comments

Comments
 (0)