首页 > 解决方案 > 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  
        }  

    })  
}  

标签: swiftavfoundationios13avassetexportsession

解决方案


最后,我通过使用 AVMutableComposition 而不是直接使用 AVURL 资产来解决我的问题。我在 AVMutableComposition 中添加音频和视频轨道。


推荐阅读