3

This code works (and still does) on all pre-IOS 13 devices. Currently howerver, am getting this error after the exportAsynchronously call runs:

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)"}}

Unsure if IOS 13 adds/changes some requirements in the basic setup of the AVAssetExportSession object or what? Could be an IOS bug?


Here is the code:

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");   
        }   
    }   
yesbutmaybeno
  • 1,078
  • 13
  • 31
  • https://stackoverflow.com/questions/58136254/avassetreferencerestrictions-on-ios-13 It could be about AVURLAsset initialization, you may want to try with `AVURLAsset(url: videoInputURL, options: nil)` – EDUsta Oct 07 '19 at 18:48
  • @EDUsta hm, that sounded really good, thanks, but I've just now added `options: nil` to the above code when initializing the AVURLAsset objects as that answer suggests, but same result unfortunately – yesbutmaybeno Oct 07 '19 at 22:21
  • Any progress on this bug? – Kerem Kusmezer Oct 22 '19 at 23:18
  • 1
    @KeremKusmezer My reply/answer below was the solution to this – yesbutmaybeno Oct 24 '19 at 00:12

1 Answers1

0

Issue seems to be related to AVAssetExportPresetPassthrough (and a combination of dealing with an AAC, maybe)

Changing to AVAssetExportPresetLowQuality or AVAssetExportPresetHighestQuality and the video/audio are properly muxed into one. Again, this is just an IOS 13 issue, and likely a bug.

yesbutmaybeno
  • 1,078
  • 13
  • 31