我试图在.srt中加载和解析一个VB.net字幕文件。这是一个非常简单的文本文件,但我遇到了困难。
结构如下:
Hide Copy Code
1
00:00:01,600 --> 00:00:04,200
English (US)
2
00:00:05,900 --> 00:00:07,999
This is a subtitle in American English
Sometimes subtitles have 2 lines
3
00:00:10,000 --> 00:00:14,000
Adding subtitles is very easy to do我真正想要做的是找出字幕文件的时间长度--意思是为字幕文件找到最后的结束时间。我正在创建一个程序,硬编码字幕到一个视频文件,所以我需要知道多长时间的视频应该根据字幕文件的长度。
我要找的结果是:
在读取了一个.srt文件后,才知道.srt文件的“长度”--意思是最后一次代码。在上面的示例中,它将是: 00:00:14,000,这是最后一次显示字幕。
发布于 2019-12-15 20:09:30
而且,这可以通过正则表达式来实现。
Imports System.IO
Imports System.Text.RegularExpressions
'...
Private Sub TheCaller()
Dim srtFile As String = "English.srt"
Dim endTime = "Not Found!"
If File.Exists(srtFile) Then
Dim patt As String = ">.(\d\d:\d\d:\d\ds?,s?\d{3})"
'Get the last match, --> 00:00:14,000 in your example:
Dim lastMatch = File.ReadLines(srtFile).
LastOrDefault(Function(x) Regex.IsMatch(x, patt))
If lastMatch IsNot Nothing Then
endTime = Regex.Match(lastMatch, patt).Groups(1).Value
End If
End If
Console.WriteLine(endTime)
End Sub输出为regex101
00:00:14,000如果您想要去掉毫秒部分,请使用以下模式:
Dim patt As String = ">.(\d\d:\d\d:\d\d)"你会得到regex101
00:00:14发布于 2019-12-15 20:40:37
您可以使用LINQ和File.Readlines轻松地完成此任务。
Dim SrtTimeCode As String = ""
Dim lastTimeLine As String = File.ReadLines(FILE_NAME) _
.LastOrDefault(Function(s) s.Contains(" --> "))
If lastTimeLine IsNot Nothing Then
SrtTimeCode = lastTimeLine.Split(New String() {" --> "}, StringSplitOptions.None)(1)
End If请注意,File.ReadLines在枚举行时只保留当前行在内存中。它不存储整个文件。这与大文件的比例更好。
发布于 2019-12-14 02:33:33
评论和解释是一致的。
Private Sub OpCode()
'Using Path.Combine you don't have to worry about if the backslash is there or not
Dim theFile1 = Path.Combine(Application.StartupPath(), ListBox1.SelectedItem.ToString)
'A streamreader needs to be closed and disposed,File.ReadAllLines opens the file, reads it, and closes it.
'It returns an array of lines
Dim lines = File.ReadAllLines(theFile1)
Dim LastLineIndex = lines.Length - 1
Dim lastLine As String = lines(LastLineIndex)
'You tried to parse the entire line. You only want the first character
Do Until Integer.TryParse(lastLine.Substring(0, 1), Nothing)
LastLineIndex -= 1
lastLine = lines(LastLineIndex)
Loop
'The lower case c tells the compiler that the preceding string is really a Char.
Dim splitLine = lastLine.Split(">"c)
'Starting at index 1 because there is a space between > and 0
Dim SrtEndTimeCode As String = splitLine(1).Substring(1, 12)
MessageBox.Show(SrtEndTimeCode)
End Subhttps://stackoverflow.com/questions/59326128
复制相似问题