File size: 28,864 Bytes
bcc5768
 
 
7273fb2
bcc5768
 
 
 
7273fb2
bcc5768
 
 
 
 
7273fb2
bcc5768
c5ce6ea
bcc5768
 
 
 
c5ce6ea
 
bcc5768
 
 
 
 
 
 
 
c5ce6ea
 
bcc5768
 
 
 
 
 
 
 
c5ce6ea
 
bcc5768
 
c5ce6ea
 
bcc5768
 
 
 
 
 
 
c5ce6ea
 
bcc5768
 
 
c5ce6ea
bcc5768
 
 
c5ce6ea
bcc5768
 
c5ce6ea
bcc5768
 
 
 
c5ce6ea
bcc5768
 
 
 
 
 
 
 
7273fb2
bcc5768
 
 
c5ce6ea
bcc5768
 
 
 
7273fb2
bcc5768
 
 
 
c5ce6ea
bcc5768
 
 
c5ce6ea
bcc5768
c5ce6ea
7273fb2
bcc5768
 
 
 
 
 
7273fb2
bcc5768
 
 
c5ce6ea
bcc5768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5ce6ea
bcc5768
c5ce6ea
7273fb2
bcc5768
 
 
 
 
 
c5ce6ea
bcc5768
 
 
 
 
 
 
 
 
 
 
 
 
 
c5ce6ea
bcc5768
 
 
c5ce6ea
bcc5768
7273fb2
bcc5768
 
7273fb2
bcc5768
 
 
7273fb2
bcc5768
7273fb2
 
bcc5768
 
 
 
 
 
c5ce6ea
bcc5768
 
 
 
 
 
 
 
 
c5ce6ea
bcc5768
 
 
 
 
 
c5ce6ea
bcc5768
c5ce6ea
 
bcc5768
 
 
 
 
 
7273fb2
bcc5768
 
 
c5ce6ea
bcc5768
 
 
 
c5ce6ea
bcc5768
 
 
 
 
 
c5ce6ea
 
bcc5768
 
 
c5ce6ea
bcc5768
 
 
 
c5ce6ea
bcc5768
 
 
 
 
 
 
 
7273fb2
bcc5768
 
 
 
7273fb2
bcc5768
 
 
 
 
 
 
7273fb2
bcc5768
 
 
c5ce6ea
bcc5768
 
 
c5ce6ea
bcc5768
 
 
 
 
 
 
 
 
 
7273fb2
bcc5768
 
7273fb2
bcc5768
 
 
 
 
 
 
 
 
 
 
 
7273fb2
bcc5768
 
7273fb2
6a0177f
bcc5768
 
 
c5ce6ea
bcc5768
 
 
c5ce6ea
bcc5768
 
 
 
c5ce6ea
bcc5768
 
 
 
 
 
 
 
 
 
 
 
 
c5ce6ea
bcc5768
 
 
 
 
 
 
c5ce6ea
bcc5768
 
c5ce6ea
bcc5768
 
 
 
 
 
 
 
 
 
 
 
c5ce6ea
 
bcc5768
 
 
 
 
 
 
 
 
c5ce6ea
bcc5768
c5ce6ea
bcc5768
 
 
c5ce6ea
bcc5768
c5ce6ea
bcc5768
 
 
c5ce6ea
bcc5768
 
 
c5ce6ea
bcc5768
 
 
 
 
c5ce6ea
bcc5768
 
7273fb2
bcc5768
 
 
 
 
 
c5ce6ea
bcc5768
 
 
 
 
 
 
 
 
 
c5ce6ea
bcc5768
 
 
fa56eeb
bcc5768
fa56eeb
bcc5768
 
 
 
 
fa56eeb
bcc5768
 
 
 
 
fa56eeb
bcc5768
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
 
 
 
fa56eeb
bcc5768
 
 
 
 
fa56eeb
bcc5768
fa56eeb
bcc5768
 
 
 
fa56eeb
 
bcc5768
 
 
 
 
 
 
 
fa56eeb
 
bcc5768
 
 
 
 
 
 
 
fa56eeb
 
bcc5768
 
fa56eeb
 
bcc5768
 
 
 
 
 
 
fa56eeb
 
bcc5768
 
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
 
fa56eeb
bcc5768
 
 
 
fa56eeb
bcc5768
 
 
 
 
 
 
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
 
 
 
fa56eeb
bcc5768
 
 
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
fa56eeb
 
bcc5768
 
 
 
 
 
6a0177f
bcc5768
 
 
6a0177f
bcc5768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa56eeb
bcc5768
fa56eeb
 
bcc5768
 
 
 
 
 
