1

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

4

2 回答 2

0

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

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)
于 2021-07-13T21:37:00.147 回答
-1

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

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声音教程以及如何录制和播放声音。

于 2020-06-26T14:23:10.353 回答