1

I want to merge multiple videos and I want to keep their actual orientation in final video. This is my code

    func mergeSelectedVideos() {
            let composition = AVMutableComposition()
            let videoTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
            let audioTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)

            var time: Double = 0.0

            for video in self.arrayOfSelectedVideoURL {
                let sourceAsset = AVAsset(url: video as URL)
                let videoAssetTrack = sourceAsset.tracks(withMediaType: AVMediaTypeVideo)[0]
                let audioAssetTrack = sourceAsset.tracks(withMediaType: AVMediaTypeAudio)[0]
                let atTime = CMTime(seconds: time, preferredTimescale:1)            
                do{
                    try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, sourceAsset.duration) , of: videoAssetTrack, at: atTime)
                    try audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, sourceAsset.duration) , of: audioAssetTrack, at: atTime)
                }catch{
                    print("-----Something wrong.")
                }
                time +=  sourceAsset.duration.seconds
            }

            let mergedVideoURL = NSURL(fileURLWithPath: NSHomeDirectory() + "/Documents/mergedVideo.mp4")
            let exporter: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!
            exporter.outputFileType = AVFileTypeMPEG4
            exporter.outputURL = mergedVideoURL as URL
            removeFileAtURLIfExists(url: mergedVideoURL)
            exporter.exportAsynchronously(completionHandler:
                {
                    switch exporter.status
                    {
                        case AVAssetExportSessionStatus.failed:
                            print("failed \(String(describing: exporter.error))")
                        case AVAssetExportSessionStatus.cancelled:
                            print("cancelled \(String(describing: exporter.error))")
                        case AVAssetExportSessionStatus.unknown:
                            print("unknown\(String(describing: exporter.error))")
                        case AVAssetExportSessionStatus.waiting:
                            print("waiting\(String(describing: exporter.error))")
                        case AVAssetExportSessionStatus.exporting:
                            print("exporting\(String(describing: exporter.error))")
                        default:
                            print("-----Merged video exportation complete.\(self.mergedVideoURL)")
                    }
                })
}

with this code I am able to merge multiple videos but portrait videos get rotated in merged video. Is it possible to merge them in a way, where portrait video will stay portrait and landscape video will stay landscape in final video? I searched a lot but didn't find any.

xXx
  • 11
  • 2
  • You need to perform transforms on the videos in order to rotate them. Besides you need to decide how you present the portrait videos - is it full video with blank screen at its sides or you only present the part of the video which covers the whole screen. The transforms you need to perform depend on that. – Max Pevsner Apr 18 '17 at 06:04
  • thanks. I want to present the portrait videos as full video with blank screen at its sides ... how to transform this? any code snippet would be a great help @MaxPevsner – xXx Apr 18 '17 at 06:17
  • check this question and the answer http://stackoverflow.com/questions/12136841/avmutablevideocomposition-rotated-video-captured-in-portrait-mode. It should help. – Max Pevsner Apr 18 '17 at 06:28
  • I followed the answer of @dizy ....but it didn't work for me. – xXx Apr 18 '17 at 06:32

0 Answers0