我想使用iPhone的内置摄像头录制视频,然后在保存之前添加文本轨道,但我似乎遇到了一些问题。
extension VideoRecorderViewController: UIImagePickerControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if mediaType!.isEqualToString(kUTTypeMovie as String) {
let mixComposition = AVMutableComposition(URL: info[UIImagePickerControllerMediaURL] as! NSURL)
print("track count: \(mixComposition.tracks.count)")
print("mixComposition is \(mixComposition.readable)")
// TODO insert tracks here
// mixComposition.insertTimeRange(CMTimeRangeMake(kCMTimeZero,sourceAsset.duration), ofTrack: assetTrack, atTime: insertTime, error: nil)
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let savePath = documentDirectory.stringByAppendingPathComponent("extratrack-1234.mov")
let url = NSURL(fileURLWithPath: savePath)
let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
if (exporter != nil) {
exporter!.outputURL = url
exporter!.outputFileType = AVFileTypeMPEG4
exporter!.exportAsynchronouslyWithCompletionHandler() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("dispatch called")
self.exportDidFinish(exporter!)
})
}
}
}
}
}
尝试此代码时,mixComposition.readable
的输出为false
,跟踪计数为零,当我们到达导出器时,实例为零,可能是由于AVMutableComposition
未加载参考文件,正如我所料。
有没有办法可以加载现有视频,添加曲目然后保存。我想避免从头开始创建文件,然后单独重新添加曲目,并确保在此过程中不丢失任何元数据。
注意,我想将字幕轨道添加为单独的轨道,而不是复合轨道。