[SOLVED] 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)
    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) {

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.


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.

Answered By – Manish Gumbal

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.