Determine type of sound file using Python (sndhdr)

The sndhdr module in Python's standard library provides utility functions to determine the type and properties of sound files. It analyzes file headers to extract audio metadata without loading the entire file.

Return Value Structure

The functions return a namedtuple containing five attributes ?

Attribute Description
filetype String representing 'aifc', 'aiff', 'au', 'hcom', 'sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', or 'ul'
framerate Sampling rate (actual value or 0 if unknown)
nchannels Number of channels (or 0 if undetermined)
nframes Number of frames (or -1 if unknown)
sampwidth Bits per sample, 'A' for A-LAW, or 'U' for ?-LAW

sndhdr.what()

This function determines the sound file type by examining the file at the given path. It returns a namedtuple with file properties or None if the file type cannot be determined ?

import sndhdr

# Analyze a sound file
result = sndhdr.what("sample.wav")
print(result)

sndhdr.whathdr()

This function analyzes sound data from a file-like object or file header. It's useful when working with file objects or byte streams ?

import sndhdr

# Using with file object
with open("sample.wav", "rb") as f:
    result = sndhdr.whathdr(f)
    print(result)

Examples

Here are examples analyzing different audio file formats ?

import sndhdr

# Analyze WAV file
wav_info = sndhdr.what("sample.wav")
print(f"WAV: {wav_info}")

# Analyze AIFF file  
aiff_info = sndhdr.what("sample.aiff")
print(f"AIFF: {aiff_info}")

# Analyze AU file
au_info = sndhdr.what("sample.au")
print(f"AU: {au_info}")
WAV: SndHeaders(filetype='wav', framerate=44100, nchannels=1, nframes=99999, sampwidth=16)
AIFF: SndHeaders(filetype='aiff', framerate=8000, nchannels=1, nframes=271200, sampwidth=16)
AU: SndHeaders(filetype='au', framerate=8000, nchannels=1, nframes=103397, sampwidth='U')

Practical Usage

The sndhdr module is useful for validating audio files and extracting metadata before processing ?

import sndhdr
import os

def analyze_audio_file(filename):
    """Analyze an audio file and return its properties"""
    if not os.path.exists(filename):
        return f"File {filename} not found"
    
    info = sndhdr.what(filename)
    if info is None:
        return f"{filename} is not a recognized audio file"
    
    return {
        'filename': filename,
        'type': info.filetype,
        'sample_rate': info.framerate,
        'channels': info.nchannels,
        'frames': info.nframes,
        'sample_width': info.sampwidth
    }

# Example usage
files = ['audio.wav', 'music.mp3', 'invalid.txt']
for file in files:
    result = analyze_audio_file(file)
    print(f"{file}: {result}")
audio.wav: File audio.wav not found
music.mp3: File music.mp3 not found  
invalid.txt: File invalid.txt not found

Conclusion

The sndhdr module provides a lightweight way to identify audio file formats and extract metadata. Use sndhdr.what() for file paths and sndhdr.whathdr() for file objects to quickly analyze audio files without loading their content.

Updated on: 2026-03-25T05:27:04+05:30

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements