我正在尝试在录制视频时添加音频。我知道我必须单独录制并在保存文件时将它们合并,但我无法弄清楚语法或添加音频设备的位置以及组合文件的位置。
大多数可用信息基于较旧的弃用语法或仅处理一个主题(视频,音频等)。
我有不同的功能来准备会话,显示会话和记录会话,并保存看起来像这段代码的outputFile,该代码基于“Zero2Launch”https://www.youtube.com/watch?v=7TqXrMnfJy8
的教程但是对于我的生活,我无法弄清楚如何处理音频。
func setupCaptureSession() {
captureSession.sessionPreset = AVCaptureSession.Preset.hd1920x1080
}
func setupDevice() {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
let devices = deviceDiscoverySession.devices
for device in devices {
if device.position == AVCaptureDevice.Position.back {
backCamera = device
} else if device.position == AVCaptureDevice.Position.front {
frontCamera = device
}
}
currentDevice = backCamera
}
func setupInputOutput() {
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice!)
captureSession.addInput(captureDeviceInput)
videoFileOutput = AVCaptureMovieFileOutput()
captureSession.addOutput(videoFileOutput!)
} catch {
print(error)
}
}
func setupPreviewLayer() {
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraPreviewLayer?.frame = self.view.frame
self.view.layer.insertSublayer(cameraPreviewLayer!, at: 0)
}
func startRunningCaptureSession() {
captureSession.startRunning()
}
func recordVideo() {
if !isRecording {
isRecording = true
UIView.animate(withDuration: 0.5, delay: 0.0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: { () -> Void in
self.recordButton.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
}, completion: nil)
let outputPath = NSTemporaryDirectory() + "output.mov"
let outputFileURL = URL(fileURLWithPath: outputPath)
videoFileOutput?.startRecording(to: outputFileURL, recordingDelegate: self)
} else {
isRecording = false
UIView.animate(withDuration: 0.5, delay: 1.0, options: [], animations: { () -> Void in
self.recordButton.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}, completion: nil)
recordButton.layer.removeAllAnimations()
videoFileOutput?.stopRecording()
}
}
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
if error != nil {
print(error)
return
}
UISaveVideoAtPathToSavedPhotosAlbum(outputFileURL.relativePath, self, nil, nil)
}
答案 0 :(得分:5)
您无需单独保存音频并合并它们。您只需将audioDeviceInput添加到会话输入中,AVCaptureMovieFileOutput将为您完成所有操作。尝试更改setupInputOutput方法
func setupInputOutput() {
guard let audioDevice = AVCaptureDevice.default(for: .audio) else { return }
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice!)
let audioDeviceInput = try AVCaptureDeviceInput(device: audioDevice)
captureSession.addInput(captureDeviceInput)
captureSession.addInput(audioDeviceInput)
videoFileOutput = AVCaptureMovieFileOutput()
captureSession.addOutput(videoFileOutput!)
} catch {
print(error)
}
}
使用AVCaptureVideoDataOutput,AVCaptureAudioDataOutput和AVAssetWriter类录制视频和音频有第二种方法,但有点复杂。如果您不需要实时更改视频或音频缓冲区,最好使用AVCaptureMovieFileOutput