Spaces:
Running
Running
import json | |
import os | |
from tinytag import TinyTag | |
from PIL import Image | |
import io | |
import math | |
path = "D:/01-Bhajan Haridham-Mp3 (Pen-1)" | |
# list all files in the directory and subdirectories and store them in a list | |
def list_files(path): | |
files = [] | |
for r, d, f in os.walk(path): | |
for file in f: | |
if ".mp3" in file: | |
files.append(os.path.join(r, file).replace("\\", "/")) | |
return files | |
bhajan_audios = list_files(path) | |
# extract all metadata from the mp3 files and store them in a list | |
data = [] | |
for bhajan in bhajan_audios: | |
metadata = {} | |
# audio = TinyTag.get(bhajan) | |
audio = TinyTag.get(bhajan, image=True) | |
try: | |
image = audio.get_image() | |
if image is not None: | |
pi = Image.open(io.BytesIO(image)) | |
pi.save("D:/01-Bhajan Haridham-Mp3 (Pen-1)/thumbnail/" + audio.title + ".jpg") | |
metadata["thumbnail"] = True | |
except: | |
pass | |
try: | |
print("Url:" + bhajan) | |
metadata["url"] = bhajan.replace("D:/01-Bhajan Haridham-Mp3 (Pen-1)","") | |
except: | |
pass | |
try: | |
print("Title:" + audio.title) | |
metadata["title"] = audio.title | |
except: | |
pass | |
try: | |
print("album:" + audio.album) | |
metadata["album"] = audio.album | |
except: | |
pass | |
try: | |
print("Artist: " + audio.artist) | |
metadata["artist"] = audio.artist | |
except: | |
pass | |
try: | |
print("Year Released: " + audio.year) | |
metadata["year"] = audio.year | |
except: | |
pass | |
try: | |
print("AlbumArtist: " + audio.albumartist) | |
metadata["albumArtist"] = audio.albumartist | |
except: | |
pass | |
try: | |
print("Duration: " + str(audio.duration) + " seconds") | |
# convert the duration to minutes and seconds | |
metadata["duration"] = [math.floor(audio.duration / 60), math.floor(audio.duration % 60)] | |
except: | |
pass | |
try: | |
print("copyright: " + audio.extra["copywrite"]) | |
# convert the duration to minutes and seconds | |
metadata["copyright"] = audio.extra["copywrite"] | |
except: | |
pass | |
data.append(metadata) | |
with open("./metadata.json", "w") as f: | |
json.dump(data, f, indent=4) | |