I am trying to merge two recorded videos with this code:
func exportVideo3() {
transprentView.hidden = false
activityIndicator.startAnimating()
let composition = AVMutableComposition()
let trackVideo = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
let trackAudio = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
var insertTime = kCMTimeZero
let path = NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false, error: nil)!.path!
for index in 1...2 {
let movieUrl = index == 1 ? video1URL : video2URL
let sourceAsset = AVURLAsset(URL: movieUrl, options: nil)
let tracks = sourceAsset.tracksWithMediaType(AVMediaTypeVideo)
let audios = sourceAsset.tracksWithMediaType(AVMediaTypeAudio)
if tracks.count > 0{
let assetTrack:AVAssetTrack = tracks[0] as! AVAssetTrack
trackVideo.insertTimeRange(CMTimeRangeMake(kCMTimeZero,sourceAsset.duration), ofTrack: assetTrack, atTime: insertTime, error: nil)
let assetTrackAudio:AVAssetTrack = audios[0] as! AVAssetTrack
trackAudio.insertTimeRange(CMTimeRangeMake(kCMTimeZero,sourceAsset.duration), ofTrack: assetTrackAudio, atTime: insertTime, error: nil)
insertTime = CMTimeAdd(insertTime, sourceAsset.duration)
}
}
let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
exporter.outputURL = documentsDirectoryURL.URLByAppendingPathComponent("mergedMovie.mp4")
if NSFileManager.defaultManager().fileExistsAtPath(exporter.outputURL.path!) {
NSFileManager.defaultManager().removeItemAtURL(exporter.outputURL, error: nil)
}
mergedVideoURL = exporter.outputURL
exporter.outputFileType = AVFileTypeMPEG4
exporter.exportAsynchronouslyWithCompletionHandler({
switch exporter.status{
case AVAssetExportSessionStatus.Failed:
println("failed \(exporter.error)")
case AVAssetExportSessionStatus.Cancelled:
println("cancelled \(exporter.error)")
default:
self.transprentView.hidden = true
self.activityIndicator.stopAnimating()
println("complete")
}
})
}
It is working fine but new Video have opposite orientation. i.e when I recored both video in portrait and merge it then the new video plays in landscape mode and when I record both in landscape and merge it then the new video plays in portrait mode.
So how can I solve this issue like when I record video in landscape mode I want output in landscape mode and same as portrait.
HERE is my sample project for more reference.