parseXML static method
Parse XML subtitle format (YouTube format)
Implementation
static List<SubtitleCue> parseXML(String content) {
final cues = <SubtitleCue>[];
try {
// Parse XML subtitle format from YouTube
// Format: <transcript><text start="0.0" dur="5.0">Subtitle text</text>...</transcript>
final textPattern = RegExp(
r'<text start="([\d.]+)" dur="([\d.]+)">(.*?)</text>',
dotAll: true,
);
final matches = textPattern.allMatches(content);
for (final match in matches) {
final startSeconds = double.tryParse(match.group(1) ?? '0') ?? 0.0;
final durationSeconds = double.tryParse(match.group(2) ?? '0') ?? 0.0;
final text =
match.group(3)?.replaceAll(RegExp(r'<[^>]+>'), '').trim() ?? '';
if (text.isNotEmpty) {
final start = Duration(milliseconds: (startSeconds * 1000).round());
final end = Duration(
milliseconds: ((startSeconds + durationSeconds) * 1000).round(),
);
cues.add(SubtitleCue(start: start, end: end, text: text));
}
}
} catch (e) {
// If XML parsing fails, return empty list
}
return cues;
}