AVAssetExportSession - An unknown error occurred (-16976)

When exporting using

AVAssetExportSession and calling exportAsynchronously

I get the error:

Error Domain=AVFoundationErrorDomain Code=-11800 \"The operation could not be completed\" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-16976), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x1c405dd60 {Error Domain=NSOSStatusErrorDomain Code=-16976 \"(null)\"}}"


My Code:


public typealias TrimPoints = [(CMTime, CMTime)]
    
    fileprivate static func verifyPresetForAsset(preset: String, asset: AVAsset) -> Bool {
        let compatiblePresets = AVAssetExportSession.exportPresets(compatibleWith: asset)
        let filteredPresets = compatiblePresets.filter { $0 == preset }
        return filteredPresets.count > 0 || preset == AVAssetExportPresetPassthrough
    }
    
    fileprivate static func removeFileAtURLIfExists(url: URL) {
        let fileManager = FileManager.default
        guard fileManager.fileExists(atPath: url.path) else { return }
        do {
            try fileManager.removeItem(at: url)
        }
        catch let error {
            print("TrimVideo - Couldn't remove existing destination file: \(String(describing: error))")
        }
        
    }

static public func trimVideo(sourceURL: URL, trimPoints: TrimPoints, completion: ((_ error: Error?, _ url: URL?) -> Void)?) {
        let destinationURL = URL(fileURLWithPath: NSHomeDirectory() + "/Documents/SplitVideo.mp4")
        
        guard sourceURL.isFileURL else { return }
        guard destinationURL.isFileURL else { return }
        
        let options = [
            AVURLAssetPreferPreciseDurationAndTimingKey: true
        ]
        
        let asset = AVURLAsset(url: sourceURL, options: options)
        let preferredPreset = AVAssetExportPresetPassthrough
        
        if  VideoProcessingUtils.verifyPresetForAsset(preset: preferredPreset, asset: asset) {
            
            let composition = AVMutableComposition()
            let videoCompTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: CMPersistentTrackID())
            let audioCompTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: CMPersistentTrackID())
            
            guard let assetVideoTrack: AVAssetTrack = asset.tracks(withMediaType: .video).first else { return }
            guard let assetAudioTrack: AVAssetTrack = asset.tracks(withMediaType: .audio).first else { return }
            
            var accumulatedTime = kCMTimeZero
            for (startTimeForCurrentSlice, endTimeForCurrentSlice) in trimPoints {
                let durationOfCurrentSlice = CMTimeSubtract(endTimeForCurrentSlice, startTimeForCurrentSlice)
                let timeRangeForCurrentSlice = CMTimeRangeMake(startTimeForCurrentSlice, durationOfCurrentSlice)
                
                do {
                    try videoCompTrack!.insertTimeRange(timeRangeForCurrentSlice, of: assetVideoTrack, at: accumulatedTime)
                    try audioCompTrack!.insertTimeRange(timeRangeForCurrentSlice, of: assetAudioTrack, at: accumulatedTime)
                    accumulatedTime = CMTimeAdd(accumulatedTime, durationOfCurrentSlice)
                }
                catch let compError {
                    print("TrimVideo: error during composition: \(compError)")
                    completion?(compError, nil)
                }
            }
            
            guard let exportSession = AVAssetExportSession(asset: composition, presetName: preferredPreset) else { return }
            
            exportSession.outputURL = destinationURL as URL
            exportSession.outputFileType = AVFileType.mp4
            exportSession.shouldOptimizeForNetworkUse = true
            
            removeFileAtURLIfExists(url: destinationURL as URL)
            
            exportSession.exportAsynchronously { () -> Void in
                switch exportSession.status {
                case AVAssetExportSessionStatus.completed:
                    print("success")
                    completion?(nil, destinationURL)
                case AVAssetExportSessionStatus.failed:
                    print("failed \(exportSession.error?.localizedDescription ?? "error nil")")
                    completion?(exportSession.error, nil)
                case AVAssetExportSessionStatus.cancelled:
                    print("cancelled \(exportSession.error?.localizedDescription ?? "error nil")")
                    completion?(exportSession.error, nil)
                default:
                    print("complete")
                    completion?(exportSession.error, nil)
                }
            }
        }
        else {
            print("TrimVideo - Could not find a suitable export preset for the input video")
            let error = NSError(domain: "co.igenerate.GeneratePhotobooth", code: -1, userInfo: nil)
            completion?(error, nil)
        }
    
    }


   


Any ideas what the Code=-16976 means ?

Post not yet marked as solved Up vote post of omarojo Down vote post of omarojo
1.5k views