Last active
April 23, 2024 19:05
-
-
Save wlinds/d5e129426ffd9de70195966340448692 to your computer and use it in GitHub Desktop.
Get distributions of sample rates among WAV files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import soundfile as sf # https://github.com/bastibe/python-soundfile | |
def get_wav_sample_rate(file_path): | |
try: | |
with sf.SoundFile(file_path) as wav_file: | |
return wav_file.samplerate | |
except: print(f"Error processing file: {file_path}") | |
def count_sample_rates_in_dir(root_dir): | |
sample_rates = {} | |
total_files = valid_files = 0 | |
for root, _, files in os.walk(root_dir): | |
for file in files: | |
if file.endswith('.wav'): | |
sample_rate = get_wav_sample_rate(os.path.join(root, file)) | |
if sample_rate: sample_rates[sample_rate] = sample_rates.get(sample_rate,0)+1; valid_files += 1 | |
total_files += 1 | |
sorted_sample_rates = dict(sorted(sample_rates.items(), key=lambda item: item[1], reverse=True)) | |
return sorted_sample_rates, total_files, valid_files | |
def printout(sample_rates, total_files, valid_files): | |
if valid_files < total_files: print("Some .wav files were skipped.") | |
print(f"{valid_files}/{total_files} files counted.\n") | |
print("Sample Rate\tCount\tPercentage\n" + 34*"-") | |
for rate, count in sample_rates.items(): | |
print(f"{rate} Hz\t{count}\t{(count/valid_files)*100:.2f}%") | |
def main(): | |
root_dir = input("Enter the directory path to search for .wav files: ") | |
if not os.path.isdir(root_dir): print("Invalid directory path."); return | |
sample_rates, total_files, valid_files = count_sample_rates_in_dir(root_dir) | |
if sample_rates: printout(sample_rates, total_files, valid_files) | |
else: print("No valid .wav files found.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment