-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkbench_core.py
More file actions
1091 lines (961 loc) · 35 KB
/
workbench_core.py
File metadata and controls
1091 lines (961 loc) · 35 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# workbench_core.py
from __future__ import annotations
"""
Core helpers used by the GUI and tests.
- Tool resolution: resolve_tool_path(), have()
- Subprocess helpers: _run_capture(), run_capture(), run_quiet()
- Cancellation: CANCEL_EVENT, process tracking, terminate_all_procs()
- Cookies: _convert_cookie_editor_json_to_netscape(), prepare_cookies()
- Audio: join_via_wav_then_lame(), write_id3_tags()
- Minimal task runner surface so the GUI can import: ProcessingOptions, run_processing_task()
Everything is written conservatively to satisfy ruff (E,F,I,UP) and Pylance.
"""
from dataclasses import dataclass
from pathlib import Path
from collections.abc import Callable
import json
import queue
import re
import shutil
import os
import random
import signal
import sys
import subprocess
import threading
import time
import http.cookiejar as _cj
# Global registry of running child processes
_CURRENT_PROCS_LOCK = threading.RLock()
_CURRENT_PROCS: set[subprocess.Popen] = set()
def _register_proc(p: subprocess.Popen) -> None:
with _CURRENT_PROCS_LOCK:
_CURRENT_PROCS.add(p)
def _unregister_proc(p: subprocess.Popen) -> None:
with _CURRENT_PROCS_LOCK:
_CURRENT_PROCS.discard(p)
# -----------------------
# Import/install helpers
# -----------------------
def ensure_python_package(pkg: str, import_name: str | None = None, log=None) -> bool:
# Use the provided import name or derive it from the package name
name = import_name or pkg.replace("-", "_")
try:
__import__(name)
return True
except ImportError:
if log:
log(f"Python package '{pkg}' missing → attempting pip install...")
try:
# Use run_quiet which should be defined in your core
rc = run_quiet([sys.executable, "-m", "pip", "install", "-U", pkg])
if rc == 0:
if log:
log(f"Installed '{pkg}' successfully.")
__import__(name) # Verify import after install
return True
else:
if log:
log(f"pip install returned rc={rc} for '{pkg}'")
except Exception as e:
if log:
log(f"pip install failed for '{pkg}': {e}")
return False
def ensure_mutagen_installed(log) -> bool:
"""Convenience wrapper to check for the 'mutagen' package."""
return ensure_python_package("mutagen", "mutagen", log=log)
# -----------------------
# Process/cancel plumbing
# -----------------------
CURRENT_PROCS: list[subprocess.Popen] = []
CURRENT_PROCS_LOCK = threading.Lock()
CANCEL_EVENT = threading.Event()
def _spawn_process(cmd: list[str], **kwargs) -> subprocess.Popen:
"""
Start a child in its own process group to support cross-platform cancellation.
Always pass an argv list (no shell).
"""
# Text mode defaults
kwargs.setdefault("stdin", subprocess.PIPE)
kwargs.setdefault("stdout", subprocess.PIPE)
kwargs.setdefault("stderr", subprocess.PIPE)
kwargs.setdefault("text", True)
if os.name == "nt":
# New process group so we can send CTRL_BREAK_EVENT
CREATE_NEW_PROCESS_GROUP = 0x00000200
kwargs["creationflags"] = kwargs.get("creationflags", 0) | CREATE_NEW_PROCESS_GROUP
else:
# New group; SIGTERM/SIGKILL can be sent to the whole tree
kwargs.setdefault("preexec_fn", os.setsid)
p = subprocess.Popen(cmd, **kwargs) # nosec: trusted argv list
_register_proc(p)
return p
def spawn_streaming(cmd: list[str], **kwargs) -> subprocess.Popen:
"""
Start a long-running command whose stdout/stderr you will read incrementally.
Use this for yt-dlp streaming in the GUI.
"""
p = _spawn_process(cmd, **kwargs)
return p
def finalize_process(p: subprocess.Popen) -> None:
"""Remove a child from the registry after it exits (GUI streaming case)."""
_unregister_proc(p)
def terminate_all_procs(timeout: float = 3.0) -> None:
"""
Try graceful shutdown of all registered children, then escalate.
Safe to call multiple times.
"""
with _CURRENT_PROCS_LOCK:
procs = list(_CURRENT_PROCS)
# 1) Graceful
for p in procs:
try:
if p.poll() is not None:
continue
if os.name == "nt":
p.send_signal(signal.CTRL_BREAK_EVENT)
else:
# signal whole group
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
except Exception:
try:
p.terminate()
except Exception:
pass
# 2) Wait a bit
deadline = time.time() + max(0.1, timeout)
for p in procs:
try:
if p.poll() is None:
remaining = deadline - time.time()
if remaining > 0:
p.wait(remaining)
except Exception:
pass
# 3) Hard kill leftovers
for p in procs:
try:
if p.poll() is None:
if os.name == "nt":
p.kill()
else:
os.killpg(os.getpgid(p.pid), signal.SIGKILL)
except Exception:
pass
# Cleanup registry
for p in procs:
_unregister_proc(p)
def _last_lines(s: str, n: int = 12) -> str:
try:
return "\n".join(s.splitlines()[-n:])
except Exception:
return s
def _run_capture(
cmd: list[str], *, timeout: float | None = None, check: bool = True, cwd: str | None = None
) -> tuple[int, str, str]:
"""
Run a command to completion, capturing stdout/stderr with optional timeout.
Raises CalledProcessError when check=True and rc != 0 (with stderr tail).
"""
p = _spawn_process(cmd, cwd=cwd)
try:
out, err = p.communicate(timeout=timeout)
except subprocess.TimeoutExpired:
# escalate on timeout
try:
if os.name == "nt":
p.send_signal(signal.CTRL_BREAK_EVENT)
else:
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
except Exception:
pass
finally:
try:
out, err = p.communicate(timeout=1.0)
except Exception:
out, err = "", ""
rc = p.poll() if p.poll() is not None else -9
else:
rc = p.returncode
finally:
_unregister_proc(p)
if check and rc != 0:
tail = _last_lines(err)
raise subprocess.CalledProcessError(rc, cmd, output=out, stderr=tail)
return rc, out, err
def run_quiet(
cmd: list[str], *, timeout: float | None = None, cwd: str | None = None
) -> tuple[int, str]:
"""
Run and return (rc, stderr_tail). Keeps last lines of stderr for diagnostics.
"""
try:
rc, _, err = _run_capture(cmd, timeout=timeout, check=False, cwd=cwd)
except subprocess.CalledProcessError as e:
# shouldn't happen because check=False above, but just in case
return e.returncode, _last_lines(e.stderr or "")
return rc, _last_lines(err or "")
# -----------------------
# Subprocess helpers
# -----------------------
def run_capture(
cmd: list[str],
cwd: Path | None = None,
env: dict[str, str] | None = None,
text: bool = True,
) -> str:
"""Convenience wrapper returning stdout (raises on non-zero)."""
rc, out, err = _run_capture(cmd, cwd=cwd, env=env, text=text)
if rc != 0:
raise RuntimeError(f"{cmd[0]} exited with {rc}: {(err or out).strip()}")
return out
# -----------------------
# Tool resolution
# -----------------------
def resolve_tool_path(exe: str) -> str | None:
"""Best-effort cross-platform lookup for external tools."""
# 1) PATH
p = shutil.which(exe)
if p:
return p
# 2) Windows common locations
if os.name == "nt":
candidates = []
local = os.environ.get("LOCALAPPDATA", "")
userprofile = os.environ.get("USERPROFILE", "")
program_files = os.environ.get("ProgramFiles", "")
program_files_x86 = os.environ.get("ProgramFiles(x86)", "")
# WinGet shim
if local:
candidates.append(os.path.join(local, "Microsoft", "WinGet", "Links", f"{exe}.exe"))
# Chocolatey shims
candidates.append(
os.path.join(
os.environ.get("ProgramData", "C:\\ProgramData"),
"chocolatey",
"bin",
f"{exe}.exe",
)
)
# Scoop shims
if userprofile:
candidates.append(os.path.join(userprofile, "scoop", "shims", f"{exe}.exe"))
# App-specific installs
if exe.lower() == "mp3gain":
if program_files_x86:
candidates.append(os.path.join(program_files_x86, "MP3Gain", "mp3gain.exe"))
if program_files:
candidates.append(os.path.join(program_files, "MP3Gain", "mp3gain.exe"))
# ffmpeg/ffprobe common dir
for base in [program_files, program_files_x86]:
if base:
candidates.append(os.path.join(base, "FFmpeg", "bin", f"{exe}.exe"))
for c in candidates:
if c and os.path.exists(c):
return c
# 3) macOS/Homebrew typical
if sys.platform == "darwin":
for pth in ["/opt/homebrew/bin", "/usr/local/bin"]:
cand = os.path.join(pth, exe)
if os.path.exists(cand):
return cand
# 4) Linux common
for pth in ["/usr/bin", "/usr/local/bin"]:
cand = os.path.join(pth, exe)
if os.path.exists(cand):
return cand
return None
def have(exe: str) -> bool:
return resolve_tool_path(exe) is not None
# -----------------------
# Stubs used by GUI (safe no-ops if not used)
# -----------------------
def _ffprobe_duration_seconds(path: Path, log) -> float:
try:
out = run_capture(
[
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
str(path),
]
)
d = float(out.strip())
if d != d or d == float("inf"):
return 0.0
return max(0.0, d)
except Exception as e:
log(f"ffprobe duration failed: {e}")
return 0.0
def verify_tools(log: Callable[[str], None] | None = None) -> None:
"""Checks for required tools and logs their status."""
if not log:
return
log("Verifying required tools...")
tools_to_check = {
"yt-dlp": True,
"ffmpeg": True,
"ffprobe": True,
"mp3gain": False, # Optional
}
for tool, is_required in tools_to_check.items():
if have(tool):
log(f"✅ {tool}: Found")
else:
status = "MISSING" if is_required else "Missing (Optional)"
log(f"❌ {tool}: {status}")
def check_and_install_deps(log: Callable[[str], None] | None = None) -> None:
"""Checks for and attempts to install system dependencies like ffmpeg."""
if not log:
return
app_dir = Path(__file__).resolve().parent
scripts_dir = app_dir / "scripts"
scripts_dir.mkdir(exist_ok=True)
missing = [exe for exe in ("ffmpeg", "ffprobe") if not have(exe)]
if not have("mp3gain"):
missing.append("mp3gain (optional)")
if not missing:
log("All system dependencies already installed.")
return
else:
log("Missing dependencies: " + ", ".join(missing))
if sys.platform.startswith("win") and have("winget"):
log("Attempting to install via WinGet...")
cmds_to_run = []
if not have("ffmpeg"):
cmds_to_run.append(
[
"winget",
"install",
"-e",
"--id",
"Gyan.FFmpeg",
"--accept-package-agreements",
"--accept-source-agreements",
]
)
if not have("mp3gain"):
cmds_to_run.append(
[
"winget",
"install",
"-e",
"--id",
"GlenSawyer.MP3Gain",
"--accept-package-agreements",
"--accept-source-agreements",
]
)
for c in cmds_to_run:
try:
log(f"Running: {' '.join(c)}")
run_capture(c)
log(f"Successfully ran install command for {c[4]}.")
except Exception as e:
log(f"Install step failed: {e}")
log("If tools are still not detected, you may need to restart your shell/terminal.")
# Write helper scripts for manual installation.
ps1 = scripts_dir / "install_deps.ps1"
ps1.write_text(
"winget source update\n"
"winget install -e --id Gyan.FFmpeg --accept-package-agreements --accept-source-agreements\n"
"winget install -e --id GlenSawyer.MP3Gain --accept-package-agreements --accept-source-agreements\n",
encoding="utf-8",
)
sh = scripts_dir / "install_deps.sh"
sh.write_text(
"#!/usr/bin/env bash\nset -euo pipefail\n"
"if command -v brew >/dev/null 2>&1; then brew update; brew install ffmpeg mp3gain; exit 0; fi\n"
"if command -v apt >/dev/null 2>&1; then sudo apt update; sudo apt install -y ffmpeg mp3gain; exit 0; fi\n"
"if command -v dnf >/dev/null 2>&1; then sudo dnf install -y https://mirrors.rpmfusion.org/free/fedora/"
"rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm || true; sudo dnf install -y ffmpeg mp3gain; exit 0; fi\n"
"echo 'No supported package manager detected (checked for brew, apt, dnf). Please install ffmpeg and mp3gain manually.'\n",
encoding="utf-8",
)
try:
os.chmod(sh, 0o700)
except Exception:
pass
log(f"Wrote helper installation scripts to: {scripts_dir}")
def write_playlist(outdir: Path, files: list[Path], log, name: str, fmt: str) -> None:
"""Write M3U/M3U8 playlists.
- M3U8: UTF-8, #EXTM3U + #EXTINF:duration,title lines.
- M3U: latin-1 fallback, simple list of filenames (broadest compatibility).
"""
fmtU = (fmt or "M3U8").upper()
try:
# Collect metadata for EXTINF
meta = []
try:
from mutagen.id3 import ID3
have_tags = True
except Exception:
have_tags = False
for f in files:
dur = int(round(_ffprobe_duration_seconds(f, log)))
title = f.stem
artist = ""
if have_tags:
try:
tags = ID3(f)
if tags.get("TIT2"):
title = str(tags["TIT2"].text[0])
if tags.get("TPE1"):
artist = str(tags["TPE1"].text[0])
except Exception:
pass
disp = (
f"{artist + ' - ' if artist else ''}{title}".replace("\n", " ").lstrip("#").strip()
)
meta.append((f.name, dur, disp))
if fmtU in ("M3U8", "BOTH"):
pl = outdir / f"{name}.m3u8"
lines = ["#EXTM3U"]
for fname, dur, disp in meta:
lines.append(f"#EXTINF:{dur},{disp}")
lines.append(fname)
txt = "\n".join(lines) + "\n"
pl.write_text(txt, encoding="utf-8")
log(f"Wrote playlist: {pl.name}")
if fmtU in ("M3U", "BOTH"):
pl2 = outdir / f"{name}.m3u"
body = "\n".join([f.name for f in files]) + "\n"
try:
pl2.write_text(body, encoding="latin-1")
except Exception:
pl2.write_text(body, encoding="utf-8")
log(f"Wrote playlist: {pl2.name}")
except Exception as e:
log(f"Playlist write failed: {e}")
def write_cue_for_joined(joined_mp3: Path, parts: list[Path], log) -> None:
try:
from mutagen.id3 import ID3
except Exception as e:
log(f"mutagen not available for CUE metadata: {e}")
return
try:
meta: list[tuple[str, str, float]] = []
for p in parts:
title, artist = p.stem, ""
try:
tags = ID3(p)
if tags.get("TIT2"):
title = str(tags["TIT2"].text[0])
if tags.get("TPE1"):
artist = str(tags["TPE1"].text[0])
except Exception:
pass
meta.append((title, artist, _ffprobe_duration_seconds(p, log)))
cue = joined_mp3.with_suffix(".cue")
lines = []
lines.append(f'TITLE "{joined_mp3.stem}"')
if meta and meta[0][1]:
lines.append(f'PERFORMER "{meta[0][1]}"')
lines.append(f'FILE "{joined_mp3.name}" MP3')
fps = 75
t = 0.0
for i, (title, artist, dur) in enumerate(meta, start=1):
frames = int(round(t * fps))
mm = frames // (60 * fps)
ss = (frames // fps) % 60
ff = frames % fps
lines.append(f" TRACK {i:02d} AUDIO")
if artist:
lines.append(f' PERFORMER "{artist}"')
lines.append(f' TITLE "{title}"')
lines.append(f" INDEX 01 {mm:02d}:{ss:02d}:{ff:02d}")
t += dur
cue.write_text("\n".join(lines) + "\n", encoding="utf-8")
log(f"Wrote CUE: {cue.name}")
except Exception as e:
log(f"CUE write failed: {e}")
def embed_id3_chapters(joined_mp3: Path, parts: list[Path], log) -> None:
try:
from mutagen.id3 import ID3, ID3NoHeaderError, CHAP, CTOC, TIT2, TPE1
except Exception as e:
log(f"mutagen not available for chapters: {e}")
return
try:
# audio = MP3(joined_mp3)
try:
tags = ID3(joined_mp3)
except ID3NoHeaderError:
tags = ID3()
def _attach(frame, sub):
if hasattr(frame, "add"):
try:
frame.add(sub)
return
except Exception:
pass
try:
frame.subframes[sub.HashKey] = [sub]
except Exception:
pass
ids = []
start_ms = 0
for idx, p in enumerate(parts, start=1):
dur = _ffprobe_duration_seconds(p, log)
end_ms = int(round((start_ms / 1000.0 + dur) * 1000))
chap = CHAP(
element_id=f"chp{idx}".encode("ascii"),
start_time=start_ms,
end_time=end_ms,
start_offset=0,
end_offset=0,
)
t_title, t_artist = p.stem, ""
try:
src = ID3(p)
if src.get("TIT2"):
t_title = str(src["TIT2"].text[0])
if src.get("TPE1"):
t_artist = str(src["TPE1"].text[0])
except Exception:
pass
_attach(chap, TIT2(encoding=3, text=t_title))
if t_artist:
_attach(chap, TPE1(encoding=3, text=t_artist))
tags.add(chap)
ids.append(chap.element_id)
start_ms = end_ms
toc = CTOC(element_id=b"toc", flags=0x03, child_element_ids=ids)
_attach(toc, TIT2(encoding=3, text=joined_mp3.stem))
tags.add(toc)
tags.save(joined_mp3, v2_version=3)
log("Embedded ID3 chapters.")
except Exception as e:
log(f"Chapter embed failed: {e}")
def write_vlc_segment_playlist(joined_mp3: Path, parts: list[Path], outdir: Path, log) -> None:
"""
Create a VLC-specific M3U that emulates chapters by repeating the same MP3
with #EXTVLCOPT:start-time / stop-time per segment.
"""
try:
from mutagen.id3 import ID3
have_tags = True
except Exception:
have_tags = False
lines = ["#EXTM3U"]
start = 0.0
for p in parts:
dur = _ffprobe_duration_seconds(p, log)
end = start + max(dur, 0.1)
title = p.stem
artist = ""
if have_tags:
try:
tags = ID3(p)
if tags.get("TIT2"):
title = str(tags["TIT2"].text[0])
if tags.get("TPE1"):
artist = str(tags["TPE1"].text[0])
except Exception:
pass
disp = f"{artist + ' - ' if artist else ''}{title}".replace("\n", " ").lstrip("#").strip()
secs = max(1, int(round(end - start)))
lines.append(f"#EXTINF:{secs},{disp}")
lines.append(f"#EXTVLCOPT:start-time={int(round(start))}")
lines.append(f"#EXTVLCOPT:stop-time={int(round(end))}")
lines.append(joined_mp3.name)
start = end
out = outdir / f"{joined_mp3.stem}.vlc-segments.m3u"
out.write_text("\n".join(lines) + "\n", encoding="utf-8")
log(f"Wrote VLC segment playlist: {out.name}")
def get_album_name(files: list[Path]) -> str:
# Heuristic: common parent folder name or fallback
if files:
return files[0].parent.name or "Album"
return "Album"
# -----------------------
# Cookies
# -----------------------
def prepare_cookies(
cookies_file: Path | None,
cookies_browser: str | None,
working_dir: Path,
log: Callable[[str], None] | None = None,
) -> Path | None:
"""
If a JSON from Cookie-Editor is provided, convert to Netscape text and return that path.
If a browser name is provided, return None (yt-dlp will use --cookies-from-browser).
Otherwise, return the original cookies_file.
"""
if cookies_file and cookies_file.suffix.lower() == ".json":
out = working_dir / "cookies.txt"
converted_path = convert_cookie_editor_json_to_netscape(cookies_file, out, log=log)
return converted_path
if cookies_browser and cookies_browser.lower() != "none":
if log:
log(f"[cookies] using cookies from browser: {cookies_browser}")
return None
return cookies_file
# In workbench_core.py
def convert_cookie_editor_json_to_netscape(json_path: Path, out_txt: Path, log) -> Path:
"""Convert Cookie-Editor/EditThisCookie JSON into a Netscape cookies.txt with header."""
try:
data = json.loads(json_path.read_text(encoding="utf-8", errors="ignore"))
if isinstance(data, dict) and "cookies" in data:
data = data["cookies"]
lines = []
header = "# Netscape HTTP Cookie File\n# This file was generated by YT Audio Workbench from a JSON export.\n"
for c in data:
domain = c.get("domain") or c.get("host") or ""
host_only = bool(c.get("hostOnly")) if "hostOnly" in c else None
if host_only is True:
if domain.startswith("."):
domain = domain.lstrip(".")
include_sub = "FALSE"
elif host_only is False:
if not domain.startswith("."):
domain = "." + domain
include_sub = "TRUE"
else:
include_sub = "TRUE" if domain.startswith(".") else "FALSE"
path = c.get("path") or "/"
https = "TRUE" if c.get("secure") else "FALSE"
exp = c.get("expirationDate") or c.get("expires") or 0
try:
exp = int(float(exp))
except Exception:
exp = 0
name = (
(c.get("name") or "").replace("\t", "%09").replace("\n", "%0A").replace("\r", "%0D")
)
value = (
(c.get("value") or "")
.replace("\t", "%09")
.replace("\n", "%0A")
.replace("\r", "%0D")
)
domain_field = ("#HttpOnly_" + domain) if c.get("httpOnly") else domain
lines.append("\t".join([domain_field, include_sub, path, https, str(exp), name, value]))
out_txt.write_text(header + "\n".join(lines) + "\n", encoding="utf-8")
# vvv FIX IS HERE vvv
if log:
log(f"Converted JSON cookies -> {out_txt}")
return out_txt
except Exception as e:
# vvv AND FIX IS HERE vvv
if log:
log(f"Cookie JSON convert failed: {e}")
return out_txt
def validate_netscape_cookiefile(path: Path, log) -> tuple[bool, str]:
"""Return (ok, message). If header missing, auto-repair and log it."""
try:
jar = _cj.MozillaCookieJar()
jar.load(str(path), ignore_discard=True, ignore_expires=True)
return True, f"Loaded {len(jar)} cookies from {path.name}"
except Exception as e:
# Try to auto-fix by adding the Netscape header if missing
try:
txt = Path(path).read_text(encoding="utf-8", errors="ignore")
first_nonempty = ""
for line in txt.splitlines():
if not line.strip():
continue
first_nonempty = line
break
if not first_nonempty.startswith("# Netscape HTTP Cookie File"):
fixed = Path(path).with_suffix(path.suffix + ".withheader")
header = "# Netscape HTTP Cookie File\n# Added header by YT Audio Workbench to satisfy validators.\n"
fixed.write_text(
header + (txt if txt.endswith("\n") else txt + "\n"),
encoding="utf-8",
)
jar2 = _cj.MozillaCookieJar()
jar2.load(str(fixed), ignore_discard=True, ignore_expires=True)
log(f"Cookie file missing header → repaired: {fixed.name}")
return True, f"Loaded {len(jar2)} cookies from {fixed.name}"
except Exception:
pass
return False, f"Cookie file validation failed: {e}"
# -----------------------
# Audio helpers
# -----------------------
def _dedup_artist_in_filenames(files, log):
"""Rename files where filename pattern repeats the artist, e.g.:
'Artist - Artist - Title.mp3' or '001 - Artist - Artist - Title.mp3' -> single artist once.
"""
out = []
pat = re.compile(
r"^(?:(?P<num>\d+)\s*-\s*)?(?P<a>[^-]+?)\s-\s(?P=a)\s-\s(?P<rest>.+)$",
re.IGNORECASE,
)
for p in files:
try:
m = pat.match(p.stem)
if m:
prefix = (m.group("num") + " - ") if m.group("num") else ""
new_stem = f"{prefix}{m.group('a').strip()} - {m.group('rest').strip()}"
new_path = p.with_name(new_stem + p.suffix)
if not new_path.exists():
os.replace(p, new_path)
log(f"Renamed (dedup artist): {p.name} -> {new_path.name}")
out.append(new_path)
continue
except Exception:
pass
out.append(p)
return out
def join_via_wav_then_lame(
files: list[Path],
outdir: Path,
sr: int,
br_kbps: int,
join_name: str,
log,
shuffle: bool = False,
keep_temp: bool = False,
progress=None,
) -> Path:
if shuffle:
random.shuffle(files)
log("Join order randomized.")
tmp_wavs: list[Path] = []
try:
total_steps = max(1, len(files) + 2)
step = 0
for idx, f in enumerate(files):
if callable(progress):
pct = int(((step) / total_steps) * 100)
progress(pct, f"Joining: decoding {idx + 1}/{len(files)}...")
wav = outdir / f"._tmp_{f.stem}.wav"
cmd = ["ffmpeg", "-y", "-i", str(f), "-ar", str(sr), str(wav)]
rc = run_quiet(cmd)
if rc != 0:
raise RuntimeError(f"WAV transcode failed for {f.name}")
tmp_wavs.append(wav)
step += 1
if callable(progress):
pct = int(((step) / total_steps) * 100)
progress(pct, "Joining: preparing concat list...")
listfile = outdir / "._concat_list.txt"
listfile.write_text("\n".join(f"file '{w.name}'" for w in tmp_wavs), encoding="utf-8")
if callable(progress):
progress(int(((step) / total_steps) * 100), "Joining: concatenating...")
concat_wav = outdir / "._concat.wav"
rc = run_quiet(
[
"ffmpeg",
"-y",
"-f",
"concat",
"-safe",
"0",
"-i",
str(listfile),
str(concat_wav),
]
)
if rc != 0:
raise RuntimeError("WAV concat failed")
joined = outdir / f"{join_name or 'joined'}.mp3"
rc = run_quiet(
[
"ffmpeg",
"-y",
"-i",
str(concat_wav),
"-codec:a",
"libmp3lame",
"-b:a",
f"{br_kbps}k",
str(joined),
]
)
if rc != 0:
raise RuntimeError("LAME encode failed")
return joined
finally:
if not keep_temp:
for p in tmp_wavs:
try:
p.unlink()
except Exception:
pass
for p in (outdir / "._concat.wav", outdir / "._concat_list.txt"):
try:
p.unlink()
except Exception:
pass
def validate_sample_rates(files: list[Path], expected_sr: int, log) -> None:
mismatches = []
for f in files:
try:
out = run_capture(
[
"ffprobe",
"-v",
"error",
"-select_streams",
"a:0",
"-show_entries",
"stream=sample_rate",
"-of",
"default=noprint_wrappers=1:nokey=1",
str(f),
]
)
sr = int(str(out).strip() or 0)
if sr and sr != expected_sr:
mismatches.append((f.name, sr))
except Exception:
pass
if mismatches:
log("Validation warnings (sample_rate mismatch):")
for name, sr in mismatches:
log(f" - {name}: {sr} Hz (expected {expected_sr})")
# -----------------------
# File Tasks
# -----------------------
def _sanitize_filename_component(name: str) -> str:
"""
Replace characters that commonly break on Windows/shells/old players.
Keeps readability; collapses runs of separators; trims trailing dots/spaces.
"""
import re
# Remove control chars
name = "".join(ch for ch in name if ch >= " " and ch != "\x7f")
# Replace reserved / risky chars with hyphen
name = re.sub(r'[<>:"/\\|?*]', "-", name) # filesystem reserved
name = re.sub(r"[&;$'\"()!]", "-", name) # shell specials
name = re.sub(r"[\[\]{}#%,]", "-", name) # playlist/software quirks
# Collapse whitespace and hyphens
name = re.sub(r"\s+", " ", name)
name = re.sub(r"[-\s]{2,}", " ", name)
# Strip dangerous trailing chars
name = name.strip(" .-_")
# Guard empty
return name or "untitled"
def _sanitize_and_rename(files: list[Path], log) -> list[Path]:
out = []
seen = set()
for p in files:
stem = p.stem
san = _sanitize_filename_component(stem)
cand = p.with_name(san + p.suffix)
# Avoid collisions
i = 1
while cand.exists() and cand != p:
cand = p.with_name(f"{san}_{i}{p.suffix}")
i += 1
if cand != p:
try:
p.rename(cand)
log(f"Renamed (sanitized): {p.name} -> {cand.name}")
except Exception as e:
log(f"Sanitize/rename failed for {p.name}: {e}")
cand = p
# Track unique
if cand.name in seen:
# append numeric to avoid duplicates within run
j = 1
alt = cand.with_name(f"{cand.stem}_{j}{cand.suffix}")
while alt.exists():
j += 1
alt = cand.with_name(f"{cand.stem}_{j}{cand.suffix}")
try:
cand.rename(alt)
log(f"Adjusted duplicate: {cand.name} -> {alt.name}")
cand = alt
except Exception:
pass
seen.add(cand.name)
out.append(cand)
return out
# -----------------------
# Tagging with mutagen (if installed)
# -----------------------
def _parse_artist_title_trackno(path: Path) -> tuple[str | None, str | None, str | None]:
stem = path.stem
parts = stem.split(" - ")
i = 0
trackno = None
if parts and parts[0].isdigit():
trackno = parts[0]
i = 1
if len(parts) - i >= 2:
artist = parts[i].strip()
title = " - ".join(parts[i + 1 :]).strip()
return artist or None, title or None, trackno
return None, stem, trackno