fa56eeb
bcc5768
 
 
 
 
 
 
 
 
 
 
 
 
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
fa56eeb
bcc5768
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
fa56eeb
 
bcc5768
 
 
 
 
 
fa56eeb
bcc5768
 
 
 
 
 
 
 
 
fa56eeb
bcc5768
 
 
 
 
 
fa56eeb
bcc5768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa56eeb
 
bcc5768
 
 
fa56eeb
bcc5768
 
 
 
fa56eeb
bcc5768
 
 
 
 
 
 
 
fa56eeb
bcc5768
 
 
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
 
 
 
 
 
 
 
 
 
fa56eeb
bcc5768
 
fa56eeb
bcc5768
 
 
 
 
 
fa56eeb
bcc5768
 
fa56eeb
 
bcc5768
 
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
 
fa56eeb
bcc5768
 
 
 
 
 
 
 
 
 
 
 
 
fa56eeb
bcc5768
 
 
 
 
 
 
fa56eeb
bcc5768
 
fa56eeb
bcc5768
 
 
 
 
 
 
 
fa56eeb
 
bcc5768
 
 
 
 
 
 
 
 
fa56eeb
bcc5768
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
 
 
 
 
fa56eeb
bcc5768
 
fa56eeb
bcc5768
 
 
 
 
 
fa56eeb
bcc5768
 
 
 
 
 
 
 
fa56eeb
bcc5768
 
 
fa56eeb
bcc5768
fa56eeb
bcc5768
 
 
 
 
fa56eeb
bcc5768
 
 
 
 
fa56eeb
bcc5768
 
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
# import re
# from dataclasses import dataclass
# from typing import Any, Dict, List, Tuple, Optional

# import gradio as gr
# from huggingface_hub import list_repo_files, hf_hub_download
# from pydub import AudioSegment
# import numpy as np

# # =========================================================
# # Config
# # =========================================================
# MEDIA_EXTS = (".mp4", ".m4a", ".mp3", ".wav", ".flac", ".ogg", ".aac", ".mov", ".avi")
# VTT_EXTS = (".vtt",)

# DEFAULT_MAX_MID_DIFF = 1.5

# # Normalize audio for stable playback in browsers
# TARGET_SR = 48000
# TARGET_CH = 1          # mono
# TARGET_SW = 2          # 16-bit PCM


# # =========================================================
# # Data structures
# # =========================================================
# @dataclass
# class Cue:
#     start: float
#     end: float
#     text: str


# # =========================================================
# # VTT parsing
# # =========================================================
# _TAG_RE = re.compile(r"</?[^>]+?>", re.IGNORECASE)
# _VTT_TIME_RE = re.compile(
#     r"(?P<start>\d{2}:\d{2}:\d{2}\.\d{3}|\d{1,2}:\d{2}\.\d{3})\s*-->\s*"
#     r"(?P<end>\d{2}:\d{2}:\d{2}\.\d{3}|\d{1,2}:\d{2}\.\d{3})"
# )


# def _strip_tags(text: str) -> str:
#     return _TAG_RE.sub("", text).strip()


# def _time_to_seconds(t: str) -> float:
#     parts = t.split(":")
#     if len(parts) == 3:
#         return int(parts[0]) * 3600 + int(parts[1]) * 60 + float(parts[2])
#     if len(parts) == 2:
#         return int(parts[0]) * 60 + float(parts[1])
#     raise ValueError(f"Bad VTT timestamp: {t}")


# def parse_vtt_file(path: str) -> List[Cue]:
#     with open(path, "r", encoding="utf-8") as f:
#         content = f.read()

#     # Remove BOM / WEBVTT header (if any)
#     content = content.replace("\ufeff", "")
#     content = re.sub(r"^\s*WEBVTT.*?\n", "", content, flags=re.IGNORECASE)

#     blocks = re.split(r"\r?\n\r?\n", content.strip())
#     cues: List[Cue] = []

#     for block in blocks:
#         lines = [l.strip() for l in block.splitlines() if l.strip()]
#         if not lines:
#             continue

#         # Locate the timestamp line (must contain "-->")
#         time_idx: Optional[int] = None
#         for i, line in enumerate(lines):
#             if "-->" in line:
#                 time_idx = i
#                 break
#         if time_idx is None:
#             continue

#         m = _VTT_TIME_RE.search(lines[time_idx])
#         if not m:
#             continue

#         start = _time_to_seconds(m.group("start"))
#         end = _time_to_seconds(m.group("end"))
#         if end <= start:
#             continue

#         # Only take lines after the timestamp line as subtitle text
#         text_lines = lines[time_idx + 1 :]
#         if not text_lines:
#             continue

