[ad_1]
I am attempting to imitate cropping a panorama video to portrait like defined right here. These aren’t directions, it explains that Apple added the flexibility to crop a panorama video to portrait in iOS 13 within the gadget’s Photographs album.
Utilizing the code beneath, to date evidently I have been capable of middle the center of a panorama video, crop the video, after which create portrait model of the video. The one situation is the ensuing portrait video is not centered in the midst of the display screen.
horizontal video:
ensuing portrait video however incorrectly centered:
that is appropriately centered:
How can I get the ensuing portrait video within the middle of the display screen?
func createExportSession(for videoURL: URL) {
let asset = AVURLAsset(url: videoURL)
let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)!
exporter.videoComposition = turnHorizontalVideoToPortraitVideo(horizontalAsset: asset)
exporter.outputURL = // ...
exporter.outputFileType = AVFileType.mp4
exporter.shouldOptimizeForNetworkUse = true
exporter.exportAsynchronously { [weak self] in
// ...
}
}
func turnHorizontalVideoToPortraitVideo(horizontalAsset: AVURLAsset) -> AVVideoComposition {
let observe = horizontalAsset.tracks(withMediaType: AVMediaType.video)[0]
let renderSize = CGSize(width: 720, peak: 1280)
var transform1 = observe.preferredTransform
transform1 = transform1.concatenating(CGAffineTransform(rotationAngle: CGFloat(90.0 * .pi / 180)))
transform1 = transform1.concatenating(CGAffineTransform(translationX: observe.naturalSize.width, y: 0))
let transform2 = CGAffineTransform(translationX: observe.naturalSize.peak, y: (observe.naturalSize.width - observe.naturalSize.peak) / 2)
let transform3 = transform2.rotated(by: Double.pi/2).concatenating(transform1)
let translate = CGAffineTransform(translationX: renderSize.width, y: renderSize.peak)
let rotateFromUpsideDown = translate.rotated(by: CGFloat(Double.pi)) // with out this the portrait video is at all times the other way up
let finalTransform = transform3.concatenating(rotateFromUpsideDown)
let transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: observe)
transformer.setTransform(finalTransform, at: .zero)
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRange(begin: .zero, length: asset.length)
instruction.layerInstructions = [transformer]
let videoComposition = AVMutableVideoComposition()
videoComposition.frameDuration = CMTime(worth: 1, timescale: 30)
videoComposition.renderSize = renderSize
videoComposition.directions = [instruction]
return videoComposition
}
[ad_2]




