File size: 2,313 Bytes
00c2d61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)