-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphase1_scan_usb.py
More file actions
68 lines (50 loc) · 1.68 KB
/
phase1_scan_usb.py
File metadata and controls
68 lines (50 loc) · 1.68 KB
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
import os
import json
from pathlib import Path
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
USB_PATH=r"F:\\" #<--USB Path Here
OUTPUT_JSON="usb_music_metadata.json"
def find_mp3_files(root):
for dirpath,_,filename in os.walk(root):
for fn in filename:
if fn.lower().endswith(".mp3"):
yield Path(dirpath)/fn
def extract_metadata(path:Path):
data={
"file_path":str(path),
"title":path.stem,
"artist":"Unknown Artist",
"album":"Unknown Album",
"composer":"Unknown Composer",
"duration":None
}
try:
audio=MP3(path, ID3=EasyID3)
tags=audio.tags or {}
if "title" in tags:
data["title"]=tags["title"][0]
if "artist" in tags:
data["artist"]=tags["artist"][0]
if "album" in tags:
data["album"]=tags["album"][0]
if "composer" in tags:
data["composer"]=tags["composer"][0]
if audio.info.length:
data["duration"]=round(audio.info.length,2)
except Exception as e:
print(f"Error reading {path}: {e}")
return data
def main():
print(f"Scanning USB drive at {USB_PATH} for MP3 files...")
songs=[]
for mp3_file in find_mp3_files(USB_PATH):
print(f"ReadingL {mp3_file}...")
metadata=extract_metadata(mp3_file)
songs.append(metadata)
print(f"\nFound {len(songs)} MP3 files. Writing metadata to {OUTPUT_JSON}...")
with open(OUTPUT_JSON,"w",encoding="utf-8") as f:
json.dump(songs,f,indent=4,ensure_ascii=False)
print(f"Metadata written to {OUTPUT_JSON} successfully.")
if __name__ == "__main__":
main()