python 怎么录制系统声音不只是麦克风声音

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 怎么录制系统声音不只是麦克风声音相关的知识,希望对你有一定的参考价值。

找到的代码只能录制麦克风声音

#我可以帮你写一段代码,能够录音形成wav文件,不过要分析录音文件的波形,你可以另外找#工具,比如cooledit,也很方便。
from sys import byteorder
from array import array
from struct import pack

import pyaudio
import wave

THRESHOLD = 500
CHUNK_SIZE = 1024
FORMAT = pyaudio.paInt16
RATE = 44100

def is_silent(snd_data):
"Returns 'True' if below the 'silent' threshold"
return max(snd_data) < THRESHOLD

def normalize(snd_data):
"Average the volume out"
MAXIMUM = 16384
times = float(MAXIMUM)/max(abs(i) for i in snd_data)

r = array('h')
for i in snd_data:
r.append(int(i*times))
return r

def trim(snd_data):
"Trim the blank spots at the start and end"
def _trim(snd_data):
snd_started = False
r = array('h')

for i in snd_data:
if not snd_started and abs(i)>THRESHOLD:
snd_started = True
r.append(i)

elif snd_started:
r.append(i)
return r

# Trim to the left
snd_data = _trim(snd_data)

# Trim to the right
snd_data.reverse()
snd_data = _trim(snd_data)
snd_data.reverse()
return snd_data

def add_silence(snd_data, seconds):
"Add silence to the start and end of 'snd_data' of length 'seconds' (float)"
r = array('h', [0 for i in xrange(int(seconds*RATE))])
r.extend(snd_data)
r.extend([0 for i in xrange(int(seconds*RATE))])
return r

def record():
"""
Record a word or words from the microphone and
return the data as an array of signed shorts.

Normalizes the audio, trims silence from the
start and end, and pads with 0.5 seconds of
blank sound to make sure VLC et al can play
it without getting chopped off.
"""
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=1, rate=RATE,
input=True, output=True,
frames_per_buffer=CHUNK_SIZE)

num_silent = 0
snd_started = False

r = array('h')

while 1:
# little endian, signed short
snd_data = array('h', stream.read(CHUNK_SIZE))
if byteorder == 'big':
snd_data.byteswap()
r.extend(snd_data)

silent = is_silent(snd_data)

if silent and snd_started:
num_silent += 1
elif not silent and not snd_started:
snd_started = True

if snd_started and num_silent > 30:
break

sample_width = p.get_sample_size(FORMAT)
stream.stop_stream()
stream.close()
p.terminate()

r = normalize(r)
r = trim(r)
r = add_silence(r, 0.5)
return sample_width, r

def record_to_file(path):
"Records from the microphone and outputs the resulting data to 'path'"
sample_width, data = record()
data = pack('<' + ('h'*len(data)), *data)

wf = wave.open(path, 'wb')
wf.setnchannels(1)
wf.setsampwidth(sample_width)
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()

if __name__ == '__main__':
print("please speak a word into the microphone")
record_to_file('demo.wav')
print("done - result written to demo.wav")
参考技术A 大哥,能把只录制麦克风声音的代码放出来吗?就是要找这个没找到。不要系统声音的。

如何使用 Java 从 USB 麦克风录制声音

【中文标题】如何使用 Java 从 USB 麦克风录制声音【英文标题】:How to Record Sound From a USB mic with Java 【发布时间】:2012-09-25 00:23:49 【问题描述】:

现在,我可以通过普通线路获得声音 - 通过麦克风端口在麦克风中。但是,我不知道如何通过 Java 直接访问硬件以获得声音。有人可以帮忙吗?

基本上,我需要它录制一段时间,然后将输入的声音录制到 .mp3 或任何文件中。

【问题讨论】:

我认为gtgaxiola的想法是正确的。您不需要访问硬件,但它是驱动程序。 JavaSound Reosurces 提供了很多这样的例子:jsresources.org/faq_audio.html#capture_source 【参考方案1】:

你可以看看Java Sound API

【讨论】:

以上是关于python 怎么录制系统声音不只是麦克风声音的主要内容,如果未能解决你的问题,请参考以下文章

为啥录屏没声音

打开打开录制声音后,为啥录屏还是没声音?

手机录制视频没有声音怎么办

为啥录制出来的视频没有声音?

三星手机无法录制声音另一个应用程序正在使用麦克风?

为啥我的屏幕录像无声音或者有杂音