AVAssetExportPreset类型

时间:2017-09-13 08:55:29

标签: ios swift avasset

我正在使用此扩展程序将视频文件从AVAsset保存到tmp文件夹。问题是,当我使用AVAssetExportPresetHighestQuality类型的视频文件时,由于这个原因无法保存:

  

错误域= AVFoundationErrorDomain代码= -11800"操作可以   没完成" UserInfo = {NSUnderlyingError = 0x1748482e0 {错误   Domain = NSOSStatusErrorDomain Code = -12780"(null)"},   NSLocalizedFailureReason =发生未知错误(-12780),   NSLocalizedDescription =无法完成操作}

有时甚至在我使用AVAssetExportPresetHighestQuality时,它会以随机顺序保存视频。

extension AVAsset {

    func write(to url: URL, success: @escaping () -> (), failure: @escaping (Error) -> ()) {
        guard let exportSession = AVAssetExportSession(asset: self, presetName: AVAssetExportPresetMediumQuality) else {
            let error = NSError(domain: "domain", code: 0, userInfo: nil)
            failure(error)

            return
        }

        exportSession.outputFileType = AVFileTypeMPEG4
        exportSession.outputURL = url

        exportSession.exportAsynchronously {
            switch exportSession.status {
            case .completed:
                success()
            case .unknown, .waiting, .exporting, .failed, .cancelled:
                let error = NSError(domain: "domain", code: 0, userInfo: nil)
                failure(error)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

此问题与AVAsset组件的错误长度有关。由于某些原因,AVAsset曲目的视频和音轨的持续时间不同,这是主要问题。

要解决此问题,我使用AVAsset的自定义扩展程序。此功能将根据视频和音频轨道创建新的AVAsset,条件将解决持续时间问题。因此,AVAsset获得的normalizingMediaDuration()可以成功导出。

extension AVAsset {
    func normalizingMediaDuration() -> AVAsset? {
        let mixComposition : AVMutableComposition = AVMutableComposition()
        var mutableCompositionVideoTrack : [AVMutableCompositionTrack] = []
        var mutableCompositionAudioTrack : [AVMutableCompositionTrack] = []
        let totalVideoCompositionInstruction : AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()

        guard let video = tracks(withMediaType: AVMediaTypeVideo).first else {
            return nil
        }

        guard let audio = tracks(withMediaType: AVMediaTypeAudio).first else {
            return nil
        }

        mutableCompositionVideoTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid))
        mutableCompositionAudioTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid))

        let duration = video.timeRange.duration.seconds > audio.timeRange.duration.seconds ? audio.timeRange.duration : video.timeRange.duration

        do{
            try mutableCompositionVideoTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero,duration), of: video, at: kCMTimeZero)
            try mutableCompositionAudioTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, duration), of: audio, at: kCMTimeZero)
        }catch{
            return nil
        }

        totalVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,duration)

        return mixComposition
    }
}