-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyt_audio_backup_gui.py
More file actions
1429 lines (1293 loc) · 53.1 KB
/
yt_audio_backup_gui.py
File metadata and controls
1429 lines (1293 loc) · 53.1 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
#!/usr/bin/env python3
"""
YT Audio Workbench (formerly "YT Audio Backup")
Reliability-first wrapper around yt-dlp + ffmpeg (+ optional mp3gain).
Key features
- Robust logs (GUI + file), progress bar, cancellable runs
- JSON → Netscape cookies converter with header + httpOnly handling
- Cookies validator with auto-repair of missing header (logs the repair)
- Optional join into a single MP3, with CUE + ID3 chapters (mutagen)
- MP3Gain normalization if present, detected even if not on PATH
- Verify Tools & Check/Install Deps (winget, plus helper scripts for macOS/Linux)
- Config persistence for all options
- Optional "Verbose yt-dlp logging" checkbox
"""
from __future__ import annotations
import os
import sys
import json
import time
import threading
from workbench_core import (
# Process Helpers
run_capture,
CANCEL_EVENT,
terminate_all_procs,
verify_tools,
check_and_install_deps,
have,
spawn_streaming,
# File & Audio Logic
_sanitize_and_rename,
_dedup_artist_in_filenames,
join_via_wav_then_lame,
validate_sample_rates,
write_id3_tags_mutagen,
# Playlist & Chapter Logic
write_playlist,
write_cue_for_joined,
embed_id3_chapters,
write_vlc_segment_playlist,
# Cookie Logic
convert_cookie_editor_json_to_netscape,
validate_netscape_cookiefile,
# Task Runner Surface
ProcessingOptions,
)
from tooltips import attach_tooltip, reset_tooltips
import subprocess
import queue
from pathlib import Path
import tkinter as tk
from help_window import open_help_window, show_about_dialog, set_app_meta
# Robustly import the local help_window.py to avoid clashes on sys.path
import importlib.util
_HELP_MOD_PATH = Path(__file__).with_name("help_window.py")
_hw = None
try:
import help_window as _hw # may be a different module on sys.path
except Exception:
_hw = None
if (
not _hw
or not getattr(_hw, "__file__", "")
or Path(_hw.__file__).resolve() != _HELP_MOD_PATH.resolve()
):
_spec = importlib.util.spec_from_file_location("awb_help_window", str(_HELP_MOD_PATH))
_mod = importlib.util.module_from_spec(_spec)
assert _spec and _spec.loader, "Failed to create loader for help_window.py"
_spec.loader.exec_module(_mod)
open_help_window = _mod.open_help_window
show_about_dialog = _mod.show_about_dialog
set_app_meta = _mod.set_app_meta
else:
open_help_window = _hw.open_help_window
show_about_dialog = _hw.show_about_dialog
set_app_meta = _hw.set_app_meta
from i18n import Language
from tkinter import ttk, filedialog, messagebox
from tkinter.scrolledtext import ScrolledText
from pathlib import Path
from ui_extras import add_help_right_aligned_menu
APP_NAME = "YT Audio Workbench"
VERSION = "0.1.8n-p9b"
# ---------- yt-dlp logger ----------
class YDLLogger:
def __init__(self, logfunc, verbose: bool = False):
# Debug: log which help_window module file is in use
try:
self.log(f"help_window module: {getattr(open_help_window, '__module__', '?')}")
except Exception:
pass
self._log = logfunc
self._verbose = verbose
def debug(self, msg):
if self._verbose:
self._log(str(msg))
def info(self, msg):
self._log(str(msg))
def warning(self, msg):
self._log("WARNING: " + str(msg))
def error(self, msg):
self._log("ERROR: " + str(msg))
# ---------- media helpers ----------
def _relayout_checks(container, widgets, min_col_width=220, max_cols=4):
try:
width = max(container.winfo_width(), 1)
except Exception:
width = 800
cols = max(1, min(max_cols, width // min_col_width))
for w in widgets:
try:
w.grid_forget()
except Exception:
pass
for c in range(cols):
try:
container.grid_columnconfigure(c, weight=1, uniform="checks")
except Exception:
pass
for i, w in enumerate(widgets):
r, c = divmod(i, cols)
w.grid(row=r, column=c, sticky="w", padx=6, pady=2)
def _bind_responsive(container, widgets, min_col_width=220, max_cols=4):
container.bind(
"<Configure>", lambda e: _relayout_checks(container, widgets, min_col_width, max_cols)
)
# ---------- GUI ----------
class App(tk.Tk):
def _t(self, key: str, fallback: str) -> str:
try:
return self.lang.get(key, fallback) or fallback
except Exception:
return fallback
def _clear_main_ui(self) -> None:
try:
for child in list(self.winfo_children()):
# Keep the help bar (right-aligned Help menu) if present
if hasattr(self, "help_bar") and child is self.help_bar:
continue
try:
child.destroy()
except Exception:
pass
except Exception:
pass
def _rebuild_main_ui(self) -> None:
# Rebuild everything below the help bar using the current language
self._clear_main_ui()
try:
self._build_ui()
except Exception as e:
try:
# Fall back to at least keeping the window usable
from tkinter import messagebox
messagebox.showerror("Rebuild UI", f"Failed to rebuild UI: {e}")
except Exception:
pass
def _rebuild_help_bar(self):
try:
if hasattr(self, "help_bar") and self.help_bar and self.help_bar.winfo_exists():
self.help_bar.destroy()
except Exception:
pass
try:
from ui_extras import add_help_right_aligned_menu
self.help_bar = add_help_right_aligned_menu(
self,
app_name=APP_NAME,
version=VERSION,
help_md_path=Path(__file__).parent / "docs" / "HELP.md",
get_text=lambda k, f: self.lang.get(k, f),
locales=self.lang.available_locales(),
on_switch_language=self.change_language,
)
except Exception:
pass
def change_language(self, code: str, persist: bool = True) -> None:
try:
self.lang.load(code)
self.current_language = code
except Exception:
return
# Update window title
try:
self.title(f"{self.lang.get('app_title', 'YT Audio Workbench')} v{VERSION}")
except Exception:
pass
# Rebuild help bar to update menu labels and selection
self._rebuild_help_bar()
# Reset tooltips before UI rebuild to prevent after() against destroyed widgets
try:
reset_tooltips()
except Exception:
pass
# Only write config when this is a real user-initiated language change
if persist:
try:
self._save_config()
except Exception:
pass
# Rebuild the rest of the UI with the current language
self._rebuild_main_ui()
def _center_main_on_screen(self):
try:
if getattr(self, '_did_initial_center', False):
return
self._did_initial_center = True
self.update_idletasks()
w = self.winfo_width() or self.winfo_reqwidth() or 1000
h = self.winfo_height() or self.winfo_reqheight() or 780
self.geometry(f"{w}x{h}")
self.state('zoomed')
except Exception:
pass
def _worker_task(self, options: ProcessingOptions) -> None:
"""The main processing pipeline that runs in a background thread."""
# Define local helpers for thread-safe communication with the GUI
def _log(msg: str):
self._log_q.put(msg)
def _progress(pct: int | None, status: str | None):
self._progress_q.put({"pct": pct, "status": status})
try:
_log("Starting run...")
_progress(0, "Preparing...")
outdir = options.run_sub_dir
outdir.mkdir(exist_ok=True, parents=True)
# 1. Prepare Cookies from core
cookies_arg = None
if options.cookies_file and options.cookies_file.exists():
if options.cookies_file.suffix.lower() == ".json":
txt_path = outdir / "cookies.txt"
options.cookies_file = convert_cookie_editor_json_to_netscape(
options.cookies_file, txt_path, _log
)
ok, msg = validate_netscape_cookiefile(options.cookies_file, _log)
if ok:
cookies_arg = str(options.cookies_file)
else:
_log(f"Cookie file issue: {msg}")
# 2. Build yt-dlp command
outtmpl = self._build_outtmpl(
options.numbering, options.fallback_numbering, options.include_id
)
cmd = [
"yt-dlp",
"-x",
"--audio-format",
"mp3",
"--audio-quality",
"0",
"-o",
str(outdir / outtmpl),
"--no-simulate",
"--no-playlist-reverse",
"--ignore-errors",
"--progress",
]
if options.use_archive:
cmd.extend(["--download-archive", str(outdir / "archive.txt")])
if options.sleep_between > 0:
cmd.extend(["--sleep-interval", str(options.sleep_between)])
if options.verbose_ydl:
cmd.append("--verbose")
if options.hi_integrity:
_log("High integrity mode enabled: wip - to be implemented.")
cmd.extend([""])
if cookies_arg:
cmd.extend(["--cookies", cookies_arg])
elif options.cookies_browser and options.cookies_browser.lower() != "none":
cmd.extend(["--cookies-from-browser", options.cookies_browser.lower()])
cmd.append(options.url)
# 3. Run yt-dlp and stream output for progress
_log(f"Running command: {' '.join(cmd)}")
_progress(5, "Downloading...")
p = spawn_streaming(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
downloaded_files = []
if p.stdout:
for line in p.stdout:
if CANCEL_EVENT.is_set():
p.terminate()
break
line = line.strip()
if line:
_log(line)
if "[download] Destination:" in line:
fpath = Path(line.split("Destination:", 1)[1].strip())
if fpath.exists():
downloaded_files.append(fpath)
elif "[download]" in line and "% of" in line:
pct = int(float(line.split("%")[0].split()[-1]))
_progress(5 + int(pct * 0.75), f"Downloading: {pct}%")
p.wait()
if CANCEL_EVENT.is_set():
raise InterruptedError("Run cancelled by user.")
# 4. Post-processing using core functions
_progress(80, "Post-processing...")
if not downloaded_files:
_log("No new files were downloaded.")
else:
_log(f"Downloaded {len(downloaded_files)} files. Post-processing...")
if options.sanitize_filenames:
downloaded_files = _sanitize_and_rename(downloaded_files, _log)
if options.embed_metadata:
album_name = outdir.name.replace(f"run_{outdir.name.split('_')[-1]}", "").strip(
" _-"
)
write_id3_tags_mutagen(downloaded_files, album_name, _log)
if options.dedup_artist:
downloaded_files = _dedup_artist_in_filenames(downloaded_files, _log)
if options.validate_sr:
validate_sample_rates(downloaded_files, options.sample_rate, _log)
if options.join:
_progress(85, "Joining files...")
joined_file = join_via_wav_then_lame(
files=downloaded_files,
outdir=outdir,
sr=options.sample_rate,
br_kbps=options.bitrate,
join_name=options.join_name,
log=_log,
shuffle=options.random_join,
keep_temp=options.keep_temp_wavs,
progress=_progress,
)
if options.write_cue:
write_cue_for_joined(joined_file, downloaded_files, _log)
if options.embed_chapters:
embed_id3_chapters(joined_file, downloaded_files, _log)
if options.vlc_segments:
write_vlc_segment_playlist(joined_file, downloaded_files, outdir, _log)
if options.playlist_format:
write_playlist(
outdir, downloaded_files, _log, "playlist", options.playlist_format
)
if options.mp3gain:
_progress(95, "Normalizing volume...")
_log("Applying MP3Gain normalization...")
# Note: We need to import 'have' from the core for this to work
if have("mp3gain"):
files_to_normalize = (
[joined_file]
if options.join and 'joined_file' in locals()
else downloaded_files
)
for f in files_to_normalize:
try:
run_capture(["mp3gain", "-r", "-k", "-p", str(f)])
except Exception as e:
_log(f"mp3gain failed for {f.name}: {e}")
else:
_log("mp3gain not found, skipping normalization.")
_progress(100, "Done")
_log("Run finished successfully.")
except InterruptedError as e:
_log(str(e))
_progress(None, "Cancelled")
except Exception as e:
_log(f"FATAL ERROR in worker thread: {e}")
_progress(None, f"Error: {e}")
finally:
# Schedule the UI update back on the main thread
self.after(0, self._finish_run)
def __init__(self) -> None:
# Initialize language manager and current language
try:
lang_dir = Path(__file__).with_name("lang")
except Exception:
from pathlib import Path as _P
lang_dir = _P("lang")
self.lang = Language(lang_dir, code="en")
self.current_language = self.lang.code
super().__init__()
self.title(f"{self.lang.get('app_title', 'YT Audio Workbench')} v{VERSION}")
try:
set_app_meta(self.title(), VERSION)
except Exception:
pass
# Set Help/About metadata
try:
set_app_meta(self.title(), "0.1.8n-p9b")
except Exception:
pass
w, h = 1000, 780
x = (self.winfo_screenwidth() - w) // 2
y = (self.winfo_screenheight() - h) // 2
self.geometry(f"{w}x{h}+{x}+{y}")
self.minsize(w, h)
# Help menu (right-aligned bar) + F1
try:
self.help_bar = add_help_right_aligned_menu(
self,
app_name=APP_NAME,
version=VERSION,
help_md_path=Path(__file__).parent / "docs" / "HELP.md",
get_text=lambda k, f: self.lang.get(k, f),
locales=self.lang.available_locales(),
on_switch_language=self.change_language,
)
except Exception:
pass
# Vars
self.url_var = tk.StringVar()
self.out_var = tk.StringVar()
self.sample_rate_var = tk.IntVar(value=44100)
self.bitrate_var = tk.IntVar(value=192)
self.embed_meta_var = tk.BooleanVar(value=True)
self.archive_var = tk.BooleanVar(value=False)
self.include_id_var = tk.BooleanVar(value=False)
self.numbering_var = tk.BooleanVar(value=True)
self.fallback_numbering_var = tk.BooleanVar(value=True)
self.join_var = tk.BooleanVar(value=False)
self.join_name_var = tk.StringVar(value="joined")
self.write_cue_var = tk.BooleanVar(value=True)
self.embed_chapters_var = tk.BooleanVar(value=True)
self.vlc_segments_var = tk.BooleanVar(value=False)
self.random_join_var = tk.BooleanVar(value=False)
self.sleep_between_var = tk.IntVar(value=3)
self.detailed_log_var = tk.BooleanVar(value=True)
self.rolling_log_var = tk.BooleanVar(value=False)
self.verbose_ydl_var = tk.BooleanVar(value=False)
self.mp3gain_var = tk.BooleanVar(value=True)
self.hi_integrity_var = tk.BooleanVar(value=False)
self.keep_temp_var = tk.BooleanVar(value=False)
self.dedup_artist_var = tk.BooleanVar(value=False)
self.sanitize_names_var = tk.BooleanVar(value=True)
self.validate_sr_var = tk.BooleanVar(value=True)
self.playlist_format_var = tk.StringVar(value="M3U8")
self.use_run_subdir_var = tk.BooleanVar(value=True)
self.cookies_file_var = tk.StringVar()
self.cookies_browser_var = tk.StringVar(value="None")
self.status_var = tk.StringVar(value="Idle")
self.progress_var = tk.IntVar(value=0)
self._progress_q: queue.Queue[dict] = queue.Queue()
self._progress_total = 0
self._progress_done = 0
self._log_q: queue.Queue[str] = queue.Queue()
self.file_log_fp = None
self.log_file_path: Path | None = None
# Hide during initial layout to avoid off-center flash
try:
self.withdraw()
except Exception:
pass
try:
self._load_config()
except Exception:
pass
self._build_ui()
# Enforce minimum window size (height)
try:
self.update_idletasks()
w = 820
h = 780
self.minsize(w, h)
except Exception:
pass
self._cookies_loaded_once = False
# Start pollers early so logs show for all actions
self.after(100, self._poll_log)
self.after(100, self._poll_progress)
# Apply saved locale after config load (reuse existing change_language path)
try:
saved_locale = None
if hasattr(self, "config") and isinstance(self.config, dict):
for _k in ("locale", "language", "lang"):
_v = self.config.get(_k)
if isinstance(_v, str) and _v.strip():
saved_locale = _v.strip()
break
if saved_locale and saved_locale != getattr(self, "current_language", None):
self.change_language(saved_locale)
except Exception:
pass
self.protocol("WM_DELETE_WINDOW", self._on_close)
# Center main window once UI is realized
try:
self.after(0, self._center_main_on_screen)
except Exception:
pass
# Layout → compute size → center → show
try:
self.update_idletasks()
self._center_main_on_screen()
self.deiconify()
except Exception:
try:
self.deiconify()
except Exception:
pass
def _build_ui(self) -> None:
pad = dict(padx=8, pady=6)
# URL
urlf = ttk.LabelFrame(self, text=self._t("frames.url", "URL"))
urlf.pack(fill="x", **pad)
ttk.Label(urlf, text=self._t("labels.url", "Playlist or video URL:")).pack(side="left")
url_entry = ttk.Entry(urlf, textvariable=self.url_var)
url_entry.pack(side="left", fill="x", expand=True, padx=6)
attach_tooltip(
url_entry,
lambda: self._t("tooltips.url_entry", "Paste a YouTube video or playlist URL."),
)
ttk.Button(urlf, text="Paste", command=lambda: self._paste_into(url_entry)).pack(
side="left"
)
self._attach_context_menu(url_entry, "entry")
# Output
outf = ttk.LabelFrame(self, text=self._t("frames.output", "Output"))
outf.pack(fill="x", **pad)
ttk.Label(outf, text="Output folder:").pack(side="left")
out_entry = ttk.Entry(outf, textvariable=self.out_var)
out_entry.pack(side="left", fill="x", expand=True, padx=6)
attach_tooltip(
out_entry,
lambda: self._t(
"tooltips.output_folder_entry", "Choose where to save downloads and logs."
),
)
browse_out_btn = ttk.Button(
outf, text=self._t("buttons.browse", "Browse…"), command=self._browse_out
)
browse_out_btn.pack(side="left")
attach_tooltip(
browse_out_btn,
lambda: self._t("tooltips.browse_output_button", "Choose the output folder."),
)
self._attach_context_menu(out_entry, "path")
use_run_cb = ttk.Checkbutton(
outf,
text=self._t("checkboxes.use_run_subdir", "Use per-run subfolder (recommended)"),
variable=self.use_run_subdir_var,
)
use_run_cb.pack(side="left", padx=8)
attach_tooltip(
use_run_cb, lambda: self._t("tooltips.use_run_subdir", "Create per-run subfolder.")
)
# Cookies
cookies = ttk.LabelFrame(self, text=self._t("frames.cookies", "Cookies"))
cookies.pack(fill="x", **pad)
c1 = ttk.Frame(cookies)
c1.pack(fill="x")
ttk.Label(c1, text=self._t("labels.cookies_file", "Cookies file (.txt or JSON)")).pack(
side="left"
)
ttk.Label(c1, text=" or ").pack(side="left")
ttk.Label(c1, text=self._t("labels.cookies_browser", "Use browser cookies:")).pack(
side="left"
)
self.cookies_browser_cb = ttk.Combobox(
c1,
textvariable=self.cookies_browser_var,
values=["None", "Chrome", "Edge", "Firefox"],
width=8,
state="readonly",
)
attach_tooltip(
self.cookies_browser_cb,
lambda: self._t(
"tooltips.cookies_browser",
"Use cookies from a supported browser (Chrome/Edge/Firefox).",
),
)
self.cookies_browser_cb.pack(side="left", padx=6)
c2 = ttk.Frame(cookies)
c2.pack(fill="x")
self.cookies_file_entry = ttk.Entry(c2, textvariable=self.cookies_file_var)
self.cookies_file_entry.pack(side="left", fill="x", expand=True, padx=6)
attach_tooltip(
self.cookies_file_entry,
lambda: self._t(
"tooltips.cookies_file_entry",
"A cookies.txt (Netscape format) or Cookie-Editor JSON file.",
),
)
browse_cookies_btn = ttk.Button(
c2, text=self._t("buttons.browse", "Browse…"), command=self._browse_cookies
)
browse_cookies_btn.pack(side="left")
attach_tooltip(
browse_cookies_btn,
lambda: self._t("tooltips.browse_cookies_button", "Select a cookies file."),
)
self._attach_context_menu(self.cookies_file_entry, "path")
# Options
opts = ttk.LabelFrame(
self, text=self._t("frames.download_formatting", "Download & Formatting")
)
opts.pack(fill="x", **pad)
rate_frame = ttk.Frame(opts)
rate_frame.pack(fill="x", pady=6)
ttk.Label(rate_frame, text=self._t("labels.sample_rate", "Sample rate:")).pack(side="left")
sr_combo = ttk.Combobox(
rate_frame,
textvariable=self.sample_rate_var,
values=[44100, 48000],
width=8,
state="readonly",
)
sr_combo.pack(side="left", padx=6)
attach_tooltip(
sr_combo,
lambda: self._t(
"tooltips.sample_rate_combo", "Enforces a consistent audio sample rate."
),
)
ttk.Label(rate_frame, text=self._t("labels.bitrate", "Bitrate (kbps):")).pack(side="left")
br_combo = ttk.Combobox(
rate_frame,
textvariable=self.bitrate_var,
values=[192, 256, 320],
width=6,
state="readonly",
)
br_combo.pack(side="left", padx=6)
attach_tooltip(br_combo, lambda: self._t("tooltips.bitrate_combo", "Sets the MP3 bitrate."))
ttk.Label(
rate_frame, text=self._t("labels.delay_between_items", "Delay between items (s):")
).pack(side="left")
delay_spin = ttk.Spinbox(
rate_frame, textvariable=self.sleep_between_var, from_=0, to=30, width=4
)
delay_spin.pack(side="left", padx=6)
attach_tooltip(
delay_spin, lambda: self._t("tooltips.delay_spinbox", "Delay between playlist items.")
)
pf = ttk.Frame(opts)
pf.pack(fill="x", pady=4)
ttk.Label(pf, text=self._t("labels.playlist_format", "Playlist format:")).pack(side="left")
plf_combo = ttk.Combobox(
pf,
textvariable=self.playlist_format_var,
values=["M3U8", "M3U", "Both"],
width=8,
state="readonly",
)
plf_combo.pack(side="left", padx=6)
attach_tooltip(
plf_combo,
lambda: self._t("tooltips.playlist_format_combo", "Choose output playlist format."),
)
chk_frame = ttk.LabelFrame(
self, text=self._t("frames.filename_options", "Filename & Options")
)
chk_frame.pack(fill="x", **pad)
chk_grid = ttk.Frame(chk_frame)
chk_grid.pack(fill="x")
checks = [
(
self._t("checkboxes.add_numbering", "Add numbering"),
self.numbering_var,
"tooltips.checkboxes.add_numbering",
),
(
self._t("checkboxes.fallback_numbering", "Fallback numbering when not a playlist"),
self.fallback_numbering_var,
"tooltips.checkboxes.fallback_numbering",
),
(
self._t("checkboxes.include_id", "Include YouTube ID in filename"),
self.include_id_var,
"tooltips.checkboxes.include_id",
),
(
self._t("checkboxes.dedup_artist", "De-duplicate artist in filename"),
self.dedup_artist_var,
"tooltips.checkboxes.dedup_artist",
),
(
self._t("checkboxes.sanitize_filenames", "Sanitize filenames (max compatibility)"),
self.sanitize_names_var,
"tooltips.checkboxes.sanitize_filenames",
),
(
self._t("checkboxes.embed_metadata", "Embed metadata"),
self.embed_meta_var,
"tooltips.checkboxes.embed_metadata",
),
(
self._t("checkboxes.use_archive", "Use download archive"),
self.archive_var,
"tooltips.checkboxes.use_archive",
),
(
self._t("checkboxes.mp3gain_normalize", "Normalize with MP3Gain"),
self.mp3gain_var,
"tooltips.checkboxes.mp3gain_normalize",
),
(
self._t("checkboxes.validate_sr", "Validate with ffprobe"),
self.validate_sr_var,
"tooltips.checkboxes.validate_sr",
),
(
self._t("checkboxes.verbose_ydl", "Verbose yt-dlp logging"),
self.verbose_ydl_var,
"tooltips.checkboxes.verbose_ydl",
),
]
check_widgets = []
for label, var, tip_key in checks:
w = ttk.Checkbutton(chk_grid, text=label, variable=var)
attach_tooltip(w, lambda k=tip_key: self._t(k, "Tooltip not found."))
check_widgets.append(w)
_relayout_checks(chk_grid, check_widgets, min_col_width=200, max_cols=4)
_bind_responsive(chk_grid, check_widgets, min_col_width=200, max_cols=4)
joinf = ttk.LabelFrame(self, text=self._t("frames.joining", "Joining"))
joinf.pack(fill="x", **pad)
name_frame = ttk.Frame(joinf)
name_frame.pack(fill="x")
join_cb = ttk.Checkbutton(
name_frame,
text=self._t("checkboxes.join_into_one", "Join into one MP3"),
variable=self.join_var,
)
join_cb.pack(side="left")
attach_tooltip(
join_cb,
lambda: self._t(
"tooltips.checkboxes.join_into_one", "Join multiple items into a single MP3 album."
),
)
ttk.Label(name_frame, text="Name:").pack(side="left", padx=(12, 0))
self.join_name_entry = ttk.Entry(name_frame, textvariable=self.join_name_var, width=32)
self.join_name_entry.pack(side="left", padx=6)
attach_tooltip(
self.join_name_entry,
lambda: self._t("tooltips.join_name_entry", "Name for the final joined MP3 / album."),
)
self._attach_context_menu(self.join_name_entry, "entry")
chk_frame2 = ttk.Frame(joinf)
chk_frame2.pack(fill="x")
join_checks = [
(
self._t("checkboxes.write_cue", "Write CUE for joined file"),
self.write_cue_var,
"tooltips.checkboxes.write_cue",
),
(
self._t("checkboxes.embed_chapters", "Embed ID3 chapters in joined file"),
self.embed_chapters_var,
"tooltips.checkboxes.embed_chapters",
),
(
self._t(
"checkboxes.vlc_segments",
"Write VLC segments playlist for joined file (M3U with EXTVLCOPT)",
),
self.vlc_segments_var,
"tooltips.checkboxes.vlc_segments",
),
(
self._t("checkboxes.random_join", "Randomize order when joining"),
self.random_join_var,
"tooltips.checkboxes.randomize_order",
),
(
self._t("checkboxes.keep_temp_wavs", "Keep temp WAVs"),
self.keep_temp_var,
"tooltips.checkboxes.keep_temp_wavs",
),
]
join_widgets = []
for label, var, tip_key in join_checks:
wj = ttk.Checkbutton(chk_frame2, text=label, variable=var)
attach_tooltip(wj, lambda k=tip_key: self._t(k, "Tooltip not found."))
join_widgets.append(wj)
_relayout_checks(chk_frame2, join_widgets, min_col_width=200, max_cols=4)
_bind_responsive(chk_frame2, join_widgets, min_col_width=200, max_cols=4)
deps = ttk.LabelFrame(self, text=self._t("frames.system_deps", "System Dependencies"))
deps.pack(fill="x", **pad)
ttk.Label(deps, text="Ensure ffmpeg/ffprobe and optional mp3gain are installed.").pack(
side="left"
)
self.verify_btn = ttk.Button(
deps,
text=self._t("buttons.verify_tools", "Verify Tools"),
command=self._verify_tools_async,
)
self.verify_btn.pack(side="right", padx=6)
attach_tooltip(
self.verify_btn,
lambda: self._t(
"tooltips.verify_tools_button", "Check whether required tools are installed."
),
)
self.check_deps_btn = ttk.Button(
deps,
text=self._t("buttons.check_install_deps", "Check & Install System Deps"),
command=self._check_and_install_deps_async,
)
self.check_deps_btn.pack(side="right")
attach_tooltip(
self.check_deps_btn,
lambda: self._t(
"tooltips.check_deps_button", "Attempt to install missing dependencies."
),
)
ctrls = ttk.Frame(self)
ctrls.pack(fill="x", **pad)
self.run_btn = ttk.Button(ctrls, text=self._t("buttons.run", "Run"), command=self._run)
self.run_btn.pack(side="left")
attach_tooltip(
self.run_btn, lambda: self._t("tooltips.run_button", "Start the processing pipeline.")
)
self.cancel_btn = ttk.Button(
ctrls, text=self._t("buttons.cancel", "Cancel"), command=self._cancel, state="disabled"
)
self.cancel_btn.pack(side="left", padx=6)
attach_tooltip(
self.cancel_btn, lambda: self._t("tooltips.cancel_button", "Cancel the current run.")
)
ttk.Label(ctrls, textvariable=self.status_var).pack(side="right")
pb = ttk.Progressbar(
ctrls, orient="horizontal", mode="determinate", variable=self.progress_var, length=260
)
pb.pack(side="right", padx=12)
logf = ttk.LabelFrame(self, text=self._t("frames.log", "Log"))
logf.pack(fill="both", expand=True, **pad)
self.log_txt = ScrolledText(logf, height=6, wrap="word")
self.log_txt.pack(fill="both", expand=True)
self.log_txt.configure(state="disabled")
self._attach_context_menu(self.log_txt, "text")
log_tools = ttk.Frame(logf)
log_tools.pack(fill="x")
open_log_btn = ttk.Button(
log_tools,
text=self._t("buttons.open_log", "View Log File"),
command=self._view_log_file,
)
open_log_btn.pack(side="left")
attach_tooltip(
open_log_btn,
lambda: self._t("tooltips.open_log_button", "Open the current run’s log file."),
)
copy_log_btn = ttk.Button(
log_tools, text="Copy Log to Clipboard", command=self._copy_log_to_clipboard
)
copy_log_btn.pack(side="left", padx=6)
attach_tooltip(
copy_log_btn,
lambda: self._t("tooltips.copy_log_button", "Copy log contents to clipboard."),
)
# ---------- small UI helpers ---------- ----------
def refresh_i18n_ui(self) -> None:
"""
Re-apply the current language to all UI elements WITHOUT persisting config.
Useful right after the window is realized to get rid of any fallback labels.
"""
try:
# Reuse the same path as language switching, but don't write config
self.change_language(self.current_language, persist=False)
except Exception:
# As a fallback, at least rebuild the visible parts with current lang
try:
self._rebuild_help_bar()
self._rebuild_main_ui()
except Exception:
pass
def _attach_context_menu(self, widget, kind: str = "entry") -> None:
menu = tk.Menu(widget, tearoff=0)
def popup(e):
try:
menu.delete(0, "end")
if kind in ("entry", "path"):
menu.add_command(label="Cut", command=lambda: widget.event_generate("<<Cut>>"))
menu.add_command(
label="Copy", command=lambda: widget.event_generate("<<Copy>>")
)
menu.add_command(
label="Paste", command=lambda: widget.event_generate("<<Paste>>")
)
menu.add_separator()
menu.add_command(
label="Select All",
command=lambda: (widget.select_range(0, "end"), widget.icursor("end")),
)
if kind == "path":
menu.add_separator()
menu.add_command(
label="Open Folder / Path",
command=lambda: self._open_path_in_os(widget.get()),
)
elif kind == "text":
menu.add_command(
label="Copy", command=lambda: self._copy_text_selection(widget)
)
menu.add_command(label="Copy All", command=lambda: self._copy_text_all(widget))
menu.add_separator()
menu.add_command(
label="Select All", command=lambda: widget.tag_add("sel", "1.0", "end-1c")
)
menu.add_command(
label="Clear Log", command=lambda: self._clear_text_widget(widget)
)
menu.tk.call("tk_popup", menu, e.x_root, e.y_root)
finally:
try:
menu.grab_release()
except Exception:
pass
for seq in ("<Button-3>", "<Button-2>", "<Control-Button-1>"):
try:
widget.bind(seq, popup, add="+")
except Exception:
pass
def _open_path_in_os(self, path_str: str) -> None:
p = (path_str or "").strip().strip('"').strip("'")
if not p:
return