Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[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 machine’s Photographs album.
Utilizing the code under, to this point evidently I have been in a position to heart 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 course 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 heart 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, top: 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.top, y: (observe.naturalSize.width - observe.naturalSize.top) / 2)
let transform3 = transform2.rotated(by: Double.pi/2).concatenating(transform1)
let translate = CGAffineTransform(translationX: renderSize.width, y: renderSize.top)
let rotateFromUpsideDown = translate.rotated(by: CGFloat(Double.pi)) // with out this the portrait video is all the time 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]