如何使用AVCaptureSession记录Mac的内部声音而不是麦克风! [英] How to record my Mac's internal sound, not the microphone!, using AVCaptureSession?

查看:248
本文介绍了如何使用AVCaptureSession记录Mac的内部声音而不是麦克风!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个具有屏幕录制功能的简单macOS应用.

I am trying to implement a simple macOS app with screen recording capabilities.

我不想记录麦克风输入,而是要记录Mac扬声器发出的声音.示例:通过这种方式,我希望能够将YouTube视频录制到文件中.

I don't want to record a microphone input, but rather a sound that comes out of my Mac's speakers. Example: this way I want to be able to record a YouTube video to a file.

AVCaptureSession是否可能? Googling展示了捕获视频和麦克风的示例,而不捕获内部音频.

Is this possible with AVCaptureSession? Googling shows the examples that capture video and microphore, but not the internal audio.

这是捕获视频和麦克风所必须的工作代码.我必须进行哪些修改才能禁用麦克风并获得扬声器内部PC的声音?

Here is the working code that I have to capture video and microphone. What do I have to modify to disable the microphone and get the internal PC's sound that comes to the speakers?

session = AVCaptureSession()
session.sessionPreset = AVCaptureSession.Preset.high

movieFileOutput = AVCaptureMovieFileOutput()

let displayId: CGDirectDisplayID = CGDirectDisplayID(CGMainDisplayID())

let audioDevice = AVCaptureDevice.default(for: .audio)!

let audioInput = try! AVCaptureDeviceInput(device: audioDevice)

let videoInput: AVCaptureScreenInput = AVCaptureScreenInput(displayID: displayId)!

session.addInput(videoInput)
session.addInput(audioInput)
session.addOutput(movieFileOutput)

session.startRunning()
movieFileOutput.startRecording(to: self.destinationUrl, recordingDelegate: self)

推荐答案

我还没有找到简单的方法,但是事实证明,如果安装了另一个软件,可以使原始代码记录音频在我的计算机上: Soundflower .

I have not found an easy way to do it but it turns out that it is possible to make the original code record the audio given that there is another software installed on my machine: Soundflower.

Soundflower是MacOS的开源内核扩展,旨在创建虚拟音频输出设备,该设备也可以用作输入.

Soundflower is an open source kernel extension for MacOS, designed to create a virtual audio output device that can also act as an input.

鉴于已安装Soundflower,因此可以使用Applications / Utilities / Audio MIDI Setup应用程序配置macOS,以将音频发送到虚拟和真实音频设备.这样,上面的代码捕获了Soundflower中的音频,但是您仍然可以在常规音频输出设备上听到它.

Given that the Soundflower is installed, one can use configure the macOS using the Applications / Utilities / Audio MIDI Setup app to send the audio to both virtual and real audio devices. This way the code above captures the audio from the Soundflower but you can still hear it on your normal audio output device.

此处描述了Applications / Utilities / Audio MIDI Setup应用程序的设置:

The setup of the Applications / Utilities / Audio MIDI Setup application is described here: How can I send my computer's audio to multiple outputs?.

这篇关于如何使用AVCaptureSession记录Mac的内部声音而不是麦克风!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