Skip to content

Commit

Permalink
Update getmetadata.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreyan1 committed Jan 27, 2024
1 parent 5f90643 commit e1c0787
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions mutagen/getmetadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from mutagen import File
from pydub import AudioSegment
import os
'''
The get_audio_metadata function is a Python utility that retrieves two key pieces of information
Expand All @@ -13,31 +12,23 @@ def get_audio_metadata(file_path):
if not os.path.exists(file_path):
return "File does not exist", ""

try:
# Get audio length
audio = AudioSegment.from_file(file_path)
length_seconds = len(audio) / 1000
except Exception as e:
return f"Error processing audio length: {e}", ""

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 length_seconds, f"Error extracting metadata: {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 length_seconds, metadata_str.strip()
return metadata_str.strip()

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

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

0 comments on commit e1c0787

Please sign in to comment.