code:
"""
資料來源:https://www.analyticsvidhya.com/blog/2021/10/build-a-voice-recorder-using-python/
pip install sounddevice
pip install scipy==1.8.0 #'1.11.1' 也可
"""
import sounddevice # 匯入sounddevice
from scipy.io.wavfile import write # 匯入 scipy.io.wavfile.write
fs= 44100 # 設定聲音採樣頻率 DVD中的採樣頻率 HZ
second = int(input("輸入要錄音的秒數: ")) # 輸入要錄音的秒數 要打整數
print("Recording.....")
# https://pypi.org/project/sounddevice/0.3.2/
record_voice = sounddevice.rec(
frames=int ( second * fs ) , # 錄音秒數 * 採樣頻率
samplerate = fs , # 採樣頻率
channels = 2 ) # 錄音兩個聲道
sounddevice.wait() # 等待錄音完成 才會執行到下一行, Ctrl+C可以中斷錄音
write("out.wav",fs,record_voice) # 寫入檔案
print("完成, 輸出到 out.wav") # 完成輸出到 out.wav
輸出結果:
sounddevice.rec?
from scipy.io.wavfile import write
write?
推薦hahow線上學習python: https://igrape.net/30afN
近期留言