Ignore the invalid audio frames (#82)
This commit is contained in:
@@ -36,6 +36,7 @@ def decode_audio(input_file: Union[str, BinaryIO], sampling_rate: int = 16000):
|
||||
|
||||
with av.open(input_file, metadata_errors="ignore") as container:
|
||||
frames = container.decode(audio=0)
|
||||
frames = _ignore_invalid_frames(frames)
|
||||
frames = _group_frames(frames, 500000)
|
||||
frames = _resample_frames(frames, resampler)
|
||||
|
||||
@@ -50,6 +51,18 @@ def decode_audio(input_file: Union[str, BinaryIO], sampling_rate: int = 16000):
|
||||
return audio.astype(np.float32) / 32768.0
|
||||
|
||||
|
||||
def _ignore_invalid_frames(frames):
|
||||
iterator = iter(frames)
|
||||
|
||||
while True:
|
||||
try:
|
||||
yield next(iterator)
|
||||
except StopIteration:
|
||||
break
|
||||
except av.error.InvalidDataError:
|
||||
continue
|
||||
|
||||
|
||||
def _group_frames(frames, num_samples=None):
|
||||
fifo = av.audio.fifo.AudioFifo()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user