開始使用免費開始

錄下我們需要的音訊

有時你不會想要整個音訊檔。record() 方法的 durationoffset 參數可以幫上忙。

在探索資料集後,你發現有一個檔案(已匯入為 nothing_at_end)在結尾有「30 秒的靜音」(https://assets.datacamp.com/production/repositories/4637/datasets/ca799cf2a7b093c06e1a5ae1dd96a49d48d65efa/30-seconds-of-nothing-16k.wav),以及一個客服通話檔(已匯入為 out_of_warranty)在開頭有「3 秒的雜訊」(https://assets.datacamp.com/production/repositories/4637/datasets/dbc47d8210fdf8de42b0da73d1c2ba92e883b2d2/static-out-of-warranty.wav)。

設定 durationoffset 代表 record() 會從 offset 開始,錄到最多 duration 秒的音訊。兩者的單位都是秒。

本練習屬於課程

Python 的口語語言處理

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Convert AudioFile to AudioData
with nothing_at_end as source:
    nothing_at_end_audio = recognizer.record(source,
                                             duration=____,
                                             offset=None)

# Transcribe AudioData to text
text = recognizer.recognize_google(nothing_at_end_audio,
                                   language="en-US")

print(text)
編輯並執行程式碼