-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSrtParser.bas
More file actions
67 lines (62 loc) · 1.78 KB
/
SrtParser.bas
File metadata and controls
67 lines (62 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=9.8
@EndOfDesignText@
Sub Class_Globals
Type SpeechLine (number As Int,startTime As String,endTime As String, text As String)
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
End Sub
Public Sub Parse(content As String) As List
content = Regex.Replace2("\r\n",32,content,CRLF)
Dim lines As List
lines.Initialize
Dim strLines As List = Regex.Split2(CRLF,32,content)
Dim index As Int = 0
Dim sb As StringBuilder
sb.Initialize
For Each line As String In strLines
If Regex.IsMatch("\d+",line) Then 'new speech line
Dim lineNumber As Int = line
If lineNumber - index = 1 Then
index = lineNumber
If lines.Size > 0 Then
Dim lastSpeech As SpeechLine
lastSpeech = lines.Get(lines.Size - 1)
lastSpeech.text = sb.ToString.Trim
End If
Dim speech As SpeechLine
speech.Initialize
speech.number = lineNumber
sb.Initialize
lines.Add(speech)
End If
else if Regex.IsMatch("\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}",line) Then
Dim speech As SpeechLine = lines.Get(lines.Size - 1)
Dim timeMatcher As Matcher = Regex.Matcher("\d{2}:\d{2}:\d{2},\d{3}",line)
Dim isStart As Boolean = True
Do While timeMatcher.Find
If isStart Then
speech.startTime = timeMatcher.Match
isStart = False
Else
speech.endTime = timeMatcher.Match
End If
Loop
Else
sb.Append(line)
sb.Append(CRLF)
End If
Next
Dim textLeft As String = sb.ToString.trim
If textLeft <> "" Then
If lines.Size > 0 Then
Dim speech As SpeechLine = lines.Get(lines.Size - 1)
speech.text = textLeft
End If
End If
Return lines
End Sub