我正在尝试从捕获会话中镜像录制的视频。前置摄像头的视频预览显示了镜像版本,但是,当我去保存文件并播放时,捕获的视频实际上是镜像的。我正在使用Apple的AVCam演示作为参考,似乎无法弄清楚!请帮忙。
我尝试创建AVCaptureConnection并尝试设置.isVideoMirrored参数。但是,出现此错误:
无法添加到会话中,因为源和目标媒体类型不兼容'
我原本以为镜像视频会容易得多。我想我可能建立了错误的连接。当我调用.canAddConnection检查时,下面的代码实际上并没有“添加连接”。
var captureSession: AVCaptureSession!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
captureSession = AVCaptureSession()
//Setup Camera
if let dualCameraDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .front) {
defaultVideoDevice = dualCameraDevice
} else if let frontCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front) {
// If the rear wide angle camera isn't available, default to the front wide angle camera.
defaultVideoDevice = frontCameraDevice
}
guard let videoDevice = defaultVideoDevice else {
print("Default video device is unavailable.")
// setupResult = .configurationFailed
captureSession.commitConfiguration()
return
}
let videoDeviceInput = try AVCaptureDeviceInput(device: videoDevice)
if captureSession.canAddInput(videoDeviceInput) {
captureSession.addInput(videoDeviceInput)
}
let movieOutput = AVCaptureMovieFileOutput()
//Video Input variable for AVCapture Connection
let videoInput: [AVCaptureInput.Port] = videoDeviceInput.ports
if captureSession.canAddOutput(movieOutput) {
captureSession.beginConfiguration()
captureSession.addOutput(movieOutput)
captureSession.sessionPreset = .medium
然后,我尝试设置AVCapture连接并尝试设置用于镜像的参数。请告诉我是否有更简单的方法来镜像输出/回放。
avCaptureConnection = AVCaptureConnection(inputPorts: videoInput, output: movieOutput)
avCaptureConnection.isEnabled = true
//Mirror the capture connection?
avCaptureConnection.automaticallyAdjustsVideoMirroring = false
avCaptureConnection.isVideoMirrored = false
//Check if we can add a connection
if captureSession.canAddConnection(avCaptureConnection) {
//Add the connection
captureSession.addConnection(avCaptureConnection)
}
captureSession.commitConfiguration()
self.movieOutput = movieOutput
setupLivePreview()
}
}
在代码中与IBAaction连接的其他地方,我初始化了录音
// Start recording video to a temporary file.
let outputFileName = NSUUID().uuidString
let outputFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!)
print("Recording in tap function")
movieOutput.startRecording(to: URL(fileURLWithPath: outputFilePath), recordingDelegate: self)
我认为我没有正确使用AVCaptureConnection,尤其是因为错误指出媒体类型不兼容。如果有实现此功能的正确方法,请告诉我。也可以听取建议,以更轻松地镜像播放。谢谢!