Need help exporting audio with AVAssetExportSession

Viewed 1048

I am having trouble exporting an AVAsset to a local directory. I am getting the following error message:

failed Error Domain=AVFoundationErrorDomain Code=-11838 "Operation Stopped" UserInfo={NSLocalizedFailureReason=The operation is not supported for this media., NSLocalizedDescription=Operation Stopped, NSUnderlyingError=0x2806d1740 {Error Domain=NSOSStatusErrorDomain Code=-12109 "(null)"}}

This is my code to upload the file:

enter image description here

Here is an image of my debugger, where you can see that the file variable references an actual AVAsset

enter image description here

Here is a description of an AVAssetExportSession that is failing

Optional<AVAssetExportSession>
  - some : <AVAssetExportSession: 0x280791270, asset = <AVURLAsset: 0x2805ef600, URL = https://firebasestorage.googleapis.com/v0/b/camouflage-43fe0.appspot.com/o/eCg9SNmLVlRUacfUBO1CTWNQizO2%2F31983848-C493-469B-A7CD-F7B215C37526%2Fproject_audio%2F1308EB6E-0FB3-4538-AEEC-DF59945C4CF9%2F3826F259-1B18-4381-B893-E69ACC8D6DEE?alt=media&token=8a0725e0-f34c-4b90-b564-3a3ca52e58cd>, presetName = AVAssetExportPresetAppleM4A, outputFileType = com.apple.m4a-audio

Thanks in advance for your help.

EDIT: I tried wrapping the AVURLAsset inside a AVMutableComposition, but now I get this error instead

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

I think you got an error because of file extension mismatch with 'presetName', 'outputFileType', 'fileURL'.

I try to solve with AVMutableComposition and export audio in m4a format. Audio is on remote server.

let url = URL(string: "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3")
let asset = AVAsset(url: url!)
let playerItem = AVPlayerItem(asset: asset)
let remoteFilePath = url
        

func downloadLocalCopyOfTrack(_ playerItem: AVPlayerItem, finished:@escaping (URL?) -> ()) {
    guard playerItem.asset.isExportable else {
        finished(nil)
        return
    }
    
    let composition = AVMutableComposition()
    let compositionAudioTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
    
    let sourceAudioTrack = playerItem.asset.tracks(withMediaType: AVMediaType.audio).first!
    do {
        let durationInSec = playerItem.asset.duration.seconds
        let duration = CMTime(seconds: durationInSec, preferredTimescale: 1)
        try compositionAudioTrack?.insertTimeRange(CMTimeRange(start: CMTime.zero, duration: duration), of: sourceAudioTrack, at: CMTime.zero)
    } catch {
        print(error.localizedDescription)
        finished(nil)
        return
    }
    
    remoteFilePath.deletePathExtension()
    let fileName = remoteFilePath.lastPathComponent
    let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let outputFilePath = documentDirectory.appendingPathComponent("\(fileName).m4a")
    
    try? FileManager.default.removeItem(at: outputFilePath)
    
    let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
    exportSession!.outputURL = outputFilePath
    exportSession!.outputFileType = AVFileType.m4a
    exportSession!.timeRange = CMTimeRange(start: CMTime.zero, duration: playerItem.duration)
    
    //Timer for Progress of Export Session
    var exportProgressBarTimer = Timer() // initialize timer
    if #available(iOS 10.0, *) {
        exportProgressBarTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
            // Get Progress
            let progress = Float((exportSession!.progress));
            if (progress < 0.99) {
                print("progress", progress * 100)
            }
        }
    }
    
    exportSession!.exportAsynchronously(completionHandler: {
        switch exportSession!.status {
        case .failed:
            print("Export failed: \(exportSession!.error!)")
            exportProgressBarTimer.invalidate()
        case .cancelled:
            print("Export canceled")
            exportProgressBarTimer.invalidate()
        default:
            print("Successfully trimmed audio")
            DispatchQueue.main.async(execute: {
                finished(outputFilePath)
                exportProgressBarTimer.invalidate()
            })
        }
    })
}

Usage:

downloadLocalCopyOfTrack(playerItem, finished: { (url) in
  if url != nil {
    //use url or play here
  }else {
    print("URL is nil")
  }
})
Related