[Python] .WAV Audio Produced by PyDub AudioSegment.split_on_silence() has no RIFF ID when...

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently taking a large audio file and splitting it based on silence using PyDub's split_on_silence function. However, when I go to open this file using wave.open I get an error saying the .wav file has no RIFF id. The source file I am using is a .wav already with a sample rate of 16000 khz. The source file IS readable via wave.open without issue. The issue just arises on the chunk files I create via split_on_silence().

    I am able to open the audio chunk in a media player and listen to it without problem. Here is my code and error output:

    import numpy as np
    import pandas as pd
    import shutil

    from typing import Union
    from pathlib import Path
    from pydub import AudioSegment, effects
    from pydub.silence import split_on_silence

    def split_audio(audio_file_path: Union[Path, str], split_audio_output_dir: Union[Path, str]):

    audio_file_name = Path(audio_file_path).name
    chunks_output_path = Path(split_audio_output_dir).joinpath(audio_file_name)

    if Path(chunks_output_path).is_dir(): # if it is already a path, clear it and recreate it
    shutil.rmtree(chunks_output_path)
    Path(chunks_output_path).mkdir()
    else:
    Path(chunks_output_path).mkdir()

    sound = AudioSegment.from_wav(audio_file_path)
    dBFS = sound.dBFS # get decibels relative to full scale


    sound_chunks = split_on_silence(sound,
    min_silence_len = 400, # measured in ms
    silence_thresh = dBFS -30 # if DBFS goes 30 below the files standard dBFS it will be "silence"
    # keep_silence = 500 # keeps silence of 250ms at beginning and end of audio to prevent abrupt start/stop of audio
    )

    for i, chunk in enumerate(sound_chunks):

    silence_chunk = AudioSegment.silent(duration = 1500, frame_rate = 16000) # 1.5 seconds of silence appended to beginning and end of audio chunk
    chunk = silence_chunk + chunk + silence_chunk

    chunk_normalized = effects.normalize(chunk)
    print("Exporting audio chunk {0}.wav for parent file {audio_file_name}".format(i, audio_file_name = audio_file_name))
    chunk_normalized.export(chunks_output_path.joinpath("audio_segment_{0}.wav".format(i)))


    Now when I go to read one of these audio files I get the following error:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import os
    import wave
    # load the .wav audio signal
    audio_signal = wave.open(r"C:\src\audio_processing\data\processed\split_audio\convertedN216MS_KLGB-Twr-Apr-15-2020-0200Z_Final.mp3.wav\audio_segment_2.wav", 'rb')
    sample_rate = audio_signal.getframerate()
    audio_len = audio_signal.getnframes() * (1 / sample_rate)

    audio_buffer = np.frombuffer(audio_signal.readframes(audio_signal.getnframes()), np.int16)

    time = np.linspace(0, audio_len, num = len(audio_buffer))

    print("This file has {} audio channel(s)".format(audio_signal.getnchannels()))

    # Plot
    plt.figure(1)
    plt.title("Audio file")
    plt.plot(time, audio_buffer)
    plt.show()

    ---------------------------------------------------------------------------
    Error Traceback (most recent call last)
    <ipython-input-23-9b03ea721d99> in <module>
    3
    4 # load the .wav audio signal
    ----> 5 audio_signal = wave.open(r"C:\src\audio_processing\data\processed\split_audio\convertedN216MS_KLGB-Twr-Apr-15-2020-0200Z_Final.mp3.wav\audio_segment_2.wav", 'rb')
    6 sample_rate = audio_signal.getframerate()
    7 audio_len = audio_signal.getnframes() * (1 / sample_rate)

    C:\Python\python37\lib\wave.py in open(f, mode)
    508 mode = 'rb'
    509 if mode in ('r', 'rb'):
    --> 510 return Wave_read(f)
    511 elif mode in ('w', 'wb'):
    512 return Wave_write(f)

    C:\Python\python37\lib\wave.py in __init__(self, f)
    162 # else, assume it is an open file object already
    163 try:
    --> 164 self.initfp(f)
    165 except:
    166 if self._i_opened_the_file:

    C:\Python\python37\lib\wave.py in initfp(self, file)
    129 self._file = Chunk(file, bigendian = 0)
    130 if self._file.getname() != b'RIFF':
    --> 131 raise Error('file does not start with RIFF id')
    132 if self._file.read(4) != b'WAVE':
    133 raise Error('not a WAVE file')

    Error: file does not start with RIFF id

    Continue reading...

Compartilhe esta Página