Python USES sndhdr module to identify audio format details

  • 2020-07-21 08:37:05
  • OfStack

This paper mainly introduces Python programming, using sndhdr module to identify audio format related content, specific as follows.

sndhdr module

Function description: THE sndhdr module provides an interface for detecting audio types.

Only 11 API

The sndhdr module provides sndhdr. what(filename) and sndhdr. whathdr(filename) functions. But they actually do the same thing. (The what function calls the whathdr function internally and returns the data completely.)

In the previous version, the whathdr function returned tuple type data, but after the Python3.5 version returned 1 namedtuple. The tuple returned contains five attributes: filetype, framerate, nchannels, nframes, and sampwidth.

1.filetype stands for audio format. The values are: 'aifc', 'aiff',' au', 'hcom',' sndr', 'sndt',' voc', 'wav',' 8svx', 'ub',' ul' or one of the None's. Several common formats are not supported.
2.framerate represents the frame rate of the audio file. If the audio file is difficult to decode or unknown, this value returns 0.
3.nchannels represents the number of channels. If the audio file is difficult to decode or unknown, this value returns 0.
4.nframes stands for frame number. If not, it returns -1.
5.sampwidth represents the length (bit) of the returned sample, with a value of a multiple of 8, or A (ES58en-ES59en format), u (ES61en-ES62en format).


>>> import sndhdr
>>> sndhdr.what('test.mp3') #  Unable to detect, return None
>>> sndhdr.what('test.wav')
SndHeaders(filetype='wav', framerate=44100, nchannels=2, nframes=12630240, sampwidth=16)

Note: THE aifc module is used in the detection of AIFC and AIFF. wave module was used to detect wav. Both of these are modules in the Python standard library

Customize the detection process

Like imghdr module 1, sndhdr also USES an tests list maintenance detection function internally. If you want to define your own detection process, you can do so by modifying the tests list.


>>> import sndhdr
>>> sndhdr.tests
[<function test_aifc at 0x000001A99B527BF8>, <function test_au at 0x000001A99B527C80>, <
function test_hcom at 0x000001A99B527D08>, <function test_voc at 0x000001A99B527D90>, <f
unction test_wav at 0x000001A99B527E18>, <function test_8svx at 0x000001A99B527EA0>, <fu
nction test_sndt at 0x000001A99B527F28>, <function test_sndr at 0x000001A99B521048>]
>>> def final(h, f): #  Customize the detection function 
... print("Maybe mp3 or aac?")
...
>>> sndhdr.what("test.mp3")
>>> sndhdr.tests.append(final) #  Add custom detection functions to the detection list 
>>> sndhdr.what("test.mp3")
Maybe mp3 or aac?

Adding the detection function by yourself requires receiving two parameters, h and f. h is the byte string used for detection, and f is the file object.

Start the sndhdr module from the command line

The format of sndhdr launched in -ES94en mode is the same as that of imghdr1. python-m sndhdr [-ES99en] file1 file2... Just fine. file can be a file or folder, and the -ES103en parameter represents recursive detection.


Desktop\test>python -m sndhdr test.mp3 test.wav
test.mp3: None
test.wav: SndHeaders(filetype='wav', framerate=44100, nchannels=2, nframes=12630240, sampwidth=16)

Summary: the overall structure of sndhdr module is very similar to imghdr, and the design defects are also very similar... The source code of the module is not much, nor is it difficult to read, it is suggested that interested readers can try to read the source code.

The above is all about Python using sndhdr module to identify audio format in detail, I hope to help you. Those who are interested can continue to see this site:

Example Analysis of Python using imghdr Module to Identify Image Format, Details of Python using base64 Module to Encode binary data, Details of hmac Module to Generate Message Summary with key added

If there is any deficiency, please let me know. Thank you for your support!


Related articles: