swiftvideoavassetexportsessionm4v

AVAssetExportSession is not exporting M4V files


I am trying to do an AVAssetExportSession with Swift, and I get this error: 'NSInvalidArgumentException', reason: 'Invalid output file type'. I looked up this error, and it says that for video, Swift only supports QuickTime files, MP4, and AppleM4V, which is weird, because I am trying to export a M4V file. However, I tried exporting an MP4 video, with the filename having the MP4 extension, and the outputFileType being an MP4 file as well, and it worked amazing. I changed it later to all be M4V (Including the video being exported being an M4V file), and it gives me the error. Am I doing something wrong? My code is down below:

let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)

        let filename = "filename.m4v"

        let documentsDirectory = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).last!

        let outputURL = documentsDirectory.appendingPathComponent(filename)

        exporter?.outputURL = outputURL
        exporter?.outputFileType = AVFileTypeAppleM4V

        exporter?.exportAsynchronously(completionHandler: {

        })

Any help is appreciated, thanks :)


Solution

  • AVAssetExportPresetHighestQuality only supports .mov files. You have to use AVAssetExportPresetPassthrough instead.

    let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough)
    

    According to the Documentation, AVAssetExportPresetLowQuality, AVAssetExportPresetMediumQuality and AVAssetExportPresetHighQuality are for QuickTime .mov files only.

    You can find out more by trying to print the contents of the variable supportedFileTypes of the AVAssetExportSession.