To accomplish your task, we'll create a Python script that traverses through the specified directory, checks the bitrate of all vocals.mp3 files in each subfolder, and then displays a table with the bitrates and the average bitrate. Below is the script that does exactly that:
import os
from mutagen.mp3 import MP3
from statistics import mean
# Set the root directory
root_dir = r'C:\Users\lucas\Downloads\Karaoke-Version\karaoke-version-dataset'
# Lists to store file paths and their corresponding bitrates
bitrates = []
files = []
# Iterate over each subdirectory in the root directory
for entry in os.scandir(root_dir):
if entry.is_dir():
subdir = entry.path
vocals_mp3 = os.path.join(subdir, 'vocals.mp3')
if os.path.exists(vocals_mp3):
try:
# Load the MP3 file
audio = MP3(vocals_mp3)
# Get the bitrate in kbps
bitrate = audio.info.bitrate // 1000
bitrates.append(bitrate)
# Store the relative path for the file
files.append(os.path.relpath(vocals_mp3, root_dir))
except Exception as e:
print(f"Error processing file {vocals_mp3}: {e}")
else:
print(f"No vocals.mp3 in directory {subdir}")
# Check if any bitrates were collected
if bitrates:
# Print the table header
print(f"{'File':<70} {'Bitrate (kbps)':>15}")
print('-' * 85)
# Print each file and its bitrate
for f, b in zip(files, bitrates):
print(f"{f:<70} {b:>15}")
# Calculate and print the average bitrate
avg_bitrate = mean(bitrates)
print('-' * 85)
print(f"{'Average':<70} {avg_bitrate:>15.2f}")
else:
print("No bitrates were collected.")
Instructions to Run the Script:
-
Install the mutagen library if you haven't already. You can install it using the following command:
pip install mutagen
-
Update the root_dir path in the script if the path to your dataset directory is different.
-
Run the script. It will process all vocals.mp3 files in the subdirectories of your dataset directory, gather their bitrates, and then display a table with the filenames and bitrates, followed by the average bitrate.
What the Script Does:
-
Traversal: The script uses os.scandir to iterate over each item in the root directory. It checks if the item is a directory and then looks for a file named vocals.mp3 within it.
-
Bitrate Extraction: For each vocals.mp3 file found, it uses the mutagen library to extract the bitrate information.
-
Data Collection: It stores the relative file paths and their corresponding bitrates in lists.
-
Display: After processing all files, it prints a formatted table showing each file and its bitrate, and then calculates and displays the average bitrate.
Sample Output:
File Bitrate (kbps)
-------------------------------------------------------------------------------------
Subfolder1/vocals.mp3 320
Subfolder2/vocals.mp3 256
Subfolder3/vocals.mp3 192
-------------------------------------------------------------------------------------
Average 256.00
Notes:
-
The script handles exceptions that may occur during file processing and will inform you if a vocals.mp3 file is missing in a subdirectory.
-
The bitrates are displayed in kilobits per second (kbps).
-
Ensure you have the necessary permissions to read files in the specified directory.
Feel free to modify the script to suit additional requirements or to enhance error handling as needed.