Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-如何录制系统音频(扬声器的输出)?_Python_Audio_System_Pyaudio - Fatal编程技术网

Python-如何录制系统音频(扬声器的输出)?

Python-如何录制系统音频(扬声器的输出)?,python,audio,system,pyaudio,Python,Audio,System,Pyaudio,我从上周开始一直在寻找这个。还尝试了pyaudio,当我使用它的另一个分叉时,系统音频与麦克风音频混合。我找不到其他模块,因此最终提出了这个问题 编辑: 此代码取自堆栈溢出。它记录扬声器输出,但输出与麦克风输入混合。 使用的pyaudio模块也来自fork: 使用 这仅记录“设备\u id”指定的设备的音频 导入pyaudio 输入波 chunk=1024#以1024个样本的块记录 样本_格式=pyaudio.paInt16#每个样本16位 通道=2 fs=44100#每秒记录44100个样本

我从上周开始一直在寻找这个。还尝试了pyaudio,当我使用它的另一个分叉时,系统音频与麦克风音频混合。我找不到其他模块,因此最终提出了这个问题

编辑:

此代码取自堆栈溢出。它记录扬声器输出,但输出与麦克风输入混合。 使用的pyaudio模块也来自fork:

使用
这仅记录“设备\u id”指定的设备的音频

导入pyaudio
输入波
chunk=1024#以1024个样本的块记录
样本_格式=pyaudio.paInt16#每个样本16位
通道=2
fs=44100#每秒记录44100个样本
秒=3
filename=“output.wav”
p=pyaudio.pyaudio()#创建到PortAudio的接口
#选择设备
打印(“可用设备:\n”)
对于范围内的i(0,p.获取设备计数()):
info=p.通过索引获取设备信息(i)
打印(str(info[“index”])+“:\t%s\n\t%s\n”%(info[“name”]、p.get_host_api_info_by_index(info[“hostApi”])[“name”]))
通过
#ToDo更改您的设备ID
设备id=7
设备信息=p.通过索引(设备id)获取设备信息
通道=设备信息[“maxInputChannels”]如果(设备信息[“maxOutputChannels”]<设备信息[“maxInputChannels”])其他设备信息[“maxOutputChannels”]
# https://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.Stream.__init__
stream=p.open(格式=sample\u格式,
频道=频道,
速率=整数(设备信息[“defaultSampleRate”]),
输入=真,
帧每缓冲区=块,
输入设备索引=设备信息[“索引”],
as_loopback=True
)
frames=[]#初始化数组以存储帧
打印(“\n记录”,设备id,…\n”)
#将数据分块存储3秒钟
对于范围内的i(0,int(fs/chunk*秒)):
data=stream.read(块)
frames.append(数据)
#停下来,关上小溪
stream.stop_stream()
stream.close()
#终止PortAudio接口
p、 终止()
打印('已完成录制')
#将记录的数据保存为WAV文件
wf=wave.open(文件名“wb”)
wf.设置通道(通道)
wf.setsampwidth(p.get_样本大小(样本格式))
设置帧率(fs)
wf.writeframes(b.)。连接(帧))
wf.close()

另外,请查看到目前为止您已经尝试过的内容。有密码吗?您遇到了什么错误?我添加了代码。
import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()
SPEAKERS = p.get_default_output_device_info()["hostApi"] #The modified part

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK,
                input_host_api_specific_stream_info=SPEAKERS,
                as_loopback = True) #The part I have modified

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS) + 1):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
import pyaudio
import wave

chunk = 1024  # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16  # 16 bits per sample
channels = 2
fs = 44100  # Record at 44100 samples per second
seconds = 3
filename = "output.wav"

p = pyaudio.PyAudio()  # Create an interface to PortAudio

#Select Device
print ( "Available devices:\n")
for i in range(0, p.get_device_count()):
    info = p.get_device_info_by_index(i)
    print ( str(info["index"]) +  ": \t %s \n \t %s \n" % (info["name"], p.get_host_api_info_by_index(info["hostApi"])["name"]))
    pass

#ToDo change to your device ID
device_id = 7
device_info = p.get_device_info_by_index(device_id)
channels = device_info["maxInputChannels"] if (device_info["maxOutputChannels"] < device_info["maxInputChannels"]) else device_info["maxOutputChannels"]
# https://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.Stream.__init__
stream = p.open(format=sample_format,
                channels=channels,
                rate=int(device_info["defaultSampleRate"]),
                input=True,
                frames_per_buffer=chunk,
                input_device_index=device_info["index"],
                as_loopback=True
                )

frames = []  # Initialize array to store frames

print('\nRecording', device_id, '...\n')

# Store data in chunks for 3 seconds
for i in range(0, int(fs / chunk * seconds)):
    data = stream.read(chunk)
    frames.append(data)

# Stop and close the stream 
stream.stop_stream()
stream.close()
# Terminate the PortAudio interface
p.terminate()

print('Finished recording')

# Save the recorded data as a WAV file
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()