我正在合并多个视频(植入暂停按钮),并且当将来自后置摄像头的视频与来自前置摄像头的视频合并时,一切工作正常,然后其中一个视频在新视频中被颠倒过来(合并视频) 。我的代码:
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
答案 0 :(得分:2)
您需要注意renderSize
的{{1}}和AVMutableVideoComposition
的转换。
要正确对齐它们,您需要以正确的度数翻译和旋转视频。要将其翻转过来,您需要将其旋转180度并将其转换为正确的坐标:
AVMutableVideoCompositionLayerInstruction
在我的情况下,...
videoComposition.renderSize = CGSizeMake(X, Y)
...
let translate = CGAffineTransformMakeTranslation(X, Y);
let rotate = CGAffineTransformRotate(translate, CGFloat(ANGLE_IN_RADIANS))
...
和平移设置为1280和720,180度旋转基本上是renderSize
弧度:
M_PI
最后,您仍然需要将此 videoComposition 添加到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]
,才能使转换生效。