首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解析.srt字幕文件

如何解析.srt字幕文件
EN

Stack Overflow用户
提问于 2019-12-13 16:09:13
回答 4查看 1.1K关注 0票数 1

我试图在.srt中加载和解析一个VB.net字幕文件。这是一个非常简单的文本文件,但我遇到了困难。

结构如下:

代码语言:javascript
复制
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
  • 一个数字
  • 接着是开始和结束时间
  • 后面跟着可以是1行或多行的文本

我真正想要做的是找出字幕文件的时间长度--意思是为字幕文件找到最后的结束时间。我正在创建一个程序,硬编码字幕到一个视频文件,所以我需要知道多长时间的视频应该根据字幕文件的长度。

我要找的结果是:

在读取了一个.srt文件后,才知道.srt文件的“长度”--意思是最后一次代码。在上面的示例中,它将是: 00:00:14,000,这是最后一次显示字幕。

EN

回答 4

Stack Overflow用户

发布于 2019-12-15 20:09:30

而且,这可以通过正则表达式来实现。

代码语言:javascript
复制
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

代码语言:javascript
复制
00:00:14,000

如果您想要去掉毫秒部分,请使用以下模式:

代码语言:javascript
复制
Dim patt As String = ">.(\d\d:\d\d:\d\d)"

你会得到regex101

代码语言:javascript
复制
00:00:14
票数 2
EN

Stack Overflow用户

发布于 2019-12-15 20:40:37

您可以使用LINQ和File.Readlines轻松地完成此任务。

代码语言:javascript
复制
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在枚举行时只保留当前行在内存中。它不存储整个文件。这与大文件的比例更好。

票数 2
EN

Stack Overflow用户

发布于 2019-12-14 02:33:33

评论和解释是一致的。

代码语言:javascript
复制
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 Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59326128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档