From 5f90643455031a43a8f3965c5f4aecb945ad59de Mon Sep 17 00:00:00 2001 From: "Basu Ray, Shreyan" Date: Sat, 27 Jan 2024 05:16:25 +0530 Subject: [PATCH 1/2] getmetadata --- mutagen/getmetadata.py | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 mutagen/getmetadata.py diff --git a/mutagen/getmetadata.py b/mutagen/getmetadata.py new file mode 100644 index 00000000..4eace3ac --- /dev/null +++ b/mutagen/getmetadata.py @@ -0,0 +1,43 @@ +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 +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 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}" + + # 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() + +# 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] From e1c078748ccfbeede6f45e0e23140b82a4d33c4b Mon Sep 17 00:00:00 2001 From: "Basu Ray, Shreyan" Date: Sat, 27 Jan 2024 05:50:03 +0530 Subject: [PATCH 2/2] Update getmetadata.py --- mutagen/getmetadata.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/mutagen/getmetadata.py b/mutagen/getmetadata.py index 4eace3ac..19c934ef 100644 --- a/mutagen/getmetadata.py +++ b/mutagen/getmetadata.py @@ -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 @@ -13,19 +12,12 @@ 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 = "" @@ -33,11 +25,10 @@ def get_audio_metadata(file_path): 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]