#         text = _strip_tags("\n".join(text_lines))
#         if text:
#             cues.append(Cue(start=start, end=end, text=text))

#     return sorted(cues, key=lambda x: x.start)


# # =========================================================
# # Alignment (match by mid time), preserve per-track windows
# # =========================================================
# def align_by_time(a: List[Cue], b: List[Cue], th: float) -> List[Dict[str, Any]]:
#     out: List[Dict[str, Any]] = []
#     i, j, idx = 0, 0, 1

#     while i < len(a) and j < len(b):
#         ma = (a[i].start + a[i].end) / 2
#         mb = (b[j].start + b[j].end) / 2

#         if abs(ma - mb) <= th:
#             out.append(
#                 {
#                     "idx": idx,
#                     # Per-track time window (recommended for playback)
#                     "a_start": a[i].start,
#                     "a_end": a[i].end,
#                     "b_start": b[j].start,
#                     "b_end": b[j].end,
#                     # Optional global time window (for comparison/debug)
#                     "start": min(a[i].start, b[j].start),
#                     "end": max(a[i].end, b[j].end),
#                     "a_text": a[i].text,
#                     "b_text": b[j].text,
#                 }
#             )
#             idx += 1
#             i += 1
#             j += 1
#         elif ma < mb:
#             i += 1
#         else:
#             j += 1

#     return out


# # =========================================================
# # Audio slicing -> return (sr, np.int16) for gr.Audio(type="numpy")
# # =========================================================
# def export_segment_numpy(audio: AudioSegment, start: float, end: float) -> Tuple[int, np.ndarray]:
#     """
#     Robust segment export for gr.Audio(type="numpy").

#     Key points:
#     - Clamp start/end (after any offsets) to valid range.
#     - Use *rounded* ms boundaries to avoid systematic truncation drift.
#     - Slice via pydub (ms-accurate) using the original stream timeline.
#     - Normalize to mono/48k/int16 for stable browser playback.
#     - Return (sr, int16 ndarray) to avoid float32 scaling pitfalls.
#     """
#     # Clamp and ensure minimum duration
#     start = float(start)
#     end = float(end)
#     if end < start:
#         start, end = end, start
#     start = max(0.0, start)
#     end = max(start + 0.05, end)

#     # Round to milliseconds (avoid int() truncation bias)
#     start_ms = int(round(start * 1000.0))
#     end_ms = int(round(end * 1000.0))

#     seg = audio[start_ms:end_ms]

#     # Normalize to mono/48k/int16
#     seg = seg.set_channels(TARGET_CH).set_frame_rate(TARGET_SR).set_sample_width(TARGET_SW)

#     arr = np.asarray(seg.get_array_of_samples())
#     if arr.dtype != np.int16:
#         arr = arr.astype(np.int16, copy=False)

#     return TARGET_SR, np.ascontiguousarray(arr)


# # =========================================================
# # Helper: robustly read seg_idx from gr.Dataframe value
# # =========================================================
# def _get_seg_idx_from_df(df_value: Any, row: int) -> Optional[int]:
#     if df_value is None:
#         return None

#     # pandas DataFrame in some Gradio versions
#     try:
#         import pandas as pd  # type: ignore
#         if isinstance(df_value, pd.DataFrame):
#             if row < 0 or row >= len(df_value.index) or df_value.shape[1] < 1:
#                 return None
#             return int(df_value.iloc[row, 0])
#     except Exception:
#         pass

#     # list-of-lists
#     try:
#         if isinstance(df_value, list) and row >= 0 and row < len(df_value) and len(df_value[row]) >= 1:
#             return int(df_value[row][0])
#     except Exception:
#         return None

#     return None


# # =========================================================
# # Gradio callbacks
# # =========================================================
# def scan_dataset(repo_id: str, repo_type: str):
#     if not repo_id:
#         raise gr.Error("请填写 Dataset / Repo 名称(例如 org/dataset)。")

#     files = list_repo_files(repo_id, repo_type=repo_type)
#     media_files = sorted([f for f in files if f.lower().endswith(MEDIA_EXTS)])
#     vtt_files = sorted([f for f in files if f.lower().endswith(VTT_EXTS)])

#     if not media_files:
#         raise gr.Error("未找到媒体文件(mp4/mp3/wav 等)。")
#     if not vtt_files:
#         raise gr.Error("未找到 VTT 字幕文件。")

#     return (
#         gr.update(choices=media_files, value=media_files[0]),
#         gr.update(choices=media_files, value=media_files[0]),
#         gr.update(choices=vtt_files, value=vtt_files[0]),
#         gr.update(choices=vtt_files, value=vtt_files[0]),
#     )


