合并视频(mkv)和音频(mka)但它并没有在swift中创建资产

时间:2017-12-22 12:54:58

标签: swift audio video merge

我正在合并一个音频(.mka)和视频(.mkv)文件,这些文件已下载到设备上,但AVAsset不是从URL创建的。

错误:

  

[__ NSArray0 objectAtIndex:]:索引0超出空NSArray的界限'

func mergeMutableVideoWithAudio(videoUrl:NSURL, audioUrl:NSURL){

    let mixComposition : AVMutableComposition = AVMutableComposition()
    var mutableCompositionVideoTrack : [AVMutableCompositionTrack] = []
    var mutableCompositionAudioTrack : [AVMutableCompositionTrack] = []
    let totalVideoCompositionInstruction : AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()

    //start merge
    let aVideoAsset : AVAsset = AVAsset(url:videoUrl as URL)
    let aAudioAsset : AVAsset = AVAsset(url:audioUrl as URL)

    let avurlAsset = AVURLAsset(url: videoUrl as URL, options: nil)

    let newArray = avurlAsset.tracks(withMediaType: .video)

    print("\(newArray.count)")

    mutableCompositionVideoTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!)

    mutableCompositionAudioTrack.append( mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)!)

    let aVideoAssetsArray : [AVAssetTrack] = aVideoAsset.tracks(withMediaType: .video)
    let aAudioAssetsArray : [AVAssetTrack] = aAudioAsset.tracks(withMediaType: .audio)

    print("\(aVideoAssetsArray.count)")
    print("\(aAudioAssetsArray.count)")

    let aVideoAssetTrack : AVAssetTrack = aVideoAssetsArray[0]
    let aAudioAssetTrack : AVAssetTrack = aAudioAssetsArray[0]
    do{
        try mutableCompositionVideoTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration), of: aVideoAssetTrack, at: kCMTimeZero)
        try mutableCompositionAudioTrack[0].insertTimeRange(CMTimeRangeMake(kCMTimeZero, aVideoAssetTrack.timeRange.duration), of: aAudioAssetTrack, at: kCMTimeZero)
    }catch{

    }
    totalVideoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,aVideoAssetTrack.timeRange.duration )
    let mutableVideoComposition : AVMutableVideoComposition = AVMutableVideoComposition()
    mutableVideoComposition.frameDuration = CMTimeMake(1, 30)
    mutableVideoComposition.renderSize = CGSize(width: 1280, height: 720)
    let mergedAudioVideoURl = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/FinalVideo.mp4")

    let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!

    assetExport.outputFileType = AVFileType.mp4
    assetExport.outputURL = mergedAudioVideoURl as URL
    removeFileAtURLIfExists(url: mergedAudioVideoURl)
    assetExport.shouldOptimizeForNetworkUse = true
    assetExport.exportAsynchronously { () -> Void in
        switch assetExport.status {
        case AVAssetExportSessionStatus.completed:
            print("-----Merge mutable video with trimmed audio exportation complete.\(mergedAudioVideoURl)")
        case  AVAssetExportSessionStatus.failed:
            print("failed \(assetExport.error)")
        case AVAssetExportSessionStatus.cancelled:
            print("cancelled \(assetExport.error)")
        default:
            print("complete")
        }
    }
}

Apple是否不支持.mka和.mkv文件?

0 个答案:

没有答案