I am trying to export a video asset from camera roll on a simulator using PHImageManager from the Photos iOS SDK. When the export completion block executes, it results in this error:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12212), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x600001baa4c0 {Error Domain=NSOSStatusErrorDomain Code=-12212 "(null)"}}
This happens every time with the specified video on the simulator's camera roll. However, this inconsistently occurs on a real device; some videos exporting fine, some not.
Why is this happening, how to fix it, and where can I find the error code documentation?
Here is the function it happens in:
public func exportVideoFile(
options: PHVideoRequestOptions? = nil,
outputURL: URL? = nil,
outputFileType: AVFileType = .mov,
progressBlock: ((Double) -> Void)? = nil,
completionBlock: @escaping ((URL, String) -> Void)
) {
guard
let phAsset = self.phAsset,
phAsset.mediaType == .video,
let writeURL = videoFilename(phAsset: phAsset),
let mimetype = MIMEType(writeURL)
else { return }
var requestOptions = PHVideoRequestOptions()
if let options = options {
requestOptions = options
} else {
requestOptions.isNetworkAccessAllowed = true
requestOptions.deliveryMode = .fastFormat
}
requestOptions.progressHandler = { progress, _, _, _ in
DispatchQueue.main.async {
debugPrint("progress", progress)
progressBlock?(progress)
}
}
PHImageManager.default().requestExportSession(
forVideo: phAsset, options: requestOptions,
exportPreset: AVAssetExportPreset1280x720
) { session, _ in
guard let session = session else { return }
session.outputURL = writeURL
session.outputFileType = outputFileType
session.shouldOptimizeForNetworkUse = true
session.exportAsynchronously {
completionBlock(writeURL, mimetype)
if let err = session.error { // Error happens here
debugPrint("Video Export Session Error: \(err.localizedDescription)")
} else {
debugPrint("Video Export Session Status: \(session.status)")
}
}
}
}
Solution:
I found out the error code "-12212" refers to kVTColorCorrectionPixelTransferFailedErr.
So I thought it's got to be a preset or quality setting issue...
I had previously tried setting requestOptions.deliveryMode = .highQualityFormat
but that did not work. However, I noticed the following call had a preset for quality:
PHImageManager.default().requestExportSession(
forVideo: phAsset, options: requestOptions,
exportPreset: AVAssetExportPreset1280x720
)
When I changed it to this:
PHImageManager.default().requestExportSession(
forVideo: phAsset, options: requestOptions,
exportPreset: AVAssetExportPresetHighestQuality
)
It worked!