如何解析 .srt 字幕文件 [英] How do I parse a .srt subtitle file

查看:77
本文介绍了如何解析 .srt 字幕文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to load and parse a .srt subtitle file in VB.net. It is a very simple text file, but I'm having difficulty.

结构如下:

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行或多行的文本
  • 我真正想做的是找到字幕文件的时间长度 - 意思是找到字幕文件的最后结束时间.我正在创建一个将字幕硬编码到视频文件的程序,所以我需要根据字幕文件的长度知道视频应该多长时间.

    What I'm really trying to do is find the length in time of the subtitle file - meaning finding the last end time for the subtitle file. I'm creating a program that hard codes subtitles to a video file so I need to know how long the video should be based on the length of the subtitle file.

    我正在寻找的结果是:

    在读取 .srt 文件以了解 .srt 文件的时间长度"后 - 意思是最后的时间代码.在上面的示例中,它将是:00:00:14,000,这是最后一次显示字幕.

    After reading a .srt file to know the "length" in time of the .srt file - meaning the last time code. In the example above it would be: 00:00:14,000 that's the last time the subtitle is displayed.

    推荐答案

    您可以使用 LINQ 和 File.Readlines

    You can do it easily with LINQ and 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 仅保留内存中的当前行.它不存储整个文件.这在处理大文件时效果更好.

    Note that File.ReadLines keeps only the current line in memory when enumerating the lines. It does not store the whole file. This scales better with big files.

    这篇关于如何解析 .srt 字幕文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