swift2 如何解决我的方向问题与合并视频从正面和背面摄像头

yzuktlbb  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(211)

我正在合并多个视频(暂停按钮的植入)和一切工作正常,除了当合并来自后摄像头的视频与来自前摄像头的视频,然后其中一个视频在新的视频(合并的视频)颠倒过来。我的代码:

let mixComposition = AVMutableComposition() 

let videoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
let trackAudio = mixComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())

var insertTime = kCMTimeZero
for var i = 0; i < currentAssets.count; i++ {
    let tracks = currentAssets[i].tracksWithMediaType(AVMediaTypeVideo)
    let audios = currentAssets[i].tracksWithMediaType(AVMediaTypeAudio)

    let assetTrack:AVAssetTrack = tracks[0] as AVAssetTrack
    try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, currentAssets[i].duration), ofTrack: assetTrack, atTime: insertTime)
    let assetTrackAudio:AVAssetTrack = audios[0] as AVAssetTrack
    try trackAudio.insertTimeRange(CMTimeRangeMake(kCMTimeZero, currentAssets[i].duration), ofTrack: assetTrackAudio, atTime: insertTime)
    insertTime = CMTimeAdd(insertTime, currentAssets[i].duration)
}
videoTrack.preferredTransform = assetTrack.preferredTransform

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory,inDomains: .UserDomainMask).last!
let mediaURL = documentsURL.URLByAppendingPathComponent(AppMediaFolder)
let savePath = mediaURL.URLByAppendingPathComponent("\(NSUUID().UUIDString).mp4").path!

self.createDirectoryIfExists(mediaURL)
let url = NSURL(fileURLWithPath: savePath)

currentAssets.removeAll()
currentAssets.append(AVAsset(URL: url))

//Create Exporter
let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
exporter.outputURL = url
exporter.outputFileType = AVFileTypeMPEG4
exporter.shouldOptimizeForNetworkUse = true
von4xj4u

von4xj4u1#

您需要小心处理AVMutableVideoCompositionrenderSizeAVMutableVideoCompositionLayerInstruction的变换。
要正确对齐它们,您需要以正确的Angular平移旋转视频。要将其上下翻转,您需要将其旋转180度,然后平移到正确的坐标:

...
videoComposition.renderSize = CGSizeMake(X, Y)
...
let translate = CGAffineTransformMakeTranslation(X, Y);
let rotate = CGAffineTransformRotate(translate, CGFloat(ANGLE_IN_RADIANS))
...

在我的例子中,renderSize和平移被设置为1280和720,180度旋转基本上是M_PI的弧度:

let videoComposition = AVMutableVideoComposition()
videoComposition.renderSize = CGSizeMake(1280, 720)
videoComposition.frameDuration = CMTimeMake(1, 30)

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

let transformInstruction:AVMutableVideoCompositionLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)
let translate = CGAffineTransformMakeTranslation(1280, 720);
let rotate = CGAffineTransformRotate(translate, CGFloat(M_PI))

transformInstruction.setTransform(rotate, atTime: kCMTimeZero)
videoInstruction.layerInstructions = [transformInstruction]
videoComposition.instructions = [videoInstruction]

最后,您仍需要将此 videoComposition 添加到AVAssetExportSession中,以使转换生效。

相关问题