Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion skills/watch/scripts/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
TIME = r'(?:[0-9]{2,}:)?[0-5][0-9]:[0-5][0-9][.,][0-9]{3}'
TIMING_RE = re.compile(rf'^\s*({TIME})\s+-->\s+({TIME})(?:\s+.*)?$')
TAG_RE = re.compile(r'<[^>]*>')
VOICE_RE = re.compile(r'<v(?:\.[^.\s<>&]+)*\s+([^<>]+)>')


class Segments(list):
Expand Down Expand Up @@ -76,7 +77,8 @@ def parse_vtt(path: str) -> list[dict]:
i += 1
text = ' '.join(html.unescape(TAG_RE.sub('', part)).strip() for part in payload).strip()
if text and end >= start:
segments.append({'start': start, 'end': end, 'text': text, '_display': display})
voices = tuple(html.unescape(label).strip() for label in VOICE_RE.findall('\n'.join(payload)))
segments.append({'start': start, 'end': end, 'text': text, '_display': (display, voices)})
return [{k: s[k] for k in ('start', 'end', 'text')} for s in _dedupe(segments)]


Expand Down
20 changes: 20 additions & 0 deletions tests/test_transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,23 @@ def test_overlapping_separate_display_regions_are_not_merged(tmp_path):
out = parse(tmp_path, 'WEBVTT\n\n00:00.000 --> 00:02.000 line:10%\nHello\n\n00:01.000 --> 00:03.000 line:90%\nHello\n')
assert len(out) == 2
assert all(set(s) == {'start', 'end', 'text'} for s in out)



def test_overlapping_cues_from_different_voices_are_not_merged(tmp_path):
out = parse(tmp_path, (
'WEBVTT\n\n00:00.000 --> 00:02.000\n<v Esme>Yes\n\n'
'00:01.000 --> 00:03.000\n<v.loud Mary>Yes\n'
))
assert out == [
{'start': 0, 'end': 2, 'text': 'Yes'},
{'start': 1, 'end': 3, 'text': 'Yes'},
]


def test_rolling_cues_from_the_same_voice_still_merge(tmp_path):
out = parse(tmp_path, (
'WEBVTT\n\n00:00.000 --> 00:02.000\n<v Mary>Yes\n\n'
'00:01.000 --> 00:03.000\n<v.loud Mary>Yes please</v>\n'
))
assert out == [{'start': 0, 'end': 3, 'text': 'Yes please'}]