使用 Python (sndhdr) 確定聲音檔案型別


Python 標準庫中的 sndhdr 模組提供了一些實用函式,用於讀取檔案中儲存的聲音資料的型別。這些函式返回一個命名元組,包含五個屬性。

filetype表示 'aifc'、'aiff'、'au'、'hcom'、'sndr'、'sndt'、'voc'、'wav'、'8svx'、'sb'、'ub' 或 'ul' 的字串。
framerate取樣率,如果未知或難以解碼,則為 0。
nchannels聲道數,如果無法確定或難以解碼,則為 0。
nframes幀數或 -1。
sampwidth每取樣位數,表示以位為單位的樣本大小,或 'A' 表示 A-LAW,或 'U' 表示 u-LAW。

sndhdr 模組中的函式

sndhdr.what()

此函式使用 whathdr() 確定儲存在檔案 filename 中的聲音資料的型別。如果成功,則返回上面描述的命名元組,否則返回 None。

sndhdr.whathdr()

此函式根據檔案頭確定儲存在檔案中的聲音資料的型別。此函式在成功時返回上面描述的命名元組,或 None。

示例

>>> import sndhdr
>>> sndhdr.whathdr("sample.wav")
SndHeaders(filetype = 'wav', framerate = 44100, nchannels = 1, nframes = 99999, sampwidth = 16)
>>> sndhdr.whathdr("sample.aiff")
SndHeaders(filetype = 'aiff', framerate = 8000, nchannels = 1, nframes = 271200, sampwidth = 16)
>>> sndhdr.whathdr("sample.au")
SndHeaders(filetype = 'au', framerate = 8000, nchannels = 1, nframes = 103397.0, sampwidth = 'U')

更新於: 2020-06-30

216 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.