Tried to use Ray great tutorial to fix the orientation issue.
left - how the video should look(portrait), right - unwanted rotated result
Code used
func setUpVideoNew()
{
let originalVideoTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]
let composition = AVMutableComposition()
let videoTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: 1)
let timeRange = originalVideoTrack.timeRange
do {
try videoTrack.insertTimeRange(timeRange, ofTrack: originalVideoTrack, atTime: kCMTimeZero)
} catch {
}
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = timeRange
let firstlayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
let firstAssetTrack = originalVideoTrack
//MARK - Fix Rotation
var firstAssetOrientation_ = UIImageOrientation.Up
var isFirstAssetPortrait_ = false
var firstTransform = videoTrack.preferredTransform;
if firstTransform.a == 0 && firstTransform.b == 1.0 && firstTransform.c == -1.0 && firstTransform.d == 0 {
firstAssetOrientation_ = UIImageOrientation.Right;
isFirstAssetPortrait_ = true;
}
if (firstTransform.a == 0 && firstTransform.b == -1.0 && firstTransform.c == 1.0 && firstTransform.d == 0) {
firstAssetOrientation_ = UIImageOrientation.Left;
isFirstAssetPortrait_ = true;
}
if (firstTransform.a == 1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == 1.0) {
firstAssetOrientation_ = UIImageOrientation.Up;
}
if (firstTransform.a == -1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == -1.0) {
firstAssetOrientation_ = UIImageOrientation.Down;
}
firstlayerInstruction.setTransform(asset.preferredTransform, atTime: kCMTimeZero)
mainInstruction.layerInstructions = [firstlayerInstruction]
let videoComposition = AVMutableVideoComposition()
videoComposition.frameDuration = CMTimeMake(1, 30)
videoComposition.instructions = [mainInstruction]
var naturalSizeFirst = CGSize()
if(isFirstAssetPortrait_){
naturalSizeFirst = CGSizeMake(videoTrack.naturalSize.height, videoTrack.naturalSize.width);
} else {
naturalSizeFirst = videoTrack.naturalSize;
}
videoComposition.renderSize = naturalSizeFirst
let playerItem = AVPlayerItem(asset: composition)
playerItem.videoComposition = videoComposition
player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
print(videoPreview.frame)
print(videoPreview.bounds)
playerLayer.frame = self.videoPreview.bounds
player.play()
self.videoPreview.layer.addSublayer(playerLayer)
}
Any suggestions?