# def load_and_align(repo_id, repo_type, media_a, media_b, vtt_a, vtt_b, th):
#     if not all([repo_id, repo_type, media_a, media_b, vtt_a, vtt_b]):
#         raise gr.Error("请先选择 A/B 的媒体文件与 VTT 文件。")

#     local_media_a = hf_hub_download(repo_id, media_a, repo_type=repo_type)
#     local_media_b = hf_hub_download(repo_id, media_b, repo_type=repo_type)
#     local_vtt_a = hf_hub_download(repo_id, vtt_a, repo_type=repo_type)
#     local_vtt_b = hf_hub_download(repo_id, vtt_b, repo_type=repo_type)

#     try:
#         audio_a = AudioSegment.from_file(local_media_a)
#         audio_b = AudioSegment.from_file(local_media_b)
#     except Exception as e:
#         raise gr.Error(
#             "媒体解码失败。若是 mp4/m4a,通常需要 ffmpeg。\n"
#             f"原始错误: {repr(e)}"
#         )

#     cues_a = parse_vtt_file(local_vtt_a)
#     cues_b = parse_vtt_file(local_vtt_b)
#     if not cues_a or not cues_b:
#         raise gr.Error("VTT 解析为空,请检查字幕文件内容。")

#     # ---- Drift fix: estimate time-scale (linear) between VTT timeline and audio timeline ----
#     # If you observe increasing offset over time, it is usually a *scale* mismatch rather than a constant offset.
#     # We estimate per-track scale by comparing audio duration to the last cue end time.
#     a_vtt_end = max(c.end for c in cues_a) if cues_a else 0.0
#     b_vtt_end = max(c.end for c in cues_b) if cues_b else 0.0
#     a_dur = float(audio_a.duration_seconds)
#     b_dur = float(audio_b.duration_seconds)

#     # Default scale = 1.0 when we cannot estimate reliably.
#     scale_a_suggest = (a_dur / a_vtt_end) if a_vtt_end > 1.0 and a_dur > 1.0 else 1.0
#     scale_b_suggest = (b_dur / b_vtt_end) if b_vtt_end > 1.0 and b_dur > 1.0 else 1.0

#     aligned = align_by_time(cues_a, cues_b, float(th))
#     if not aligned:
#         raise gr.Error("未对齐到任何字幕片段,请尝试增大对齐阈值。")

#     rows = [
#         [
#             x["idx"],
#             f'{x["a_start"]:.2f}-{x["a_end"]:.2f}',
#             f'{x["b_start"]:.2f}-{x["b_end"]:.2f}',
#             x["a_text"],
#             x["b_text"],
#         ]
#         for x in aligned
#     ]

#     # Critical: build idx -> seg map to survive dataframe sorting/reordering
#     idx_map = {int(x["idx"]): x for x in aligned}

#     state = {
#         "aligned": aligned,
#         "idx_map": idx_map,
#         "audio_a": audio_a,
#         "audio_b": audio_b,
#         "scale_a_suggest": scale_a_suggest,
#         "scale_b_suggest": scale_b_suggest,
#         "a_vtt_end": a_vtt_end,
#         "b_vtt_end": b_vtt_end,
#         "a_dur": a_dur,
#         "b_dur": b_dur,
#     }

#     # Clear old playback outputs
#     return rows, state, None, None, {}, gr.update(value=scale_a_suggest), gr.update(value=scale_b_suggest)


# def play_on_select(evt: gr.SelectData, df_value, crop_mode, offset_a, offset_b, scale_a, scale_b, state):
#     if not state or "aligned" not in state:
#         raise gr.Error("请先加载并对齐。")

#     # evt.index: int or (row, col)
#     idx_raw = evt.index
#     row = int(idx_raw[0] if isinstance(idx_raw, (tuple, list)) else idx_raw)

#     offset_a = float(offset_a)
#     offset_b = float(offset_b)
#     scale_a = float(scale_a)
#     scale_b = float(scale_b)

#     # Prefer seg_idx from the clicked row's first column; then resolve via idx_map.
#     seg_idx = _get_seg_idx_from_df(df_value, row)
#     seg = None
#     idx_map = state.get("idx_map", {}) or {}
#     if seg_idx is not None and seg_idx in idx_map:
#         seg = idx_map[seg_idx]
#     else:
#         # Fallback to row->aligned if idx missing (should be rare)
#         aligned = state["aligned"]
#         if row < 0 or row >= len(aligned):
#             raise gr.Error("选中行越界,请重试或重新对齐。")
#         seg = aligned[row]
#         seg_idx = int(seg.get("idx", row + 1))

