swift - AVAssetExportSession 给我 AVFoundationErrorDomain Code=-11800
问题描述
我在实际设备中的 ios 13.3 中面临同样的问题,它在 ios 13.2 模拟器中工作,但出现以下错误。
Error Domain=AVFoundationErrorDomain Code=-11800“操作无法完成” UserInfo={NSLocalizedFailureReason=发生未知错误(-17508),NSLocalizedDescription=操作无法完成,NSUnderlyingError=0x2816d11d0 {Error Domain=NSOSStatusErrorDomain Code=- 17508 “(空)”}}
这是我想将 .mov 文件转换为 mp4 的代码。
class func encodeVideo(at videoURL: String, completionHandler: ((URL?, Error?) -> Void)?) {
let avAsset = AVURLAsset(url: URL.init(fileURLWithPath: videoURL), options: nil)
let startDate = Date()
//Create Export session
guard let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) else {
completionHandler?(nil, nil)
return
}
//Creating temp path to save the converted video
let filename = "Video_\(Date().timeIntervalSince1970).mp4"
// Below Folder Path used tor getting directory path
let strfilePath = (FolderPath.temporaryDirectory.getDirectoryPath as NSString).appendingPathComponent(filename)
let filePath = URL.init(fileURLWithPath: strfilePath)
//Check if the file already exists then remove the previous file
if FileManager.default.fileExists(atPath: filePath.path) {
do {
try FileManager.default.removeItem(at: filePath)
} catch {
completionHandler?(nil, error)
}
}
exportSession.outputURL = filePath
exportSession.outputFileType = AVFileType.mp4
exportSession.shouldOptimizeForNetworkUse = true
let start = CMTimeMakeWithSeconds(0.0, preferredTimescale: 0)
let range = CMTimeRangeMake(start: start, duration: avAsset.duration)
exportSession.timeRange = range
exportSession.exportAsynchronously(completionHandler: {() -> Void in
switch exportSession.status {
case .failed:
print(exportSession.error ?? "NO ERROR")
completionHandler?(nil, exportSession.error)
case .cancelled:
print("Export canceled")
completionHandler?(nil, nil)
case .completed:
//Video conversion finished
let endDate = Date()
let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful!")
print(exportSession.outputURL ?? "NO OUTPUT URL")
completionHandler?(exportSession.outputURL, nil)
default: break
}
})
}
解决方案
最后,我通过使用 AVMutableComposition 而不是直接使用 AVURL 资产来解决我的问题。我在 AVMutableComposition 中添加音频和视频轨道。
推荐阅读
- azure - 如何访问 Azure 管道中的环境变量?
- python - 是否可以在 Google TPU 上运行常规 python 代码?
- algorithm - 类似的代码,相同的功能,不知道它们有什么区别
- laravel - Laravel:如何在表单请求输入中记录错误
- javascript - 如何在 JS 变量中转义冒号?
- javascript - 如何在 JavaScript 中将数组转换为对象
- powershell - 使用 Firefox 截屏
- javascript - 如何在不改变原始数组的情况下过滤嵌套数组?
- sql - Where 子句中的聚合问题,当 Max(x) != 3 时选择 Max(x)
- glsl - 如何从着色器中取出一个圆圈?