Skip to content

Commit

Permalink
Add support for VTT files spanning on more than 24 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPoint committed Dec 30, 2020
1 parent 578c9c8 commit 75f3aaf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
33 changes: 18 additions & 15 deletions SubtitlesParser/Classes/Parsers/VttParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,30 +170,33 @@ private bool TryParseTimecodeLine(string line, out int startTc, out int endTc)
/// <returns>The parsed timecode as a TimeSpan instance. If the parsing was unsuccessful, -1 is returned (subtitles should never show)</returns>
private int ParseVttTimecode(string s)
{
string timeString = string.Empty;
var match = Regex.Match(s, "[0-9]+:[0-9]+:[0-9]+[,\\.][0-9]+");
int hours = 0;
int minutes = 0;
int seconds = 0;
int milliseconds = -1;
var match = Regex.Match(s, "([0-9]+):([0-9]+):([0-9]+)[,\\.]([0-9]+)");
if (match.Success)
{
timeString = match.Value;
hours = int.Parse(match.Groups[1].Value);
minutes = int.Parse(match.Groups[2].Value);
seconds = int.Parse(match.Groups[3].Value);
milliseconds = int.Parse(match.Groups[4].Value);
}
else
{
match = Regex.Match(s, "[0-9]+:[0-9]+[,\\.][0-9]+");
if (match.Success)
{
timeString = "00:" + match.Value;
match = Regex.Match(s, "([0-9]+):([0-9]+)[,\\.]([0-9]+)");
if (match.Success) {
minutes = int.Parse(match.Groups[1].Value);
seconds = int.Parse(match.Groups[2].Value);
milliseconds = int.Parse(match.Groups[3].Value);
}
}

if (!string.IsNullOrEmpty(timeString))
if (milliseconds >= 0)
{
timeString = timeString.Replace(',', '.');
TimeSpan result;
if (TimeSpan.TryParse(timeString, out result))
{
var nbOfMs = (int)result.TotalMilliseconds;
return nbOfMs;
}
TimeSpan result = new TimeSpan(0, hours, minutes, seconds, milliseconds);
var nbOfMs = (int)result.TotalMilliseconds;
return nbOfMs;
}

return -1;
Expand Down
16 changes: 16 additions & 0 deletions Test/Content/TestFiles/Greater than 24h.vtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
WEBVTT
00:01.000 --> 00:04.000
Never drink liquid nitrogen.

00:05.000 --> 00:09.000
It will perforate your stomach.
You could die.

23:00:05.000 --> 23:59:59.999
It will perforate your stomach.
You could die.

24:00:00.000 --> 55:00:00.000
It will perforate your stomach.
You could die.

0 comments on commit 75f3aaf

Please sign in to comment.