gpt4 book ai didi

ios - 水平翻转视频如果是这样它没有镜像效果

转载 作者:可可西里 更新时间:2023-11-01 01:59:19 24 4
gpt4 key购买 nike

在我的自定义相机中,当我使用前置摄像头拍摄视频时,它会像原始 iPhone 相机一样产生镜像效果。我不想要那个。我想水平翻转视频,并在下面的这个函数中实现它。我有一个名为 filmedWithFront 的 bool 变量,当使用前置摄像头拍摄视频时该变量为真。

var filmedWithFront = false

func cropVideo(_ outputFileURL:URL){
let videoAsset: AVAsset = AVAsset(url: outputFileURL) as AVAsset

let clipVideoTrack = videoAsset.tracks(withMediaType: AVMediaType.video).first! as AVAssetTrack

let composition = AVMutableComposition()
composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: CMPersistentTrackID())

let videoComposition = AVMutableVideoComposition()

videoComposition.renderSize = CGSize(width: 720, height: 1280)

videoComposition.frameDuration = CMTimeMake(1, 30)

let instruction = AVMutableVideoCompositionInstruction()

instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(180, 30))

// rotate to portrait
let transformer:AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
let t1 = CGAffineTransform(translationX: 720, y: 0);
let t2 = t1.rotated(by: CGFloat(CGFloat.pi/2));

transformer.setTransform(t2, at: kCMTimeZero)
instruction.layerInstructions = [transformer]
videoComposition.instructions = [instruction]


if filmedWithFront == true {
// This is where I want to add the code to flip video horizontally
}


let removedPath = outputFileURL.path
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let cropUniqueId = NSUUID().uuidString
let outputPath = "\(documentsPath)/\(cropUniqueId).mov"

arrayOfStringPaths.append(outputPath)
stringOfArrayPaths = outputPath
let relativePath = "\(cropUniqueId).mov"
let relativeURL = URL(fileURLWithPath: relativePath)
saveData(arrayPath: relativePath)
let outputUrl = URL(fileURLWithPath: outputPath, relativeTo: relativeURL)
let exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPreset1280x720)!

exporter.videoComposition = videoComposition
exporter.outputURL = outputUrl
exporter.outputFileType = AVFileType.mov
exporter.shouldOptimizeForNetworkUse = true

exporter.exportAsynchronously(completionHandler: { () -> Void in
DispatchQueue.main.async(execute: {
self.handleExportCompletion(exporter, removedPath)
})
})
}

最佳答案

这是我为最终修复前置摄像头的镜像视频输出所做的转换的片段...videoInputWriter 是 AVAssetWriterInput。希望这会有所帮助。

if (cameraPosition == .front) {
var transform: CGAffineTransform = CGAffineTransform(scaleX: -1.0, y: 1.0)
transform = transform.rotated(by: CGFloat(Double.pi/2))
self.videoInputWriter.transform = transform
}

关于ios - 水平翻转视频如果是这样它没有镜像效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47860896/

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