#     if crop_mode == "global":
#         a_start, a_end = seg["start"] * scale_a + offset_a, seg["end"] * scale_a + offset_a
#         b_start, b_end = seg["start"] * scale_b + offset_b, seg["end"] * scale_b + offset_b
#     else:
#         # per_track playback (recommended)
#         a_start, a_end = seg["a_start"] * scale_a + offset_a, seg["a_end"] * scale_a + offset_a
#         b_start, b_end = seg["b_start"] * scale_b + offset_b, seg["b_end"] * scale_b + offset_b

#     a_np = export_segment_numpy(state["audio_a"], a_start, a_end)
#     b_np = export_segment_numpy(state["audio_b"], b_start, b_end)

#     info = {
#         "segment": seg_idx,
#         "row": row,
#         "crop_mode": crop_mode,
#         "A_time": f"{a_start:.2f}-{a_end:.2f}",
#         "B_time": f"{b_start:.2f}-{b_end:.2f}",
#         "scale_a": scale_a,
#         "scale_b": scale_b,
#         "scale_a_suggest": state.get("scale_a_suggest", 1.0),
#         "scale_b_suggest": state.get("scale_b_suggest", 1.0),
#     }
#     return a_np, b_np, info


# # =========================================================
# # UI
# # =========================================================
# with gr.Blocks(title="双语音频字幕对齐(点击即播放)") as demo:
#     gr.Markdown(
#         "# 双语音频字幕对齐(点击表格即播放)\n"
#         "流程:扫描 Dataset → 选择 A/B 媒体与字幕 → 加载并对齐 → 点击表格任意单元格播放对应片段。\n"
#         "若字幕与音频整体存在固定延迟,可用 Track A/B 偏移进行校正。"
#     )

#     state = gr.State()

#     with gr.Row():
#         repo_id = gr.Textbox(label="Dataset / Repo 名称", placeholder="org/dataset")
#         repo_type = gr.Radio(["dataset", "model"], value="dataset", label="Repo 类型")

#     btn_scan = gr.Button("扫描 Dataset", variant="primary")

#     with gr.Row():
#         media_a = gr.Dropdown(label="Track A 媒体")
#         media_b = gr.Dropdown(label="Track B 媒体")

#     with gr.Row():
#         vtt_a = gr.Dropdown(label="Track A 字幕")
#         vtt_b = gr.Dropdown(label="Track B 字幕")

#     btn_scan.click(
#         scan_dataset,
#         inputs=[repo_id, repo_type],
#         outputs=[media_a, media_b, vtt_a, vtt_b],
#     )

#     th = gr.Slider(0.3, 5.0, value=DEFAULT_MAX_MID_DIFF, step=0.1, label="对齐阈值(秒)")
#     btn_align = gr.Button("加载并对齐", variant="primary")

#     df = gr.Dataframe(
#         headers=["#", "A Time", "B Time", "Track A", "Track B"],
#         interactive=True,  # can be sorted/edited; mapping is stable due to idx_map
#         wrap=True,
#         max_height=520,
#     )

#     with gr.Row():
#         crop_mode = gr.Radio(
#             choices=["per_track", "global"],
#             value="per_track",
#             label="裁剪方式(建议 per_track)",
#         )
#         offset_a = gr.Slider(-20, 20, value=0.0, step=0.05, label="Track A 时间偏移(s)")
#         offset_b = gr.Slider(-20, 20, value=0.0, step=0.05, label="Track B 时间偏移(s)")
#         scale_a = gr.Slider(0.95, 1.05, value=1.0, step=0.0005, label="Track A 时间缩放(scale)")
#         scale_b = gr.Slider(0.95, 1.05, value=1.0, step=0.0005, label="Track B 时间缩放(scale)")

#     with gr.Row():
#         a_out = gr.Audio(label="Track A 片段", type="numpy")
#         b_out = gr.Audio(label="Track B 片段", type="numpy")

#     play_info = gr.JSON(label="当前片段")

#     btn_align.click(
#         load_and_align,
#         inputs=[repo_id, repo_type, media_a, media_b, vtt_a, vtt_b, th],
#         outputs=[df, state, a_out, b_out, play_info, scale_a, scale_b],
#     )

#     df.select(
#         play_on_select,
#         inputs=[df, crop_mode, offset_a, offset_b, scale_a, scale_b, state],
#         outputs=[a_out, b_out, play_info],
#     )

# if __name__ == "__main__":
#     demo.launch()

import re
from dataclasses import dataclass
from typing import Any, Dict, List, Tuple, Optional

