我想以編程方式錄製在我的筆記本電腦中使用python播放的聲音。我發現PyAudio
並用以下程序完成任務上來:在python中錄製輸出聲音
import pyaudio, wave, sys
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = sys.argv[1]
p = pyaudio.PyAudio()
channel_map = (0, 1)
stream_info = pyaudio.PaMacCoreStreamInfo(
flags = pyaudio.PaMacCoreStreamInfo.paMacCorePlayNice,
channel_map = channel_map)
stream = p.open(format = FORMAT,
rate = RATE,
input = True,
input_host_api_specific_stream_info = stream_info,
channels = CHANNELS)
all = []
for i in range(0, RATE/chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
stream.close()
p.terminate()
data = ''.join(all)
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
的問題是我的耳機插孔連接到麥克風插孔。我試圖更換這些線路:
input = True,
input_host_api_specific_stream_info = stream_info,
這些:
output = True,
output_host_api_specific_stream_info = stream_info,
但後來我得到這個錯誤:
Traceback (most recent call last):
File "./test.py", line 25, in
data = stream.read(chunk)
File "/Library/Python/2.5/site-packages/pyaudio.py", line 562, in read
paCanNotReadFromAnOutputOnlyStream)
IOError: [Errno Not input stream] -9975
有沒有辦法來實例化PyAudio流,使其輸入從計算機的輸出,我不必將耳機插孔連接到麥克風?有沒有更好的方法去做這件事?我寧願粘着W /一個Python應用程序,並避免可可。
聲音已經在您的筆記本電腦中? 「另存爲...」是Macintosh的問題。 – 2010-01-12 12:13:12
有沒有簡單的方法來保存在你的瀏覽器中的Flash播放器中播放的文件(我猜他們是.flv文件)?我知道很多次,像youtube和vimeo這樣的網站都有鏈接來下載.flv文件,但很多時候他們沒有鏈接來執行此操作。 – aaronstacy 2010-01-12 15:57:23