使用 PHImageManager 导出视频文件 fails:AVFoundationErrorDomain Code=-11800 "The operation could not be completed" Code=-12212
Export of Video file using PHImageManager fails:AVFoundationErrorDomain Code=-11800 "The operation could not be completed" Code=-12212
我正在尝试使用 Photos iOS SDK 中的 PHImageManager 从模拟器上的相机胶卷导出视频资产。执行导出完成块时,会导致此错误:
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)"}}
每次模拟器相机胶卷上的指定视频都会发生这种情况。但是,这在真实设备上并不一致;有些视频可以正常导出,有些则不行。
为什么会发生这种情况,如何修复它,在哪里可以找到错误代码文档?
这是它发生的函数:
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)")
}
}
}
}
解法:
我发现错误代码“-12212”指的是 kVTColorCorrectionPixelTransferFailedErr。
所以我认为这一定是预设或质量设置问题...
我之前曾尝试设置 requestOptions.deliveryMode = .highQualityFormat
但那没有用。但是,我注意到以下调用具有质量预设:
PHImageManager.default().requestExportSession(
forVideo: phAsset, options: requestOptions,
exportPreset: AVAssetExportPreset1280x720
)
当我把它改成这样的时候:
PHImageManager.default().requestExportSession(
forVideo: phAsset, options: requestOptions,
exportPreset: AVAssetExportPresetHighestQuality
)
成功了!
我正在尝试使用 Photos iOS SDK 中的 PHImageManager 从模拟器上的相机胶卷导出视频资产。执行导出完成块时,会导致此错误:
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)"}}
每次模拟器相机胶卷上的指定视频都会发生这种情况。但是,这在真实设备上并不一致;有些视频可以正常导出,有些则不行。
为什么会发生这种情况,如何修复它,在哪里可以找到错误代码文档?
这是它发生的函数:
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)")
}
}
}
}
解法:
我发现错误代码“-12212”指的是 kVTColorCorrectionPixelTransferFailedErr。
所以我认为这一定是预设或质量设置问题...
我之前曾尝试设置 requestOptions.deliveryMode = .highQualityFormat
但那没有用。但是,我注意到以下调用具有质量预设:
PHImageManager.default().requestExportSession(
forVideo: phAsset, options: requestOptions,
exportPreset: AVAssetExportPreset1280x720
)
当我把它改成这样的时候:
PHImageManager.default().requestExportSession(
forVideo: phAsset, options: requestOptions,
exportPreset: AVAssetExportPresetHighestQuality
)
成功了!