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

时间:2020-06-26 13:46:58

标签: python python-3.x audio audio-recording python-sounddevice

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

2 个答案:

答案 0 :(得分: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声音教程,以及如何录制和播放声音。

答案 1 :(得分: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)