This article describes the Python programming, using the sndhdr module to recognize the audio format, as follows.
sndhdr module
Function Description: The sndhdr module provides an interface to detect the audio type.
The only API
The sndhdr module provides two functions (filename) and (filename). But they actually do the same thing. (I don't know what the point of writing an extra one is, the what function calls the whathdr function internally and returns the data intact)
In previous versions, the whathdr function returned a tuple, which has been changed to a namedtuple since Python 3.5. The returned tuple consists of five attributes: filetype, framerate, nchannels, nframes, and sampwidth.
Represents the audio format. Values are: 'aifc', 'aiff', 'au', 'hcom', 'sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', 'ul', or one of None. 'wav', '8svx', 'sb', 'ub', 'ul' or one of None. A few common formats are not supported in turn.
Represents the frame rate of the audio file. If the audio file is difficult to decode or unknown, this value will return 0.
Represents the number of channels. If the audio file is difficult to decode or unknown, this value will return 0.
Represents the number of frames. Returns -1 if it cannot be determined.
Represents the length (in bits) of the returned sample, the value is a multiple of 8, or returns A (A-LAW format), u (u-LAW format).
>>> import sndhdr >>> ('test.mp3') # Unable to detect, return None >>> ('') SndHeaders(filetype='wav', framerate=44100, nchannels=2, nframes=12630240, sampwidth=16)
Note: The module uses the aifc module when detecting AIFC and AIFF. The module uses the wave module to detect wav. These are both modules in the Python standard library.
Customized Inspection Process
Like the imghdr module, sndhdr internally uses a list of tests to maintain the detection functions. If you want to define your own detection process, you can do so by modifying the tests list.
>>> import sndhdr >>> [<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): # Customized detection functions ... print("Maybe mp3 or aac?") ... >>> ("test.mp3") >>> (final) # Add custom detection functions to the detection list >>> ("test.mp3") Maybe mp3 or aac?
The self-add detection function needs to receive two parameters h and f, h is the byte string used for detection and f is the file object.
Starting the sndhdr module from the command line
The format of sndhdr started in -m mode is also the same as imghdr, call python -m sndhdr [-r] file1 file2... in terminal. file can be a file or a folder, and the -r parameter represents recursive detection.
Desktop\test>python -m sndhdr test.mp3 test.mp3: None : SndHeaders(filetype='wav', framerate=44100, nchannels=2, nframes=12630240, sampwidth=16)
Summary:The internal structure of the sndhdr module in general is very similar to imghdr, and the design flaws are also very similar... The source code of the module is not much, and it's not very hard to read, so I suggest that interested readers try to read the source code.
The above is the full content of this article on Python with sndhdr module to identify the audio format details, I hope it will help you. Interested friends can continue to refer to this site:
《Python with imghdr module to recognize the image format example analysis》、《Python use base64 module for binary data encoding details》、《The hmac module generates a summary of messages with the key added.》
If there are deficiencies, welcome to leave a message to point out. Thank you friends for the support of this site!