import gradio as gr
from huggingface_hub import list_repo_files, hf_hub_download
from pydub import AudioSegment
import numpy as np

# =========================================================
# Config
# =========================================================
MEDIA_EXTS = (".mp4", ".m4a", ".mp3", ".wav", ".flac", ".ogg", ".aac", ".mov", ".avi")
VTT_EXTS = (".vtt",)

DEFAULT_MAX_MID_DIFF = 1.5

# Normalize audio for stable playback in browsers
TARGET_SR = 48000
TARGET_CH = 1          # mono
TARGET_SW = 2          # 16-bit PCM


# =========================================================
# Data structures
# =========================================================
@dataclass
class Cue:
    start: float
    end: float
    text: str


# =========================================================
# VTT parsing
# =========================================================
_TAG_RE = re.compile(r"</?[^>]+?>", re.IGNORECASE)
_VTT_TIME_RE = re.compile(
    r"(?P<start>\d{2}:\d{2}:\d{2}\.\d{3}|\d{1,2}:\d{2}\.\d{3})\s*-->\s*"
    r"(?P<end>\d{2}:\d{2}:\d{2}\.\d{3}|\d{1,2}:\d{2}\.\d{3})"
)


def _strip_tags(text: str) -> str:
    return _TAG_RE.sub("", text).strip()


def _time_to_seconds(t: str) -> float:
    parts = t.split(":")
    if len(parts) == 3:
        return int(parts[0]) * 3600 + int(parts[1]) * 60 + float(parts[2])
    if len(parts) == 2:
        return int(parts[0]) * 60 + float(parts[1])
    raise ValueError(f"Bad VTT timestamp: {t}")


def parse_vtt_file(path: str) -> List[Cue]:
    with open(path, "r", encoding="utf-8") as f:
        content = f.read()

    # Remove BOM / WEBVTT header (if any)
    content = content.replace("\ufeff", "")
    content = re.sub(r"^\s*WEBVTT.*?\n", "", content, flags=re.IGNORECASE)

    blocks = re.split(r"\r?\n\r?\n", content.strip())
    cues: List[Cue] = []

    for block in blocks:
        lines = [l.strip() for l in block.splitlines() if l.strip()]
        if not lines:
            continue

        # Locate the timestamp line (must contain "-->")
        time_idx: Optional[int] = None
        for i, line in enumerate(lines):
            if "-->" in line:
                time_idx = i
                break
        if time_idx is None:
            continue

        m = _VTT_TIME_RE.search(lines[time_idx])
        if not m:
            continue

        start = _time_to_seconds(m.group("start"))
        end = _time_to_seconds(m.group("end"))
        if end <= start:
            continue

        # Only take lines after the timestamp line as subtitle text
        text_lines = lines[time_idx + 1 :]
        if not text_lines:
            continue

        text = _strip_tags("\n".join(text_lines))
        if text:
            cues.append(Cue(start=start, end=end, text=text))

    return sorted(cues, key=lambda x: x.start)


# =========================================================
# Alignment (match by mid time), preserve per-track windows
# =========================================================
def align_by_time(a: List[Cue], b: List[Cue], th: float) -> List[Dict[str, Any]]:
    out: List[Dict[str, Any]] = []
    i, j, idx = 0, 0, 1

    while i < len(a) and j < len(b):
        ma = (a[i].start + a[i].end) / 2
        mb = (b[j].start + b[j].end) / 2

        if abs(ma - mb) <= th:
            out.append(
                {
                    "idx": idx,
                    # Per-track time window (recommended for playback)
                    "a_start": a[i].start,
                    "a_end": a[i].end,
                    "b_start": b[j].start,
                    "b_end": b[j].end,
                    # Optional global time window (for comparison/debug)
                    "start": min(a[i].start, b[j].start),
                    "end": max(a[i].end, b[j].end),
                    "a_text": a[i].text,
                    "b_text": b[j].text,
                }
            )
            idx += 1
            i += 1
            j += 1
        elif ma < mb:
            i += 1
        else:
            j += 1

    return out


