【问题标题】:How to write speaker output to a file sounddevice如何将扬声器输出写入文件 sounddevice
【发布时间】:2020-06-26 13:46:58
【问题描述】:

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

【问题讨论】:

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


【解决方案1】:

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

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)

【讨论】:

    【解决方案2】:

    这是一个解决方案:(参见 cmets)

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

    【讨论】:

    • 这有助于写入 wav 文件,但它通过我的麦克风录制声音,而不是我的计算机发出的声音,就像我在上面播放视频一样;我的扬声器发出的声音。
    • @TheFluffDragon9 我编辑了答案以将输入设备更改为您的扬声器。如果这不起作用,我将删除我的答案。
    • 如何找到演讲者的全名?有没有办法打印设备列表或其他东西?感谢您所有的帮助! 'digital output' 之后的句号是顺便说一句吗?
    • @TheFluffDragon9 如果您使用 Windows,您可以转到声音设置并查看输出设备的名称,即您要输入的设备。
    • 很抱歉一直问问题,但我只是想把这件事做对。如果this 是我的声音设置所说的并且我想要底部扬声器,那么我的代码行是什么样的?
    猜你喜欢
    • 2010-11-04
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多