如何将扬声器输出写入文件声音设备

How to write speaker output to a file sounddevice

有没有一种方法可以使用 python 库声音设备将扬声器的输出写入文件? 例如,如果我要通过我的计算机播放任何声音,它们将被写入 mp4/wav 文件。

这是一个解决方案:(查看评论)

import sounddevice as sd
from scipy.io.wavfile import write

fs = 44100  # Sample rate
seconds = 3  # Duration of recording
sd.default.device = 'digital output'  # Speakers full name here

myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait()  # Wait until recording is finished
write('output.wav', fs, myrecording)  # Save as WAV file 

以上代码来自:https://realpython.com/playing-and-recording-sound-python/#python-sounddevice_1 python 中关于声音以及如何录制和播放声音的完整教程。

您可以只指定输出设备 - 例如:

import sounddevice as REC
REC.default.device = 'Speakers (Realtek High Definition Audio), Windows DirectSound'

要获取 sounddevice 识别的所有声音设备,您可以在您的命令行中使用此命令:

this:    py -m sounddevice
or this: python -m sounddevice
or this: python3 -m sounddevice

我的工作代码:

from scipy.io.wavfile import wavWrite
import sounddevice as REC

# Recording properties
SAMPLE_RATE = 44100
SECONDS = 10

# Channels
MONO    = 1
STEREO  = 2

# Command to get all devices listed: py -m sounddevice 
# Device you want to record
REC.default.device = 'VoiceMeeter VAIO3 Output (VB-Audio VoiceMeeter VAIO3), Windows DirectSound'

print(f'Recording for {SECONDS} seconds')

# Starts recording
recording = REC.rec( int(SECONDS * SAMPLE_RATE), samplerate = SAMPLE_RATE, channels = MONO)
REC.wait()  # Waits for recording to finish

# Writes recorded data in to the wave file
wavWrite('recording.wav', SAMPLE_RATE, recording)