# =========================================================
# Audio slicing -> return (sr, np.int16) for gr.Audio(type="numpy")
# =========================================================
def export_segment_numpy(audio: AudioSegment, start: float, end: float) -> Tuple[int, np.ndarray]:
    """
    Robust segment export for gr.Audio(type="numpy").

    Key points:
    - Clamp start/end (after any offsets) to valid range.
    - Use *rounded* ms boundaries to avoid systematic truncation drift.
    - Slice via pydub (ms-accurate) using the original stream timeline.
    - Normalize to mono/48k/int16 for stable browser playback.
    - Return (sr, int16 ndarray) to avoid float32 scaling pitfalls.
    """
    # Clamp and ensure minimum duration
    start = float(start)
    end = float(end)
    if end < start:
        start, end = end, start
    start = max(0.0, start)
    end = max(start + 0.05, end)

    # Round to milliseconds (avoid int() truncation bias)
    start_ms = int(round(start * 1000.0))
    end_ms = int(round(end * 1000.0))

    seg = audio[start_ms:end_ms]

    # Normalize to mono/48k/int16
    seg = seg.set_channels(TARGET_CH).set_frame_rate(TARGET_SR).set_sample_width(TARGET_SW)

    arr = np.asarray(seg.get_array_of_samples())
    if arr.dtype != np.int16:
        arr = arr.astype(np.int16, copy=False)

    return TARGET_SR, np.ascontiguousarray(arr)


# =========================================================
# Helper: robustly read seg_idx from gr.Dataframe value
# =========================================================
def _get_seg_idx_from_df(df_value: Any, row: int) -> Optional[int]:
    if df_value is None:
        return None

    # pandas DataFrame in some Gradio versions
    try:
        import pandas as pd  # type: ignore
        if isinstance(df_value, pd.DataFrame):
            if row < 0 or row >= len(df_value.index) or df_value.shape[1] < 1:
                return None
            return int(df_value.iloc[row, 0])
    except Exception:
        pass

    # list-of-lists
    try:
        if isinstance(df_value, list) and row >= 0 and row < len(df_value) and len(df_value[row]) >= 1:
            return int(df_value[row][0])
    except Exception:
        return None

    return None


# =========================================================
# Gradio callbacks
# =========================================================
def scan_dataset(repo_id: str, repo_type: str):
    if not repo_id:
        raise gr.Error("请填写 Dataset / Repo 名称(例如 org/dataset)。")

    files = list_repo_files(repo_id, repo_type=repo_type)
    media_files = sorted([f for f in files if f.lower().endswith(MEDIA_EXTS)])
    vtt_files = sorted([f for f in files if f.lower().endswith(VTT_EXTS)])

    if not media_files:
        raise gr.Error("未找到媒体文件(mp4/mp3/wav 等)。")
    if not vtt_files:
        raise gr.Error("未找到 VTT 字幕文件。")

    return (
        gr.update(choices=media_files, value=media_files[0]),
        gr.update(choices=media_files, value=media_files[0]),
        gr.update(choices=vtt_files, value=vtt_files[0]),
        gr.update(choices=vtt_files, value=vtt_files[0]),
    )


def load_and_align(repo_id, repo_type, media_a, media_b, vtt_a, vtt_b, th):
    if not all([repo_id, repo_type, media_a, media_b, vtt_a, vtt_b]):
        raise gr.Error("请先选择 A/B 的媒体文件与 VTT 文件。")

    local_media_a = hf_hub_download(repo_id, media_a, repo_type=repo_type)
    local_media_b = hf_hub_download(repo_id, media_b, repo_type=repo_type)
    local_vtt_a = hf_hub_download(repo_id, vtt_a, repo_type=repo_type)
    local_vtt_b = hf_hub_download(repo_id, vtt_b, repo_type=repo_type)

    try:
        audio_a = AudioSegment.from_file(local_media_a)
        audio_b = AudioSegment.from_file(local_media_b)
    except Exception as e:
        raise gr.Error(
            "媒体解码失败。若是 mp4/m4a,通常需要 ffmpeg。\n"
            f"原始错误: {repr(e)}"
        )

    cues_a = parse_vtt_file(local_vtt_a)
    cues_b = parse_vtt_file(local_vtt_b)
    if not cues_a or not cues_b:
        raise gr.Error("VTT 解析为空,请检查字幕文件内容。")

    aligned = align_by_time(cues_a, cues_b, float(th))
    if not aligned:
        raise gr.Error("未对齐到任何字幕片段,请尝试增大对齐阈值。")

    rows = [
        [
            x["idx"],
            f'{x["a_start"]:.2f}-{x["a_end"]:.2f}',
            f'{x["b_start"]:.2f}-{x["b_end"]:.2f}',
            x["a_text"],
            x["b_text"],
        ]
        for x in aligned
    ]

    # Critical: build idx -> seg map to survive dataframe sorting/reordering
    idx_map = {int(x["idx"]): x for x in aligned}

    state = {
        "aligned": aligned,
        "idx_map": idx_map,
        "audio_a": audio_a,
        "audio_b": audio_b,
    }

    # Clear old playback outputs
    return rows, state, None, None, {}


