我使用著名的PBJVision记录视频,然后必须合并在一起。我使用AVMutableComposition使用insertTimeRange(_:ofAsset:atTime:error:)组合视频。如果视频是用同一台相机拍摄的,效果很好。但是,例如,如果一个是用后面的摄像机拍摄的,那么使用前面的摄像机拍摄另一个,后者的视频就丢失了。看来只有音频被添加了。这是我的代码:
var error: NSError? = nil
let composition = AVMutableComposition()
var currentTime = kCMTimeZero
for (index, videoURL) in enumerate(videoURLS) {
let asset = AVURLAsset.assetWithURL(videoURL) as! AVAsset
let success = composition.insertTimeRange(CMTimeRange(start: kCMTimeZero, duration: asset.duration),
ofAsset: asset,
atTime: currentTime,
error: &error)
if !success {
if error != nil {
println("timerange isnert error - \(error?.localizedDescription)")
}
}
// add time till we get to the last video
if index < videoURLS.count - 1 {
currentTime = CMTimeAdd(currentTime, asset.duration)
}
}
let outputURL = fileSystemHelper.temporaryStorageURLForExportSession()
let fileManager = NSFileManager.defaultManager()
fileManager.removeItemAtURL(outputURL, error: &error)
if error != nil {
println("export session file removal error - \(error?.localizedDescription)")
}
let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)
exportSession.outputFileType = AVFileTypeMPEG4
exportSession.outputURL = outputURL
let start = CMTimeMake(0, 1)
let range = CMTimeRangeMake(start, composition.duration)
//exportSession.timeRange = range
exportSession.exportAsynchronouslyWithCompletionHandler { () -> Void in
switch exportSession.status {
case .Completed:
self.fileSystemHelper.copyFileAtURL(outputURL, toURL: finalURL)
self.appendURL = nil
// self.isRecording = false
completion()
case .Failed:
println("fail error - \(exportSession.error.localizedDescription)")
self.fileSystemHelper.removeFileAtURL(outputURL)
self.appendURL = nil
//self.isRecording = false
println("failed to mix")
// delegate?.videoCaptureDidFinishRecordingVideoAtURL(URL, appended: appendURL == nil)
default:
println("something else happened, check code")
}
}发布于 2015-05-28 11:23:10
就在我问完这个问题之后,我在附近散步了一夜,我自己找到了答案:)所以不同的相机有不同的最大分辨率,从而产生不同尺寸的帧,混淆构图对象。它使用第一个视频的大小,并忽略不同大小的视频的帧。因此,测试和看看什么是最好的分辨率AVCaptureSessionPreset支持的两个相机在特定的设备上。然后在您的视频捕获代码中使用预置,不要直接跳转到使用AVCaptureSessionPresetHigh。
我希望这对其他人也有帮助:)
https://stackoverflow.com/questions/30358827
复制相似问题