sndhdr — Determine type of sound file¶
Source code:Lib/sndhdr.py
Deprecated since version 3.11, will be removed in version 3.13:Thesndhdr module is deprecated(seePEP 594 for details and alternatives).
Thesndhdr provides utility functions which attempt to determine the typeof sound data which is in a file. When these functions are able to determinewhat type of sound data is stored in a file, they return anamedtuple(), containing five attributes: (filetype,framerate,nchannels,nframes,sampwidth). The value fortypeindicates the data type and will be one of the strings'aifc','aiff','au','hcom','sndr','sndt','voc','wav','8svx','sb','ub', or'ul'. Thesampling_rate will be either the actualvalue or0 if unknown or difficult to decode. Similarly,channels will beeither the number of channels or0 if it cannot be determined or if thevalue is difficult to decode. The value forframes will be either the numberof frames or-1. The last item in the tuple,bits_per_sample, will eitherbe the sample size in bits or'A' for A-LAW or'U' for u-LAW.
- sndhdr.what(filename)¶
Determines the type of sound data stored in the filefilename using
whathdr(). If it succeeds, returns a namedtuple as described above, otherwiseNoneis returned.Changed in version 3.5:Result changed from a tuple to a namedtuple.
- sndhdr.whathdr(filename)¶
Determines the type of sound data stored in a file based on the file header.The name of the file is given byfilename. This function returns a namedtuple asdescribed above on success, or
None.Changed in version 3.5:Result changed from a tuple to a namedtuple.
The following sound header types are recognized, as listed below with the return valuefromwhathdr(): andwhat():
Value | Sound header format |
|---|---|
| Compressed Audio Interchange Files |
| Audio Interchange Files |
| Au Files |
| HCOM Files |
| Sndtool Sound Files |
| Creative Labs Audio Files |
| Waveform Audio File Format Files |
| 8-Bit Sampled Voice Files |
| Signed Byte Audio Data Files |
| UB Files |
| uLAW Audio Files |
- sndhdr.tests¶
A list of functions performing the individual tests. Each function takes twoarguments: the byte-stream and an open file-like object. When
what()iscalled with a byte-stream, the file-like object will beNone.The test function should return a string describing the image type if the testsucceeded, or
Noneif it failed.
Example:
>>>importsndhdr>>>imghdr.what('bass.wav')'wav'>>>imghdr.whathdr('bass.wav')'wav'