You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在不加载整个文件的情况下获取WAV文件的采样率?

Efficiently Get WAV Sample Rate Without Loading Entire File

Great question—dealing with large WAV files can be frustrating when you only need metadata like the sample rate instead of the full waveform. Here are three reliable approaches to do this efficiently:

1. Use Python's Built-in wave Module (No Extra Dependencies)

The standard library's wave module lets you inspect WAV file metadata without loading the entire audio data. It's lightweight and perfect for this use case:

import wave

def get_sample_rate(file_path):
    with wave.open(file_path, 'rb') as wav_file:
        return wav_file.getframerate()

# Example usage
sample_rate = get_sample_rate("large_file.wav")
print(f"Sample rate: {sample_rate} Hz")

This opens the file in read-binary mode, fetches the sample rate via getframerate(), and closes the file automatically thanks to the with statement. No waveform data is loaded into memory.

2. Manually Parse the WAV File Header

If you want to avoid using any libraries at all, you can directly read and parse the WAV file's RIFF header. The sample rate is stored in bytes 24-27 (little-endian format) for standard PCM WAV files:

def get_sample_rate_manual(file_path):
    with open(file_path, 'rb') as f:
        # Skip RIFF chunk header (first 12 bytes)
        f.seek(12)
        # Skip fmt subchunk header (first 8 bytes of fmt chunk)
        f.seek(8, 1)
        # Read sample rate (4 bytes, little-endian)
        sample_rate = int.from_bytes(f.read(4), byteorder='little')
        return sample_rate

# Example usage
sample_rate = get_sample_rate_manual("large_file.wav")
print(f"Sample rate: {sample_rate} Hz")

Note: This works for standard PCM WAV files. If you're dealing with non-PCM formats (like compressed WAVs), you might need to adjust the parsing logic to handle different subchunk structures.

3. Use librosa (For More Advanced Audio Workflows)

If you're already working with audio processing libraries, librosa has a dedicated function to fetch the sample rate without loading the full audio:

import librosa

sample_rate = librosa.get_samplerate("large_file.wav")
print(f"Sample rate: {sample_rate} Hz")

This function is optimized to read only the necessary header data, making it efficient even for huge files. Keep in mind you'll need to install librosa first (pip install librosa).


内容的提问来源于stack exchange,提问作者Mees de Vries

火山引擎 最新活动