getSubtitlesData method
Implementation
Subtitles getSubtitlesData(
String subtitlesContent,
SubtitleType subtitleType,
) {
RegExp regExp;
if (subtitleType == SubtitleType.webvtt) {
regExp = RegExp(
r'((\d{2}):(\d{2}):(\d{2})\.(\d+)) +--> +((\d{2}):(\d{2}):(\d{2})\.(\d{3})).*[\r\n]+\s*((?:(?!\r?\n\r?).)*(\r\n|\r|\n)(?:.*))',
caseSensitive: false,
multiLine: true,
);
} else if (subtitleType == SubtitleType.srt) {
regExp = RegExp(
r'((\d{2}):(\d{2}):(\d{2})\,(\d+)) +--> +((\d{2}):(\d{2}):(\d{2})\,(\d{3})).*[\r\n]+\s*((?:(?!\r?\n\r?).)*(\r\n|\r|\n)(?:.*))',
caseSensitive: false,
multiLine: true,
);
} else {
throw Exception('Incorrect subtitle type');
}
final matches = regExp.allMatches(subtitlesContent).toList();
final subtitleList = <Subtitle>[];
for (final regExpMatch in matches) {
final startTimeHours = int.parse(regExpMatch.group(2)!);
final startTimeMinutes = int.parse(regExpMatch.group(3)!);
final startTimeSeconds = int.parse(regExpMatch.group(4)!);
final startTimeMilliseconds = int.parse(regExpMatch.group(5)!);
final endTimeHours = int.parse(regExpMatch.group(7)!);
final endTimeMinutes = int.parse(regExpMatch.group(8)!);
final endTimeSeconds = int.parse(regExpMatch.group(9)!);
final endTimeMilliseconds = int.parse(regExpMatch.group(10)!);
final text = removeAllHtmlTags(regExpMatch.group(11)!);
final startTime = Duration(
hours: startTimeHours,
minutes: startTimeMinutes,
seconds: startTimeSeconds,
milliseconds: startTimeMilliseconds,
);
final endTime = Duration(
hours: endTimeHours,
minutes: endTimeMinutes,
seconds: endTimeSeconds,
milliseconds: endTimeMilliseconds,
);
subtitleList.add(
Subtitle(startTime: startTime, endTime: endTime, text: text.trim()),
);
}
return Subtitles(subtitles: subtitleList);
}