Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_audio_metadata Function for Enhanced Audio File Analysis #638

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions mutagen/getmetadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from mutagen import File
import os
'''
The get_audio_metadata function is a Python utility that retrieves two key pieces of information
from audio files:
1. the length of the audio in seconds and
2. its complete metadata (like album, title, artist, genre, date).
It uses pydub for calculating the audio duration and mutagen for extracting metadata.
'''
def get_audio_metadata(file_path):
# Check if file exists
if not os.path.exists(file_path):
return "File does not exist", ""

try:
# Get metadata with mutagen
audio_file = File(file_path, easy=True) # Using easy=True to simplify metadata
metadata = audio_file.tags if audio_file else {}
except Exception as e:
return f"Error extracting metadata: {e}"

# Format metadata
metadata_str = ""
if metadata:
metadata_items = [f"{key}: {', '.join(value) if isinstance(value, list) else value}" for key, value in metadata.items()]
metadata_str = ', '.join(metadata_items)

return metadata_str.strip()

# Example usage:
# length, metadata = get_audio_metadata('path/to/your/audiofile.mp3')
# print("Metadata:", metadata)

#Contributed by Shreyan Basu Ray [Github - @Shreyan1]