AVAssetExportSession 在 IOS 13 上失败,将音频和视频混合在一起

AVAssetExportSession fails on IOS 13, muxing together audio and video

此代码在所有 IOS 13 之前的设备上有效(并且仍然有效)。目前,在 exportAsynchronously 调用运行后出现此错误:

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

不确定 IOS 13 adds/changes AVAssetExportSession 对象的基本设置中的一些要求还是什么?可能是 IOS 错误?


代码如下:

func compileAudioAndVideoToMovie(audioInputURL:URL, videoInputURL:URL) {   
        let docPath:String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];   
        let videoOutputURL:URL = URL(fileURLWithPath: docPath).appendingPathComponent("video.mp4");   
        do   
        {   
            try FileManager.default.removeItem(at: videoOutputURL);   
        }   
        catch {}   
        let mixComposition = AVMutableComposition();   
        let videoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid);   
        let videoInputAsset = AVURLAsset(url: videoInputURL);   
        let audioTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid);   
        let audioInputAsset = AVURLAsset(url: audioInputURL);   
        do   
        {   
            try videoTrack?.insertTimeRange(CMTimeRangeMake(start: CMTimeMake(value: 0, timescale: 1000), duration: CMTimeMake(value: 3000, timescale: 1000)), of: videoInputAsset.tracks(withMediaType: AVMediaType.video)[0], at: CMTimeMake(value: 0, timescale: 1000));// Insert an 3-second video clip into the video track   
            try audioTrack?.insertTimeRange(CMTimeRangeMake(start: CMTimeMake(value: 0, timescale: 1000), duration: CMTimeMake(value: 3000, timescale: 1000)), of: audioInputAsset.tracks(withMediaType: AVMediaType.audio)[0], at: CMTimeMake(value: 0, timescale: 1000));// Insert an 3-second audio clip into the audio track   

            let assetExporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetPassthrough);   
            assetExporter?.outputFileType = AVFileType.mp4;   
            assetExporter?.outputURL = videoOutputURL;   
            assetExporter?.shouldOptimizeForNetworkUse = false;   
            assetExporter?.exportAsynchronously {   
                switch (assetExporter?.status)   
                {   
                case .cancelled:   
                    print("Exporting cancelled");   
                case .completed:   
                    print("Exporting completed");   
                case .exporting:   
                    print("Exporting ...");   
                case .failed:   
                    print("Exporting failed");   
                default:   
                    print("Exporting with other result");   
                }   
                if let error = assetExporter?.error   
                {   
                    print("Error:\n\(error)");   
                }   
            }   
        }   
        catch   
        {   
            print("Exception when compiling movie");   
        }   
    }   

问题似乎与 AVAssetExportPresetPassthrough(以及处理 AAC 的组合,也许)有关

更改为 AVAssetExportPresetLowQuality 或 AVAssetExportPresetHighestQuality 并且 video/audio 已正确合二为一。同样,这只是一个 IOS 13 问题,很可能是一个错误。