PyDub AudioSegment.split_on_silence() 生成的 .WAV 音频在使用 wave.open 读取时没有 RIFF ID
.WAV Audio Produced by PyDub AudioSegment.split_on_silence() has no RIFF ID when reading with wave.open
我目前正在使用 PyDub 的 split_on_silence 功能获取一个大型音频文件并根据静音对其进行拆分。但是,当我使用 wave.open 打开此文件时,我收到一条错误消息,指出 .wav 文件没有 RIFF id。我使用的源文件是一个 .wav 文件,采样率为 16000 khz。源文件可通过 wave.open 毫无问题地读取。这个问题只出现在我通过 split_on_silence().
创建的块文件上
我可以在媒体播放器中打开音频块并毫无问题地收听。这是我的代码和错误输出:
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)))
现在,当我阅读其中一个音频文件时,出现以下错误:
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
好吧,我很快就找到了答案。
在我的导出调用中:
chunk_normalized.export(chunks_output_path.joinpath("audio_segment_{0}.wav".format(i)))
我需要像这样包含格式 = 'wav' 参数:
chunk_normalized.export(chunks_output_path.joinpath("audio_segment_{0}.wav".format(i)), format = 'wav')
我目前正在使用 PyDub 的 split_on_silence 功能获取一个大型音频文件并根据静音对其进行拆分。但是,当我使用 wave.open 打开此文件时,我收到一条错误消息,指出 .wav 文件没有 RIFF id。我使用的源文件是一个 .wav 文件,采样率为 16000 khz。源文件可通过 wave.open 毫无问题地读取。这个问题只出现在我通过 split_on_silence().
创建的块文件上我可以在媒体播放器中打开音频块并毫无问题地收听。这是我的代码和错误输出:
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)))
现在,当我阅读其中一个音频文件时,出现以下错误:
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
好吧,我很快就找到了答案。
在我的导出调用中:
chunk_normalized.export(chunks_output_path.joinpath("audio_segment_{0}.wav".format(i)))
我需要像这样包含格式 = 'wav' 参数:
chunk_normalized.export(chunks_output_path.joinpath("audio_segment_{0}.wav".format(i)), format = 'wav')