1
0
Fork 0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-03-09 12:50:23 -05:00
yt-dlp/yt_dlp/utils/subtitles.py
2024-02-27 09:27:13 +05:30

36 lines
930 B
Python

import re
from dataclasses import dataclass
from typing import TypeAlias
Seconds: TypeAlias = float
@dataclass
class Metadata:
name: str
value: str
@dataclass
class Subtitle:
text: str
start: Seconds
end: Seconds = None
def parse_lrc(text):
for line in text.split('\n'):
times = []
while mobj := re.fullmatch(r'\[(?P<time>((\d+:)?\d+:)?\d+(.\d+)?)\](?P<content>.*)', line):
times.append(sum(
float(t) * 60**i for i, t in enumerate(reversed(mobj.group('time').split(':')))))
line = mobj.group('content')
for t in times:
yield Subtitle(start=t, text=line.strip())
if not times:
if mobj := re.fullmatch(r'\[(?P<name>[^\]:]+):(?P<value>[^\]]+)\]', line):
yield Metadata(mobj.group('name'), mobj.group('value').strip())
elif line.strip():
yield ValueError(line)