-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_audio.py
More file actions
315 lines (247 loc) · 9.68 KB
/
process_audio.py
File metadata and controls
315 lines (247 loc) · 9.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Unified Audio Processing Pipeline
Imports and orchestrates the individual skills:
1. audio-metadata-editor: Edit ID3 tags
2. archive-uploader: Upload to archive.org
Usage:
python process_audio.py <folder> # Process single folder
python process_audio.py --all # Process all pending folders
python process_audio.py <folder> --tag-only # Only add metadata
python process_audio.py <folder> --upload-only # Only upload
python process_audio.py <folder> --dry-run # Preview without changes
"""
import argparse
import json
import sys
from datetime import datetime
from pathlib import Path
import yaml
# Fix for Windows console UTF-8 encoding
if sys.platform == "win32":
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
# Add skills to path for importing
SKILLS_PATH = Path(__file__).parent / ".claude" / "skills"
sys.path.insert(0, str(SKILLS_PATH / "audio-metadata-editor" / "scripts"))
sys.path.insert(0, str(SKILLS_PATH / "archive-uploader" / "scripts"))
# Import from skills (NO CODE DUPLICATION!)
from edit_metadata import process_folder as tag_folder
from upload_to_archive import (
upload_to_archive,
generate_identifier,
generate_description,
generate_tags,
check_archive_exists,
move_to_done,
get_audio_files
)
# ============================================================
# Configuration
# ============================================================
def load_config() -> dict:
"""Load configuration from config.yaml."""
config_path = Path(__file__).parent / "config.yaml"
if config_path.exists():
with open(config_path) as f:
return yaml.safe_load(f)
return {
"defaults": {
"artist": "Sheikh Mohammad Mohsin",
"album": "Al Fiqh ul Ahwat Lecture Series",
"genre": "Islamic Jurisprudence",
"language": "Urdu",
"collection": "opensource_audio",
"base_tags": "Islamic Lecture; Fiqh; Urdu"
},
"folders": {
"input": "audio_folders",
"done": "audio-uploaded-done"
}
}
# ============================================================
# Upload History
# ============================================================
def get_history_path() -> Path:
return Path(__file__).parent / "upload_history.json"
def load_history() -> dict:
path = get_history_path()
if path.exists():
with open(path) as f:
return json.load(f)
return {}
def save_history(history: dict):
path = get_history_path()
with open(path, "w") as f:
json.dump(history, f, indent=2)
def add_to_history(identifier: str, folder_name: str, url: str, file_count: int):
history = load_history()
history[identifier] = {
"folder": folder_name,
"uploaded": datetime.now().isoformat(),
"url": url,
"files": file_count
}
save_history(history)
def is_already_uploaded(identifier: str) -> bool:
return identifier in load_history()
# ============================================================
# Pipeline Steps
# ============================================================
def step_tag_metadata(folder: Path, config: dict, dry_run: bool = False) -> bool:
"""Step 1: Edit metadata using audio-metadata-editor skill."""
print("\n[STEP 1] Editing metadata...")
if dry_run:
print(" [DRY RUN] Would tag metadata")
return True
defaults = config["defaults"]
success, skipped = tag_folder(
folder,
artist=defaults["artist"],
album=defaults["album"],
genre=defaults["genre"],
language=defaults["language"]
)
return success > 0
def step_upload(folder: Path, config: dict, dry_run: bool = False) -> bool:
"""Step 2: Upload using archive-uploader skill."""
print("\n[STEP 2] Uploading to archive.org...")
defaults = config["defaults"]
identifier = generate_identifier(folder)
# Check for duplicates
if is_already_uploaded(identifier):
print(f" [SKIP] Already in upload history: {identifier}")
return False
if check_archive_exists(identifier):
print(f" [SKIP] Already exists on archive.org: {identifier}")
file_count = len(get_audio_files(folder))
add_to_history(identifier, folder.name, f"https://archive.org/details/{identifier}", file_count)
return False
# Generate metadata
description = generate_description(folder, defaults["artist"])
tags = generate_tags(folder, defaults["base_tags"])
# Upload (without auto-move, we'll handle it separately)
success = upload_to_archive(
folder=folder,
identifier=None, # Auto-generate
creator=defaults["artist"],
description=description,
tags=tags,
collection=defaults["collection"],
dry_run=dry_run,
auto_move=False # We handle move in step 3
)
if success and not dry_run:
file_count = len(get_audio_files(folder))
add_to_history(identifier, folder.name, f"https://archive.org/details/{identifier}", file_count)
return success
def step_move_to_done(folder: Path, config: dict, dry_run: bool = False) -> bool:
"""Step 3: Move folder to done directory."""
print("\n[STEP 3] Moving to done folder...")
done_folder = Path(__file__).parent / config["folders"]["done"]
if dry_run:
print(f" [DRY RUN] Would move to: {done_folder / folder.name}")
return True
result = move_to_done(folder, done_folder)
if result:
print(f" Moved to: {result}")
return result is not None
# ============================================================
# Main Pipeline
# ============================================================
def process_folder(folder: Path, config: dict, tag_only: bool = False,
upload_only: bool = False, dry_run: bool = False) -> bool:
"""Process a single folder through the pipeline."""
print("=" * 60)
print(f"Processing: {folder.name}")
print("=" * 60)
if not folder.is_dir():
print(f"Error: {folder} is not a directory")
return False
# Step 1: Edit metadata (unless upload-only)
if not upload_only:
if not step_tag_metadata(folder, config, dry_run):
print(" Warning: Metadata step had issues")
if tag_only:
print("\n[DONE] Metadata updated (--tag-only mode)")
return True
# Step 2: Upload to archive.org
if not step_upload(folder, config, dry_run):
return False
# Step 3: Move to done folder
if not dry_run:
step_move_to_done(folder, config, dry_run)
print("\n" + "=" * 60)
print("SUCCESS!")
print("=" * 60)
return True
def process_all(config: dict, dry_run: bool = False) -> int:
"""Process all folders in input directory."""
input_folder = Path(__file__).parent / config["folders"]["input"]
if not input_folder.exists():
print(f"Input folder not found: {input_folder}")
return 0
folders = [f for f in input_folder.iterdir() if f.is_dir()]
if not folders:
print("No folders to process!")
return 0
print(f"Found {len(folders)} folders to process\n")
success_count = 0
for folder in sorted(folders):
if process_folder(folder, config, dry_run=dry_run):
success_count += 1
print()
print(f"\nProcessed {success_count}/{len(folders)} folders successfully")
return success_count
# ============================================================
# CLI
# ============================================================
def main():
parser = argparse.ArgumentParser(
description="Unified Audio Processing Pipeline",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python process_audio.py audio_folders/21_Chapter # Process single folder
python process_audio.py --all # Process all pending
python process_audio.py folder --tag-only # Only add metadata
python process_audio.py folder --dry-run # Preview changes
"""
)
parser.add_argument("folder", nargs="?", type=Path, help="Folder to process")
parser.add_argument("--all", action="store_true", help="Process all folders in input directory")
parser.add_argument("--tag-only", action="store_true", help="Only edit metadata, don't upload")
parser.add_argument("--upload-only", action="store_true", help="Only upload, skip metadata")
parser.add_argument("--dry-run", action="store_true", help="Preview without making changes")
parser.add_argument("--history", action="store_true", help="Show upload history")
args = parser.parse_args()
config = load_config()
# Show history
if args.history:
history = load_history()
if not history:
print("No upload history yet.")
else:
print("Upload History:")
print("-" * 60)
for identifier, info in history.items():
print(f" {identifier}")
print(f" Folder: {info['folder']}")
print(f" Date: {info['uploaded']}")
print(f" Files: {info['files']}")
print(f" URL: {info['url']}")
print()
return
# Process all folders
if args.all:
process_all(config, args.dry_run)
return
# Process single folder
if not args.folder:
parser.print_help()
sys.exit(1)
process_folder(args.folder, config, args.tag_only, args.upload_only, args.dry_run)
if __name__ == "__main__":
main()