尝试使用AVFoundation在同一帧中合并三个不同的视频轨道时,多次获取第一个视频轨道

时间:2018-12-08 05:01:19

标签: ios swift avfoundation

我要使用 AVFoundation 框架将多个视频及其音频合并到一个视频帧中。

为此,我创建了一个接受资产数组的方法,到目前为止,我正在传递三个不同视频的资产。

到目前为止,我已经合并了他们的音频,但是问题在于视频帧,其中在每个帧中仅重复第一个资产的视频

Current output video

我正在使用下面的代码来合并视频,这些视频完美地结合了所有三个视频的音频,但是输入数组中的第一个视频重复了三遍,这是主要问题:

我希望所有三个不同的视频都显示在帧中。

func merge(Videos aArrAssets: [AVAsset]){

        let mixComposition = AVMutableComposition()

        func setup(asset aAsset: AVAsset, WithComposition aComposition: AVMutableComposition) -> AVAssetTrack{

            let aMutableCompositionVideoTrack = aComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
            let aMutableCompositionAudioTrack = aComposition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)

            let aVideoAssetTrack: AVAssetTrack = aAsset.tracks(withMediaType: .video)[0]
            let aAudioAssetTrack: AVAssetTrack = aAsset.tracks(withMediaType: .audio)[0]

            do{
            try aMutableCompositionVideoTrack?.insertTimeRange(CMTimeRangeMake(start: .zero, duration: aAsset.duration), of: aVideoAssetTrack, at: .zero)
            try aMutableCompositionAudioTrack?.insertTimeRange(CMTimeRangeMake(start: .zero, duration: aAsset.duration), of: aAudioAssetTrack, at: .zero)
            }catch{}

            return aVideoAssetTrack
        }

        let aArrVideoTracks = aArrAssets.map { setup(asset: $0, WithComposition: mixComposition) }

        var aArrLayerInstructions : [AVMutableVideoCompositionLayerInstruction] = []

        //Transform every video
        var aNewHeight : CGFloat = 0
        for (aIndex,aTrack) in aArrVideoTracks.enumerated(){

            aNewHeight += aIndex > 0 ? aArrVideoTracks[aIndex - 1].naturalSize.height : 0

            let aLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: aTrack)
            let aFristTransform = CGAffineTransform(translationX: 0, y: aNewHeight)

            aLayerInstruction.setTransform(aFristTransform, at: .zero)
            aArrLayerInstructions.append(aLayerInstruction)
        }


        let aTotalTime = aArrVideoTracks.map { $0.timeRange.duration }.max()

        let aInstruction = AVMutableVideoCompositionInstruction()
        aInstruction.timeRange = CMTimeRangeMake(start: .zero, duration: aTotalTime!)
        aInstruction.layerInstructions = aArrLayerInstructions

        let aVideoComposition = AVMutableVideoComposition()
        aVideoComposition.instructions = [aInstruction]
        aVideoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)

        let aTotalWidth = aArrVideoTracks.map { $0.naturalSize.width }.max()!
        let aTotalHeight = aArrVideoTracks.map { $0.naturalSize.height }.reduce(0){ $0 + $1 }
        aVideoComposition.renderSize = CGSize(width: aTotalWidth, height: aTotalHeight)


        saveVideo(WithAsset: mixComposition, videoComp : aVideoComposition) { (aError, aUrl) in
            print("Location : \(String(describing: aUrl))")
        }
    }

    private func saveVideo(WithAsset aAsset : AVAsset, videoComp : AVVideoComposition, completion: @escaping (_ error: Error?, _ url: URL?) -> Void){


        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "ddMMyyyy_HHmm"
        let date = dateFormatter.string(from: NSDate() as Date)

        // Exporting
        let savePathUrl: URL = URL(fileURLWithPath: NSHomeDirectory() + "/Documents/newVideo_\(date).mov")
        do { // delete old video
            try FileManager.default.removeItem(at: savePathUrl)
        } catch { print(error.localizedDescription) }


        let assetExport: AVAssetExportSession = AVAssetExportSession(asset: aAsset, presetName: AVAssetExportPresetMediumQuality)!
        assetExport.outputFileType = .mov
        assetExport.outputURL = savePathUrl
//        assetExport.shouldOptimizeForNetworkUse = true
        assetExport.videoComposition = videoComp

        assetExport.exportAsynchronously { () -> Void in
            switch assetExport.status {
            case .completed:
                print("success")
                completion(nil, savePathUrl)
            case .failed:
                print("failed \(assetExport.error?.localizedDescription ?? "error nil")")
                completion(assetExport.error, nil)
            case .cancelled:
                print("cancelled \(assetExport.error?.localizedDescription ?? "error nil")")
                completion(assetExport.error, nil)
            default:
                print("complete")
                completion(assetExport.error, nil)
            }
        }
    }

我知道我在代码中做错了什么,但无法弄清楚哪里,所以我需要一些帮助才能找到它。

谢谢。

1 个答案:

答案 0 :(得分:1)

您的问题是,在构建AVMutableVideoCompositionLayerInstruction时,aTrack引用是对您使用

设置的原始资产的跟踪的引用。
let aVideoAssetTrack: AVAssetTrack = aAsset.tracks(withMediaType: .video)[0]

它的trackID为1,因为它是源AVAsset中的第一条轨道。因此,当您检查aArrLayerInstructions时,您会发现指令的trackID均为1。这就是为什么您要三遍看第一部视频

(lldb) p aArrLayerInstructions[0].trackID
(CMPersistentTrackID) $R8 = 1
(lldb) p aArrLayerInstructions[1].trackID
(CMPersistentTrackID) $R10 = 1
...

解决方案不是在构建合成层指令时枚举源轨道,而是枚举合成轨道。

let tracks = mixComposition.tracks(withMediaType: .video)
for (aIndex,aTrack) in tracks.enumerated(){
...

如果您这样做,将为您的图层说明获取正确的trackID。

(lldb) p aArrLayerInstructions[0].trackID
(CMPersistentTrackID) $R2 = 1
(lldb) p aArrLayerInstructions[1].trackID
(CMPersistentTrackID) $R4 = 3
...