Nesse diretório C:\Users\lucas\Downloads\Karaoke-Version\karaoke-version-dataset tem várias pastas e em cada pasta tem vários arquivos mp3
Preciso de um script python que verifica de forma otimizada qual é a duração total em HORAS dos arquivos Lead Vocal.mp3
Assistant
Certainly! Below is a Python script that traverses through the specified directory and its subdirectories, finds all files named Lead Vocal.mp3, calculates their durations using the mutagen library, and sums up the total duration in hours.
python
import os
from mutagen.mp3 import MP3
def get_total_duration(directory):
total_duration = 0 # in seconds
for root, dirs, files in os.walk(directory):
if 'Lead Vocal.mp3' in files:
file_path = os.path.join(root, 'Lead Vocal.mp3')
try:
audio = MP3(file_path)
total_duration += audio.info.length
except Exception as e:
print(f"Error processing file {file_path}: {e}")
total_hours = total_duration / 3600
return total_hours
if __name__ == "__main__":
directory = r'C:\Users\lucas\Downloads\Karaoke-Version\karaoke-version-dataset'
total_hours = get_total_duration(directory)
print(f'Total duration in hours: {total_hours:.2f}')
Instructions:
Install Mutagen Library:
You need to have the mutagen library installed to run this script. If it's not already installed, you can install it using pip:
shell
pip install mutagen
Script Explanation:
The script uses the os.walk() function to traverse through all directories and subdirectories starting from the specified root directory.
It checks for files named Lead Vocal.mp3 in each directory.
For each found file, it uses mutagen.mp3.MP3 to read the MP3 file and extract its duration (audio.info.length).
It accumulates the durations of all Lead Vocal.mp3 files.
Finally, it converts the total duration from seconds to hours by dividing by 3600 and prints the result formatted to two decimal places.
Run the Script:
Make sure to replace the directory variable with the correct path if it's different.
Run the script using Python:
shell
python script_name.py
Replace script_name.py with the actual name of your Python script file.
Output:
The script will output the total duration in hours of all Lead Vocal.mp3 files found in the directory.
code
Total duration in hours: X.XX
Note: This script is optimized to only process files named Lead Vocal.mp3, thus avoiding unnecessary computation on other files. It uses efficient directory traversal and reads MP3 metadata without fully decoding the audio data, which keeps it performant even on large datasets.
Feel free to let me know if you need any further assistance or modifications to the script!