一尘不染

如何通过合并前后摄像头的视频来解决我的方向问题

swift

我正在合并多个视频(植入暂停按钮),并且将后置摄像头的视频与前摄像头的视频合并时,一切工作正常,然后其中一个视频在新视频(合并的视频)中上下颠倒了。
我的代码:

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

阅读 250

收藏
2020-07-07

共1个答案

一尘不染

你需要小心的renderSize你的AVMutableVideoComposition和的转换AVMutableVideoCompositionLayerInstruction

为了正确对齐它们,您需要以正确的角度 平移旋转 视频。要将其上下翻转,您需要将其旋转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以使转换生效。

2020-07-07