def play_on_select(evt: gr.SelectData, df_value, crop_mode, offset_a, offset_b, state):
    if not state or "aligned" not in state:
        raise gr.Error("请先加载并对齐。")

    # evt.index: int or (row, col)
    idx_raw = evt.index
    row = int(idx_raw[0] if isinstance(idx_raw, (tuple, list)) else idx_raw)

    offset_a = float(offset_a)
    offset_b = float(offset_b)

    # Prefer seg_idx from the clicked row's first column; then resolve via idx_map.
    seg_idx = _get_seg_idx_from_df(df_value, row)
    seg = None
    idx_map = state.get("idx_map", {}) or {}
    if seg_idx is not None and seg_idx in idx_map:
        seg = idx_map[seg_idx]
    else:
        # Fallback to row->aligned if idx missing (should be rare)
        aligned = state["aligned"]
        if row < 0 or row >= len(aligned):
            raise gr.Error("选中行越界,请重试或重新对齐。")
        seg = aligned[row]
        seg_idx = int(seg.get("idx", row + 1))

    if crop_mode == "global":
        a_start, a_end = seg["start"] + offset_a, seg["end"] + offset_a
        b_start, b_end = seg["start"] + offset_b, seg["end"] + offset_b
    else:
        # per_track playback (recommended)
        a_start, a_end = seg["a_start"] + offset_a, seg["a_end"] + offset_a
        b_start, b_end = seg["b_start"] + offset_b, seg["b_end"] + offset_b

    a_np = export_segment_numpy(state["audio_a"], a_start, a_end)
    b_np = export_segment_numpy(state["audio_b"], b_start, b_end)

    info = {
        "segment": seg_idx,
        "row": row,
        "crop_mode": crop_mode,
        "A_time": f"{a_start:.2f}-{a_end:.2f}",
        "B_time": f"{b_start:.2f}-{b_end:.2f}",
    }
    return a_np, b_np, info


# =========================================================
# UI
# =========================================================
with gr.Blocks(title="双语音频字幕对齐(点击即播放)") as demo:
    gr.Markdown(
        "# 双语音频字幕对齐(点击表格即播放)\n"
        "流程:扫描 Dataset → 选择 A/B 媒体与字幕 → 加载并对齐 → 点击表格任意单元格播放对应片段。\n"
        "若字幕与音频整体存在固定延迟,可用 Track A/B 偏移进行校正。"
    )

    state = gr.State()

    with gr.Row():
        repo_id = gr.Textbox(label="Dataset / Repo 名称", placeholder="org/dataset")
        repo_type = gr.Radio(["dataset", "model"], value="dataset", label="Repo 类型")

    btn_scan = gr.Button("扫描 Dataset", variant="primary")

    with gr.Row():
        media_a = gr.Dropdown(label="Track A 媒体")
        media_b = gr.Dropdown(label="Track B 媒体")

    with gr.Row():
        vtt_a = gr.Dropdown(label="Track A 字幕")
        vtt_b = gr.Dropdown(label="Track B 字幕")

    btn_scan.click(
        scan_dataset,
        inputs=[repo_id, repo_type],
        outputs=[media_a, media_b, vtt_a, vtt_b],
    )

    th = gr.Slider(0.3, 5.0, value=DEFAULT_MAX_MID_DIFF, step=0.1, label="对齐阈值(秒)")
    btn_align = gr.Button("加载并对齐", variant="primary")

    df = gr.Dataframe(
        headers=["#", "A Time", "B Time", "Track A", "Track B"],
        interactive=True,  # can be sorted/edited; mapping is stable due to idx_map
        wrap=True,
        max_height=520,
    )

    with gr.Row():
        crop_mode = gr.Radio(
            choices=["per_track", "global"],
            value="per_track",
            label="裁剪方式(建议 per_track)",
        )
        offset_a = gr.Slider(-20, 20, value=0.0, step=0.05, label="Track A 时间偏移(s)")
        offset_b = gr.Slider(-20, 20, value=0.0, step=0.05, label="Track B 时间偏移(s)")

    with gr.Row():
        a_out = gr.Audio(label="Track A 片段", type="numpy")
        b_out = gr.Audio(label="Track B 片段", type="numpy")

    play_info = gr.JSON(label="当前片段")

    btn_align.click(
        load_and_align,
        inputs=[repo_id, repo_type, media_a, media_b, vtt_a, vtt_b, th],
        outputs=[df, state, a_out, b_out, play_info],
    )

    df.select(
        play_on_select,
        inputs=[df, crop_mode, offset_a, offset_b, state],
        outputs=[a_out, b_out, play_info],
    )

if __name__ == "__main__":
    demo.launch()