Python uses the sndhdr module to identify audio formats.
This article describes how to use the sndhdr module to identify audio formats in Python programming.
Sndhdr Module
Function Description: The sndhdr module provides an interface for detecting audio types.
Unique API
The sndhdr module provides two functions: sndhdr. what (filename) and sndhdr. whathdr (filename. But they actually have the same functions. (I don't know what it means to write another one. what function calls the whathdr function internally and returns the complete data)
In earlier versions, the whathdr function returns data of the tuples, and returns a namedtuple after Python3.5. The returned tuples include five attributes: filetype, framerate, nchannels, nframes, and sampwidth.
1. filetype indicates the audio format. Value: 'aifc ', 'aiff', 'au ', 'hcom', 'sndr', 'sndt ', 'voc', 'wav ', '8svx ', 'SB ', 'ub', 'ul ', or None. Some common formats are not supported.
2. framerate indicates the Frame Rate of the audio file. If the audio file is difficult to decode or is unknown, this value returns 0.
3. nchannels indicates the number of channels. If the audio file is difficult to decode or is unknown, this value returns 0.
4. nframes indicates the number of frames. -1 is returned if you are not sure.
5. sampwidth indicates the length (BITs) of the returned sample, A multiple of 8, or return A (A-LAW format), u (u-LAW format ).
>>> Import sndhdr >>> sndhdr.what('test}') # 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 when detecting aifc and AIFF. The wave module is used for wav detection. Both are Python standard library modules
Custom detection process
Like the imghdr module, sndhdr also uses a tests list maintenance detection function. If you want to customize the detection process, you can modify the tests list.
>>> Import sndhdr >>> sndhdr. tests [<function test_aifc at least>, <function test_au at 0x000001A99B527C80>, <function test_hcom at least>, <function test_voc at 0x000001A99B527D90>, <function test_wav at least>, <function test_8svx at least>, <function test_sndt at 0x000001A99B527F28>, <function test_sndr at 0x000001A99B521048>]> def final (h, f ): # Custom detection function... print ("Maybe mp3 or aac? ")... >>> Sndhdr. what ("testbench") >>> sndhdr. tests. append (final) # Add a custom detection function to the detection list> sndhdr. what ("testbench") Maybe mp3 or aac?
The self-added detection function needs to receive two parameters h and f. h is the byte string used for detection, and f is the file object.
Start the sndhdr module through the command line
Sndhdr is started in the same format as imghdr in-m mode. You can call python-m sndhdr [-r] file1 file2. File can be a file or folder, and the-r parameter indicates recursive detection.
Desktop\test>python -m sndhdr test.mp3 test.wavtest.mp3: Nonetest.wav: SndHeaders(filetype='wav', framerate=44100, nchannels=2, nframes=12630240, sampwidth=16)
Summary:The overall structure of the sndhdr module is very similar to that of imghdr, and the design defects are also very similar... The source code of the module is not much, and it is not difficult to understand it. It is recommended that interested readers try to read the source code.
The above is all the details about how Python uses the sndhdr module to identify audio formats. I hope it will be helpful to you. Interested friends can continue to refer to this site:
《Python uses the imghdr module to identify image format instance Parsing",《Python uses base64 for binary data encoding",《Hmac module generation detailed explanation of message digest with key added"
If you have any shortcomings, please leave a message. Thank you for your support!