Sunday, September 20, 2020

Python: Find and Delete Corrupted Audio Files

import os
import wave
import glob

def compare_header_and_size(wav_filename):
    with wave.open(wav_filename, 'r') as fin:
            header_fsize = (fin.getnframes() * fin.getnchannels() * fin.getsampwidth()) + 44
    file_fsize = os.path.getsize(wav_filename)
    return header_fsize != file_fsize

print('The following files are corrupted:')
for f in  glob.glob("_background_noise_/*.wav"):
    if compare_header_and_size(f):
        print(f)
        os.remove(f)

No comments:

Post a Comment