gpt4 book ai didi

ios - CGAffineTransform 应用附加旋转

转载 作者:行者123 更新时间:2023-11-28 07:25:27 25 4
gpt4 key购买 nike

从图库中选择视频后,我试图旋转它并将其保存在图库中。当我选择一个处于横向模式的视频时,我的旋转效果很好。但是,如果我选择一个处于纵向模式的视频,并尝试将其旋转到给定的角度。视频获得额外的旋转。

这是我的代码:

        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 个案例:

  1. 您旋转横向视频,效果非常好;

  2. 您旋转一个竖屏视频,效果非常好;

因此,问题不在于轮换;您尝试使用的肖像视频已经有了初始轮换!不用担心,这种情况经常发生并且通常是默认行为(我不确定为什么,我已经处理过很多次了)

一些例子:

AR Problem

AVAssetExportSession Problem

无需重写已经生成的代码;你的问题有解决方案here ,但是,如果您找不到答案,请在下方发表评论,我们可以解决!

关于ios - CGAffineTransform 应用附加旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56747356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com