I am using AVMutableComposition to merge/stitch multiple recorded video tracks and save it like this expected output
..so I am looping through the subviews to transform each tracks according to each subview size.
var index = 0
for subview:UIView in template.subviews {
let scaleX = (subview.frame.width / template.frame.width) / (tracks[index].naturalSize.width / mainComposition.renderSize.width)
let scaleY = (subview.frame.height / template.frame.height) / (tracks[index].naturalSize.height / mainComposition.renderSize.height)
let x = subview.frame.origin.x
let y = subview.frame.origin.y
let rotate = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
let scale = CGAffineTransformMakeScale(scaleX, scaleY)
let translate = CGAffineTransformMakeTranslation(x, y)
let transform = CGAffineTransformConcat(scale, translate)
let transformation = AVMutableVideoCompositionLayerInstruction(assetTrack: tracks[index])
transformation.setTransform(CGAffineTransformConcat(rotate, transform), atTime: kCMTimeZero)
instructions.append(transformation)
index++
}
The problem is that the videos are all out of placed in the saved video. But without the rotation, I am getting the expected output like in the attached image but with the videos in landscape - which i read that is always the case for captured videos in iPhone device (how to export video asset via AVAssetExportSession in portrait mode)
I have been playing with CGAffineTransform, rearranging translate, rotate, or scale on which goes first or last but not getting good result. I have also tried creating two layerinstruction separating the rotation and sctaletranslate but still the same.
Thanks for any help