ios - CGAffineTransform 应用额外的旋转
问题描述
从图库中挑选视频后,我正在尝试旋转它并将其保存在图库中。当我选择一个处于横向模式的视频时,我的旋转效果很好。但是,如果我选择一个处于纵向模式的视频,并尝试以给定的角度旋转它。视频获得额外的旋转。
这是我的代码:
let currentAsset = AVAsset( url: outputFileURL)
let composition = AVMutableComposition.init()
let videoComposition = AVMutableVideoComposition()
videoComposition.frameDuration = CMTimeMake(1, 30)
videoComposition.renderScale = 1.0
let compositionCommentaryTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)
let compositionVideoTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
let videoDuration: CMTime = currentAsset.duration
let clipVideoTrack:AVAssetTrack = currentAsset.tracks(withMediaType: AVMediaType.video)[0]
let audioTrack: AVAssetTrack = currentAsset.tracks(withMediaType: AVMediaType.audio)[0]
try? compositionCommentaryTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, currentAsset.duration), of: audioTrack, at: kCMTimeZero)
try? compositionVideoTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, currentAsset.duration ), of: clipVideoTrack, at: kCMTimeZero)
var naturalSize = clipVideoTrack.naturalSize
naturalSize = CGSize.init(width: naturalSize.width, height: naturalSize.height)
videoComposition.renderSize = naturalSize
let x = naturalSize.width
let y = naturalSize.height
let scale = CGFloat(1.0)
let frontLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack!)
var rotationTransform = CGAffineTransform(translationX: x/2, y: y/2)
rotationTransform = rotationTransform.rotated(by: angle)
rotationTransform = rotationTransform.translatedBy(x: -x/2, y: -y/2)
frontLayerInstruction.setTransform(rotationTransform, at: kCMTimeZero)
// saving video in gallery
let MainInstruction = AVMutableVideoCompositionInstruction()
MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration)
MainInstruction.layerInstructions = [frontLayerInstruction]
videoComposition.instructions = [MainInstruction]
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let videoPath = documentsPath+"/cropEditVideo.mov"
let fileManager = FileManager.default
if fileManager.fileExists(atPath: videoPath) {
try! fileManager.removeItem(atPath: videoPath)
}
var exportSession = AVAssetExportSession.init(asset: composition, presetName: AVAssetExportPresetHighestQuality)
exportSession?.outputFileType = AVFileType.mov
exportSession?.videoComposition = videoComposition
exportSession?.outputURL = URL.init(fileURLWithPath: videoPath)
.
.
.
.
我没有发布使用 AVAssetExportSession 保存视频的完整代码,因为我相信它不需要。
因此,如果我对视频应用 10 度的旋转,它会以 280 度的旋转保存。所以基本上是一个附加的,它只发生在肖像视频中。
任何帮助将非常感激。
解决方案
让我们考虑一下:
你有2个案例:
您旋转横向视频,它可以完美运行;
您旋转纵向视频,它可以完美运行;
因此,问题不在于轮换;您尝试使用的肖像视频已经进行了初始旋转!不用担心,这一直在发生,通常是默认行为(我不知道为什么,我已经处理过很多次了)
一些例子:
无需重写已经生成的代码;您的问题在这里有解决方案,但是,如果您找不到答案,请在下方评论,我们可以解决!
推荐阅读
- gitlab - 即使组在重新配置期间拥有该文件夹,Gitlab 也没有启动并尝试执行 chgrp 命令
- gcc - 找不到 -ll collect2:错误:ld 返回 1 退出状态
- reactjs - UseEffect 或在功能组件中抛出逻辑
- pycharm - 无法在 Windows 10 PC 中打开我的 PyCharm 应用程序?
- c++ - 单个文件中的多个命名空间相互引用 C++
- python-3.x - Modin df iterrows 非常缓慢。有什么办法可以加快速度吗?
- php - 如何将表格的内容发送到我的电子邮件?使用 HTML 和 PHP
- elasticsearch - 知道弹性搜索中的开始日期字段时如何计算持续时间字段?
- postgresql - 我将如何按大多数加权匹配排序?
- php - 我想删除 json 格式