I have been trying to get portrait video to transform in a such what that it could be aspect fit in center of player while keep rest of the screen empty.
Example of How the video should look like
here is the code : Few thing : self.videoRenderSize = CGSize(width: 1920.0, height: 1080.0) videoTrackSize could be (960,720), (1280,720), (1920,1080) ( All Portrait though, so width is height and vice versa)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = passThroughTimeRanges[i]
let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: currentTrack)
let assetObject = self.assetsList[i]["assetVideo"] as! Asset
let asset = assetObject.movieAsset as AVAsset
let videoTrack:AVAssetTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]
let videoTrackSize = videoTrack.naturalSize
let preferredTransform = videoTrack.preferredTransform
let videoOrientationMode = self.isVideoPortrait(asset)
print(preferredTransform)
print(videoTrackSize)
switch videoOrientationMode {
case "Portrait" :
if self.videoRenderSize.height > videoTrackSize.height {
}else if self.videoRenderSize.height == videoTrackSize.height {
}else {
}
break
case "PortraitUpsideDown":
break
case "LandscapeRight", "LandscapeLeft":
let heightScale = CGFloat(self.videoRenderSize.height / videoTrackSize.height)
let widthScale = CGFloat(self.videoRenderSize.width / videoTrackSize.width)
layerInstruction.setTransform(CGAffineTransformMakeScale(widthScale,heightScale), atTime: instruction.timeRange.start)
break
default:
print("Unknown Orientation")
}
instruction.layerInstructions = [layerInstruction]
compositionInstructions.append(instruction)
}
videoComposition.instructions = compositionInstructions
videoComposition.renderSize = self.videoRenderSize
videoComposition.frameDuration = CMTimeMake(1, 30)
videoComposition.renderScale = 1.0 // This is a iPhone only option.
Code for isVideoPortrait Function
func isVideoPortrait(asset : AVAsset) -> String{
var videoOrientation : String = ""
let tracks : NSArray = asset.tracksWithMediaType(AVMediaTypeVideo)
if(tracks.count > 0) {
let videoTrack : AVAssetTrack = tracks[0] as! AVAssetTrack
let t = videoTrack.preferredTransform
// Portrait
if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0 || t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0)
{
videoOrientation = "Portrait"
}
// PortraitUpsideDown
if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0) {
videoOrientation = "PortraitUpsideDown"
}
// LandscapeRight
if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0)
{
videoOrientation = "LandscapeRight"
}
// LandscapeLeft
if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0)
{
videoOrientation = "LandscapeLeft"
}
}
return videoOrientation
}
What would be transformation layerInstruction for making sure video will transfor in center and could be placed like in image attached above.