Manish Gumbal
Manish Gumbal

Reputation: 293

Merge Videos Array in Swift

I am trying to merge the videos from the array of avssets but I am able to get only first and last video. Videos between the array are showing black area. Check the code I am using.

    func mergeVideoArray() {
    let mixComposition = AVMutableComposition()
    for videoAsset in videoURLArray {

        let videoTrack =
            mixComposition.addMutableTrack(withMediaType: AVMediaType.video,
                                           preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
        do {
            try videoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration),
                                            of: videoAsset.tracks(withMediaType: AVMediaType.video).first!,
                                           at: totalTime)
            videoSize = (videoTrack?.naturalSize)!
        } catch let error as NSError {
            print("error: \(error)")
        }
        let trackArray = videoAsset.tracks(withMediaType: .audio)
        if trackArray.count > 0 {
            let audioTrack =
                mixComposition.addMutableTrack(withMediaType: AVMediaType.audio,
                                               preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
            do {
                try audioTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: videoAsset.tracks(withMediaType: AVMediaType.audio).first!, at: audioTime)
                audioTime = audioTime + videoAsset.duration
            }
            catch {

            }
        }
        totalTime = totalTime + videoAsset.duration
        let videoInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack!)
        if videoAsset != videoURLArray.last{
            videoInstruction.setOpacity(0.0, at: videoAsset.duration)
        }
        layerInstructionsArray.append(videoInstruction)
    }
    let mainInstruction = AVMutableVideoCompositionInstruction()

    mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, totalTime)
    mainInstruction.layerInstructions = layerInstructionsArray

    let mainComposition = AVMutableVideoComposition()
    mainComposition.instructions = [mainInstruction]
    mainComposition.frameDuration = CMTimeMake(1, 30)
    mainComposition.renderSize = CGSize(width: videoSize.width, height: videoSize.height)

    let url = "merge_video".outputURL
    let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
    exporter!.outputURL = url
    exporter!.outputFileType = AVFileType.mov
    exporter!.shouldOptimizeForNetworkUse = false
    exporter!.videoComposition = mainComposition

    exporter!.exportAsynchronously {

        let video = AVAsset(url: url)
        let playerItem = AVPlayerItem(asset: video)
        let player = AVPlayer(playerItem: playerItem)
        self.playerViewController.player = player

        self.present(self.playerViewController, animated: true) {
            self.playerViewController.player!.play()
        }
    }
}

Please help me resolving this issue. Thanks in advance.

Note I am able to create a video from the array but only first and last index values are showing in videos. For rest of the values only blank screen is showing.

Upvotes: 2

Views: 1272

Answers (2)

Umair Ali Khan
Umair Ali Khan

Reputation: 27

Right way to do it would be to change

   if videoAsset != videoURLArray.last{
    videoInstruction.setOpacity(0.0, at: videoAsset.duration)
}

to:

videoInstruction.setOpacity(0.0, at: totalTime)

I want to emphasize here that adding

        totalTime = totalTime + videoAsset.duration

before setting opacity to layer instruction makes all the difference. Otherwise videos are black screen.

Upvotes: 0

Manish Gumbal
Manish Gumbal

Reputation: 293

I just solved my question just need to update the one line in the code. Please have a look in to the code.

    if videoAsset != videoURLArray.last{
        videoInstruction.setOpacity(0.0, at: totalTime)
    }

Note: just need to change the at position of the next video for the every value of array.

Upvotes: 1

Related Questions