Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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.

Here is the structure:
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

**************************
* A number
   * Followed by start and end time
       * followed by the text which can be 1 or multiple lines

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 hardcodes subtitles to a video file so i need to know how long the video should be based on the length of the subtitle file.

What I have tried:

i havent had any luck with things ive tried - im kinda lost on this one
Posted
Updated 13-Dec-19 9:31am
v2

First, break it into subtitle sections - these are separated by a blank line.
Then, each section starts with a pair or time codes, separated by "-->".
So, read the whole file by using File.ReadAllLines, the scan it for empty lines using the String.IsNullOrWhitespace method.

Then split the second line of the section using String.Split to leave the start and end times as strings.
Process each time using TimeSpan.TryParseExact Method (System) | Microsoft Docs[^] and the rest is trivial.
 
Share this answer
 
Comments
jimmy534534534534 13-Dec-19 11:09am    
Thanks so much for the reply.
Can you elaborate on how exactly to do what you suggested?
OriginalGriff 13-Dec-19 11:25am    
what part of the above instructions is giving you difficulty?
Well I guess I got it - it's probably not the best code, but it works:

Here's what's going on in the code:
I have a Listbox with .srt files
The code takes the .srt file and puts it in a textbox
Then it parses it starting with the last line and goes back up to 20 lines (to give room for extra line breaks at end of file etc.
Then it looks for the first line that only has an integer (meaning the last line)
then it looks for the line after that which is the timecode
then it takes the part on the right which is the end code
And that is the "length" of the .srt file

Dim appPath As String = Application.StartupPath() ' app path
Dim theFile1 As String

theFile1 = appPath & "\" & ListBox1.SelectedItem.ToString 'this is where i have the .srt files

Dim FILE_NAME As String = theFile1

Dim TextLine As String

If System.IO.File.Exists(FILE_NAME) = True Then

Dim objReader As New System.IO.StreamReader(FILE_NAME)

Do While objReader.Peek() <> -1

TextLine = TextLine & objReader.ReadLine() & vbNewLine

Loop

TextBox7.Text = TextLine ' load .srt into textbox

Else

MessageBox.Show("File Does Not Exist")

End If
Dim SrtTimeCode As String
SrtTimeCode = ""

If TextBox7.Lines.Any = True Then ' only execute if textbox has lines

Dim lastLine As String

For i = 1 To 20 'Check from the end of text file back 20 lines for final subtitle chunk
lastLine = TextBox7.Lines(TextBox7.Lines.Length - i)

If Integer.TryParse(lastLine, vbNull) Then ' if the last line is found

SrtTimeCode = TextBox7.Lines(TextBox7.Lines.Length - i + 1) 'the last timecode has been found - now it needs to be split

GoTo TheEnd
End If


Next i
End If


theEnd:
Dim ChoppedSRTTimeCodeFinal As String
Dim test As String = SrtTimeCode
Dim ChoppedSRTTimeCode As String = test.Substring(test.IndexOf(">"c) + 1)


'ChoppedSRTTimeCodeFinal = ChoppedSRTTimeCode.Substring(test.IndexOf(","c) + 1)
ChoppedSRTTimeCodeFinal = ChoppedSRTTimeCode.Substring(0, ChoppedSRTTimeCode.IndexOf(","))

MsgBox(ChoppedSRTTimeCodeFinal) ' this is the final timecode parsed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900