今天要采集电脑声音,想通过AI去解析录制的音频文字,百度一下发现全都是收费的,干脆自己写一个,开干:
只录制电脑自己的声音(也可以将系统声音和麦克风叠加进来),分几步,第一步就要先解决声音去向问题,需要安装Voicemeeter https://vb-audio.com/Voicemeeter/
至于怎么用的可以去B站看看:https://www.bilibili.com/video/BV1Md4y1K7fS/
不过B站这个他没说清楚,他只是说了麦克风和多个切换,下面我来详细说一下关键位置:
1、需要先将电电脑的输出声卡改成Voicemeeter 的,不然待会儿用Voicemeeter 时会提示声卡忙
2、需要先配置Voicemeeter将电脑输出声音映射到输入,选择需要输出的合成的音频,里面的1,2可以选择和麦克风混合输出这个就看是否有需求了,我没需求。看下图箭头选择Voicemeeter的虚拟声卡从哪个电脑硬件输出声音:
然后就是采集,这里直接上自己写的python采集
import ffmpeg
import sounddevice as sd
import threading
import numpy as np
import time
from datetime import datetime
print(sd.query_devices())
# 录音设置
samplerate = 44100 # 采样率
channels = 2 # 声道数
device = 1 # 使用1号音频设备,需要根据实际设备进行调整
bit_rate = '64k' # 比特率
silence_threshold = 0.01 # 静音阈值
silence_duration = 5 # 静音持续时间 (秒)
recording = True
def record_stream():
global recording
# 格式化文件名为 "reYYYYMMDDHHMMSS.mp3"
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
output = f"re{current_time}.mp3"
process = (
ffmpeg
.input('pipe:0', format='s16le', acodec='pcm_s16le', ac=channels, ar=samplerate)
.output(output, **{'b:a': bit_rate, 'acodec': 'libmp3lame'})
.overwrite_output()
.run_async(pipe_stdin=True)
)
with sd.InputStream(samplerate=samplerate, device=device, channels=channels, dtype='int16') as stream:
last_sound_time = time.time()
print(f"Recording started. Press 's' to stop. Saving as {output}")
while recording:
audio_frame, _ = stream.read(1024)
audio_np = np.frombuffer(audio_frame, dtype=np.int16)
if np.linalg.norm(audio_np) > silence_threshold * 32768:
last_sound_time = time.time()
elif (time.time() - last_sound_time) > silence_duration:
print("15 seconds of silence detected. Stopping recording.")
break
process.stdin.write(audio_frame.tobytes())
process.stdin.close()
process.wait()
print(f"Recording saved to {output}")
def listen_for_stop():
global recording
input() # Wait for user to press 's'
recording = False
print("Stop command received.")
# Start recording in a separate thread
recording_thread = threading.Thread(target=record_stream)
recording_thread.start()
# Listen for stop command in the main thread
listen_for_stop()
# Wait for the recording thread to finish
recording_thread.join()
运行python voice.go 如果缺东西自己安装上就行,这里要说明,里面我加了一个没有声音自动停止录制的功能就是如果电脑没声音超过5秒自动停止,或者就是按回车自动停止并保存文件