-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscan.py
More file actions
executable file
·191 lines (154 loc) · 5.49 KB
/
scan.py
File metadata and controls
executable file
·191 lines (154 loc) · 5.49 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
"""
Scans /mix directory and populates tracks.json with metadata
Supports MP3, M4A, OGG, FLAC, and WAV formats
Automatically manages a virtual environment for dependencies
"""
import os
import sys
import subprocess
import json
from pathlib import Path
SCRIPT_DIR = Path(__file__).parent.absolute()
VENV_DIR = SCRIPT_DIR / "venv"
MIX_DIR = SCRIPT_DIR / "mix"
OUTPUT_FILE = MIX_DIR / "tracks.json"
def setup_venv():
"""Create and set up virtual environment if it doesn't exist"""
# Determine the path to pip and python in the venv
if sys.platform == "win32":
pip_path = VENV_DIR / "Scripts" / "pip"
python_path = VENV_DIR / "Scripts" / "python"
else:
pip_path = VENV_DIR / "bin" / "pip"
python_path = VENV_DIR / "bin" / "python3"
# Check if venv needs to be created or recreated
if not VENV_DIR.exists() or not python_path.exists():
if VENV_DIR.exists():
print("Virtual environment incomplete, recreating...")
import shutil
shutil.rmtree(VENV_DIR)
else:
print("Creating virtual environment...")
try:
subprocess.check_call([sys.executable, "-m", "venv", str(VENV_DIR)])
print("Virtual environment created successfully.")
except subprocess.CalledProcessError as e:
print(f"Error creating virtual environment: {e}")
sys.exit(1)
# Ensure pip is available (sometimes venv doesn't include it)
if not pip_path.exists():
print("Installing pip in virtual environment...")
try:
subprocess.check_call([str(python_path), "-m", "ensurepip", "--upgrade"])
except subprocess.CalledProcessError as e:
print(f"Error ensuring pip: {e}")
sys.exit(1)
check = subprocess.run(
[str(python_path), "-c", "import mutagen"],
capture_output=True
)
if check.returncode != 0:
try:
subprocess.check_call([str(python_path), "-m", "pip", "install", "-q", "mutagen"])
except subprocess.CalledProcessError:
print("Note: Could not install mutagen (offline?). Metadata will be derived from filenames.\n")
return python_path
def run_in_venv():
"""Re-run this script in the virtual environment"""
python_path = setup_venv()
# Re-run this script with the venv Python
print("Running scanner in virtual environment...\n")
subprocess.check_call([str(python_path), __file__, "--in-venv"])
sys.exit(0)
def scan_tracks():
"""Main function to scan audio files and generate tracks.json"""
# Import mutagen here (only after venv is active)
try:
from mutagen import File as MutagenFile
has_mutagen = True
except ImportError:
MutagenFile = None
has_mutagen = False
print("Mutagen not available. Metadata will be derived from filenames.\n")
# Supported audio formats
SUPPORTED_EXTENSIONS = ('.mp3', '.m4a', '.ogg', '.flac', '.wav')
# Check if tracks directory exists, create if it doesn't
if not MIX_DIR.exists():
print(f"Creating {MIX_DIR.name} directory...")
MIX_DIR.mkdir(parents=True, exist_ok=True)
print(f"✓ {MIX_DIR.name} directory created.")
print(f"\nAdd audio files to the {MIX_DIR.name} directory and run this script again.")
print(f"Supported formats: {', '.join(SUPPORTED_EXTENSIONS)}")
sys.exit(0)
# Find all supported audio files
audio_files = [f for f in MIX_DIR.iterdir() if f.suffix.lower() in SUPPORTED_EXTENSIONS]
if not audio_files:
print(f"No audio files found in {MIX_DIR}")
print(f"\nPlease add audio files to the {MIX_DIR.name} directory and run this script again.")
print(f"Supported formats: {', '.join(SUPPORTED_EXTENSIONS)}")
sys.exit(0)
print(f"Found {len(audio_files)} audio file(s). Extracting metadata...\n")
tracks = []
for audio_file in sorted(audio_files):
try:
title = None
artist = None
if has_mutagen:
audio = MutagenFile(audio_file, easy=True)
if audio and audio.tags:
title = audio.tags.get('title', [None])[0]
artist = audio.tags.get('artist', [None])[0]
# Fallback to filename for title if not found
if not title:
title = audio_file.stem # filename without extension
# Fallback to "Unknown Artist" if not found
if not artist:
artist = "Unknown Artist"
track_info = {
"title": title,
"artist": artist,
"filename": audio_file.name
}
tracks.append(track_info)
print(f"✓ {track_info['artist']} - {track_info['title']}")
except Exception as e:
print(f"✗ Error reading {audio_file.name}: {e}")
continue
# Check if ALL titles start with numbers
# If so, strip the leading numbers from all titles
import re
all_have_leading_numbers = all(
re.match(r'^\d+\s*[-.]?\s*', track['title'])
for track in tracks
)
if all_have_leading_numbers and tracks:
print("\nDetected track numbers in all titles. Stripping them...")
for track in tracks:
original_title = track['title']
# Remove leading number pattern
cleaned_title = re.sub(r'^\d+\s*[-.]?\s*', '', original_title)
if cleaned_title: # Only update if something remains
track['title'] = cleaned_title
if cleaned_title != original_title:
print(f" {original_title} → {cleaned_title}")
if not tracks:
print("\nNo valid audio files could be processed.")
sys.exit(1)
# Write to tracks.json
try:
with open(OUTPUT_FILE, 'w', encoding='utf-8') as f:
json.dump(tracks, f, indent="\t", ensure_ascii=False)
print(f"\n✓ Successfully generated {OUTPUT_FILE.name} with {len(tracks)} track(s).")
except Exception as e:
print(f"\nError writing {OUTPUT_FILE.name}: {e}")
sys.exit(1)
def main():
"""Main entry point"""
# Check if we're already running in venv
if "--in-venv" not in sys.argv:
run_in_venv()
else:
scan_tracks()
if __name__ == "__main__":
main()