VS 2010 Subtitle SRT Editor-VBForums
Results 1 to 3 of 3

Thread: Subtitle SRT Editor

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    2

    Post Subtitle SRT Editor

    SRT Editor

    I'm doing a simple srt editor for checking lines and time, already has the reader and save functionalities done. Any insides would be usufull

    Main.vb
    Code:
    Imports System.IO
    Public Class Main
        Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim TheFileName As String = ""
            Try
                If My.Application.CommandLineArgs.Count > 0 Then
                    TheFileName = My.Application.CommandLineArgs(0)
                End If
            Catch ex As Exception
                IO.File.AppendAllText("C:\Log.txt", String.Format("{0}{1}", Environment.NewLine, ex.ToString()))
            End Try
            If TheFileName.Length > 0 Then
                Dim ext As String = Path.GetExtension(TheFileName)
                Brow1_inp.Text = TheFileName
                If ext = ".srt" Then
                    Dim srtinfo As Array = reader(Brow1_inp.Text, P1_DG, 1) 'le entregamos la ruta del archivo y el panel donde dejar los datos'
                    P1_Ldia.Text = srtinfo(0) & " lineas de dialogo"
                    P1_Lcod.Text = srtinfo(1) & " lineas de codigo"
                Else
                    MsgBox("El archivo no corresponde a un SubRip", MsgBoxStyle.Information, "Error")
                End If
            Else
                P1_Leer.Enabled = False
            End If
            'Combo Boxes population'
            Dim comboSource As New Dictionary(Of String, String)()
            comboSource.Add("0", "")
            comboSource.Add("1", "Todo")
            comboSource.Add("2", "Lineas")
            comboSource.Add("3", "Tiempo")
            comboSource.Add("4", "Dialogo")
            P1_CCom.DataSource = New BindingSource(comboSource, Nothing)
            P1_CCom.DisplayMember = "Value"
            P1_CCom.ValueMember = "Key"
            P2_CCom.DataSource = New BindingSource(comboSource, Nothing)
            P2_CCom.DisplayMember = "Value"
            P2_CCom.ValueMember = "Key"
            'habilitacion de botones'
            P1_CLab.Visible = False
            P1_CCom.Visible = False
            P1_Revnum.Enabled = False
            P1_Revtie.Enabled = False
            P1_Clear.Enabled = False
            P1_Save.Enabled = False
            P2_CLab.Visible = False
            P2_CCom.Visible = False
            P2_Leer.Enabled = False
            P2_Revnum.Enabled = False
            P2_revtie.Enabled = False
            P2_Clear.Enabled = False
            P2_Save.Enabled = False
        End Sub
        Private Sub Brow1_but_Click_1(sender As System.Object, e As System.EventArgs) Handles Brow1_but.Click
            Dim FolderBrowserDialog As New OpenFileDialog
            With FolderBrowserDialog
                .DefaultExt = "srt"
                .Filter = "Subtitulo srt (*.srt)|*.srt"
                If .ShowDialog = DialogResult.OK Then
                    Brow1_inp.Text = .FileName
                    P1_Leer.Enabled = True
                Else
                    P1_Leer.Enabled = False
                End If
            End With
        End Sub
        Private Sub Brow2_but_Click_2(sender As System.Object, e As System.EventArgs) Handles Brow2_but.Click
            Dim FolderBrowserDialog As New OpenFileDialog
            With FolderBrowserDialog
                .DefaultExt = "srt"
                .Filter = "Subtitulo srt (*.srt)|*.srt"
                If .ShowDialog = DialogResult.OK Then
                    Brow2_inp.Text = .FileName
                    P2_Leer.Enabled = True
                Else
                    P2_Leer.Enabled = False
                End If
            End With
        End Sub
        Private Sub Sig_Click(sender As System.Object, e As System.EventArgs) Handles Sig.Click
            Dim i = Me.Size.Width
            If i < 1000 Then
                Me.Size = New Size(1400, 650)
                P2.Visible = True
                Sig.Text = "<-"
                P1_CLab.Visible = True
                P1_CCom.Visible = True
            Else
                Me.Size = New Size(700, 650)
                P2.Visible = False
                Sig.Text = "->"
                P1_CLab.Visible = False
                P1_CCom.Visible = False
            End If
    
        End Sub
        Private Sub P1_CCom_Change(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P1_CCom.SelectionChangeCommitted
            Dim key As String = DirectCast(P1_CCom.SelectedItem, KeyValuePair(Of String, String)).Key
            Dim value As String = DirectCast(P1_CCom.SelectedItem, KeyValuePair(Of String, String)).Value
            Dim sel As Integer
            Dim num As Integer = P1_DG.Rows.Count
            Dim i As Integer = 0
            Dim filas As Integer
            sel = CInt(key)
            Select Case sel
                Case 1
                    While i < num
                        filas = P2_DG.Rows.Count
                        If filas = i Then
                            P2_DG.Rows.Add()
                        End If
                        P2_DG.Rows(i).Cells(0).Value = P1_DG.Rows(i).Cells(0).Value
                        P2_DG.Rows(i).Cells(1).Value = P1_DG.Rows(i).Cells(1).Value
                        P2_DG.Rows(i).Cells(2).Value = P1_DG.Rows(i).Cells(2).Value
                        P2_DG.Rows(i).Cells(3).Value = P1_DG.Rows(i).Cells(3).Value
                        i = i + 1
                    End While
                Case 2
                    While i < num
                        filas = P2_DG.Rows.Count
                        If filas = i Then
                            P2_DG.Rows.Add()
                        End If
                        P2_DG.Rows(i).Cells(0).Value = P1_DG.Rows(i).Cells(0).Value
                        i = i + 1
                    End While
                Case 3
                    While i < num
                        filas = P2_DG.Rows.Count
                        If filas = i Then
                            P2_DG.Rows.Add()
                        End If
                        P2_DG.Rows(i).Cells(1).Value = P1_DG.Rows(i).Cells(1).Value
                        P2_DG.Rows(i).Cells(2).Value = P1_DG.Rows(i).Cells(2).Value
                        i = i + 1
                    End While
                Case 4
                    While i < num
                        filas = P2_DG.Rows.Count
                        If filas = i Then
                            P2_DG.Rows.Add()
                        End If
                        P2_DG.Rows(i).Cells(3).Value = P1_DG.Rows(i).Cells(3).Value
                        i = i + 1
                    End While
            End Select
            P2_Revnum.Enabled = True
            P2_revtie.Enabled = True
            P2_Clear.Enabled = True
            P2_Save.Enabled = True
            P2_CLab.Visible = True
            P2_CCom.Visible = True
        End Sub
        Private Sub P2_CCom_Change(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles P2_CCom.SelectionChangeCommitted
            Dim key As String = DirectCast(P2_CCom.SelectedItem, KeyValuePair(Of String, String)).Key
            Dim value As String = DirectCast(P2_CCom.SelectedItem, KeyValuePair(Of String, String)).Value
            Dim sel As Integer
            Dim num As Integer = P2_DG.Rows.Count
            Dim i As Integer = 0
            Dim filas As Integer
            sel = CInt(key)
            Select Case sel
                Case 1
                    While i < num
                        filas = P1_DG.Rows.Count
                        If filas = i Then
                            P1_DG.Rows.Add()
                        End If
                        P1_DG.Rows(i).Cells(0).Value = P2_DG.Rows(i).Cells(0).Value
                        P1_DG.Rows(i).Cells(1).Value = P2_DG.Rows(i).Cells(1).Value
                        P1_DG.Rows(i).Cells(2).Value = P2_DG.Rows(i).Cells(2).Value
                        P1_DG.Rows(i).Cells(3).Value = P2_DG.Rows(i).Cells(3).Value
                        i = i + 1
                    End While
                Case 2
                    While i < num
                        filas = P1_DG.Rows.Count
                        If filas = i Then
                            P1_DG.Rows.Add()
                        End If
                        P1_DG.Rows(i).Cells(0).Value = P2_DG.Rows(i).Cells(0).Value
                        i = i + 1
                    End While
                Case 3
                    While i < num
                        filas = P1_DG.Rows.Count
                        If filas = i Then
                            P1_DG.Rows.Add()
                        End If
                        P1_DG.Rows(i).Cells(1).Value = P2_DG.Rows(i).Cells(1).Value
                        P1_DG.Rows(i).Cells(2).Value = P2_DG.Rows(i).Cells(2).Value
                        i = i + 1
                    End While
                Case 4
                    While i < num
                        filas = P1_DG.Rows.Count
                        If filas = i Then
                            P1_DG.Rows.Add()
                        End If
                        P1_DG.Rows(i).Cells(3).Value = P2_DG.Rows(i).Cells(3).Value
                        i = i + 1
                    End While
            End Select
            P1_Revnum.Enabled = True
            P1_Revtie.Enabled = True
            P1_Clear.Enabled = True
            P1_Save.Enabled = True
        End Sub
    
        Private Sub P1_Clear_Click(sender As System.Object, e As System.EventArgs) Handles P1_Clear.Click
            P1_DG.Rows.Clear()
            P1_Revnum.Enabled = False
            P1_Revtie.Enabled = False
            P1_Clear.Enabled = False
            P1_Save.Enabled = False
        End Sub
        Private Sub P2_Clear_Click(sender As System.Object, e As System.EventArgs) Handles P2_Clear.Click
            P2_DG.Rows.Clear()
            P2_Revnum.Enabled = False
            P2_revtie.Enabled = False
            P2_Clear.Enabled = False
            P2_Save.Enabled = False
        End Sub
        Private Sub P1_Leer_Click(sender As System.Object, e As System.EventArgs) Handles P1_Leer.Click
            If File.Exists(Brow1_inp.Text) Then
                Dim srtinfo As Array = reader(Brow1_inp.Text, P1_DG, 1) 'le entregamos la ruta del archivo y el panel donde dejar los datos'
                P1_Ldia.Text = srtinfo(0) & " lineas de dialogo"
                P1_Lcod.Text = srtinfo(1) & " lineas de codigo"
            Else
                MsgBox("Seleccionar un archivo", MsgBoxStyle.Information, "Error")
            End If
        End Sub
        Private Sub P2_Leer_Click(sender As System.Object, e As System.EventArgs) Handles P2_Leer.Click
            If File.Exists(Brow2_inp.Text) Then
                Dim srtinfo As Array = reader(Brow2_inp.Text, P2_DG, 2) 'le entregamos la ruta del archivo y el panel donde dejar los datos'
                P2_Ldia.Text = srtinfo(0) & " lineas de dialogo"
                P2_Lcod.Text = srtinfo(1) & " lineas de codigo"
            Else
                MsgBox("Seleccionar un archivo", MsgBoxStyle.Information, "Error")
            End If
        End Sub
        Private Sub P1_Revnum_Click(sender As System.Object, e As System.EventArgs) Handles P1_Revnum.Click
            Rev_Numeros(P1_DG)
        End Sub
        Private Sub P2_Revnum_Click(sender As System.Object, e As System.EventArgs) Handles P2_Revnum.Click
            Rev_Numeros(P2_DG)
        End Sub
        Private Sub P1_Save_Click(sender As System.Object, e As System.EventArgs) Handles P1_Save.Click
            Guardar_srt(P1_DG, 1)
        End Sub
        Private Sub P2_Save_Click(sender As System.Object, e As System.EventArgs) Handles P2_Save.Click
            Guardar_srt(P2_DG, 2)
        End Sub
        Private Sub P1_Revtie_Click(sender As System.Object, e As System.EventArgs) Handles P1_Revtie.Click
            Rev_Tiempo(P1_DG)
        End Sub
        Private Sub P2_Revtie_Click(sender As System.Object, e As System.EventArgs) Handles P2_revtie.Click
            Rev_Tiempo(P2_DG)
        End Sub
        Private Sub Guardar_srt(Datos As DataGridView, auxiliar As Integer)
            Dim saveFileDialog1 As New SaveFileDialog()
            saveFileDialog1.DefaultExt = "srt"
            saveFileDialog1.Filter = "archivos srt (*.srt)|*.srt"
            saveFileDialog1.Title = "Grabar subtitulo"
            saveFileDialog1.ShowDialog()
    
            If saveFileDialog1.FileName <> "" Then
                Dim txtFilePathName = saveFileDialog1.FileName
                Dim fsOut As New FileStream(saveFileDialog1.FileName, FileMode.Create)
                Dim swFile As New StreamWriter(fsOut)
                Dim j As Integer = Datos.Rows.Count
                Dim i As Integer = 1
                Dim text_var As String
                While i < j
                    swFile.WriteLine(Datos.Rows(i - 1).Cells(0).Value)
                    swFile.WriteLine(Datos.Rows(i - 1).Cells(1).Value & " --> " & Datos.Rows(i - 1).Cells(2).Value)
                    text_var = Datos.Rows(i - 1).Cells(3).Value
                    Dim words As String() = text_var.Split(New String() {"<br>"}, StringSplitOptions.None)
                    Dim k = 0
                    Dim aux As Integer = words.Length
                    While k < aux
                        If (words(k) <> "<br>") Then
                            swFile.WriteLine(words(k))
                        End If
                        k = k + 1
                    End While
                    swFile.WriteLine("")
                    i = i + 1
                End While
                swFile.Close()
                If auxiliar = 1 Then
                    Brow1_inp.Text = txtFilePathName
                    P1_Leer.Enabled = True
                End If
                If auxiliar = 2 Then
                    Brow2_inp.Text = txtFilePathName
                    P2_Leer.Enabled = True
                End If
            End If
        End Sub
        Private Sub Rev_Numeros(Datos As DataGridView)
            Dim num As Integer = Datos.Rows.Count - 1
            Dim valor As Integer
            Dim i As Integer = 0
            Dim j As Integer = 0
            Dim Errores(num, 1) As Integer
            Dim mensaje As String
            While i < num
                valor = Datos.Rows(i).Cells(0).Value
                If valor <> i + 1 Then
                    Errores(i, 0) = i + 1
                    Errores(i, 1) = valor
                    j = j + 1
                Else
                    Errores(i, 0) = 0
                    Errores(i, 1) = 0
                End If
                i = i + 1
            End While
            If j > 0 Then
                i = 0
                j = j - 1
                Dim num_err(j, 1) As Integer
                j = 0
                While i < num
                    If Errores(i, 0) <> 0 Then
                        num_err(j, 0) = Errores(i, 0)
                        num_err(j, 1) = Errores(i, 1)
                        j = j + 1
                    End If
                    i = i + 1
                End While
                Errores = Nothing
                mensaje = "Se encontraron " & num_err.Length / 2 & " errores en los numeros de linea " & vbCrLf & " ¿Desea Corregir?"
                If MessageBox.Show(mensaje, "Revision de Lineas", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    i = 0
                    j = 1
                    While i < num
                        Datos.Rows(i).Cells(0).Value = j
                        i = i + 1
                        j = j + 1
                    End While
                End If
            Else
                mensaje = "No se encontraron errores en los numeros de linea"
                MsgBox(mensaje, MsgBoxStyle.Information, "Resultado")
            End If
        End Sub
        Private Sub Rev_Tiempo(Datos As DataGridView)
            Dim string_ini As String
            Dim string_fin As String
            Dim tiempo_ini As DateTime
            Dim tiempo_fin As DateTime
            Dim duration As TimeSpan
            Dim num As Integer = Datos.Rows.Count - 1
            Dim i As Integer = 0
            Dim j As Integer = -1
            While i < num
                string_ini = Datos.Rows(i).Cells(1).Value
                string_fin = Datos.Rows(i).Cells(2).Value
                string_ini = string_ini.Replace(",", ".")
                string_fin = string_fin.Replace(",", ".")
                tiempo_ini = DateTime.Parse(string_ini)
                tiempo_fin = DateTime.Parse(string_fin)
                duration = tiempo_fin - tiempo_ini
                If (duration.TotalMilliseconds < 200) Then
                    j = i
                    i = num
                End If
                i = i + 1
            End While
            If j >= 0 Then
                Datos.Rows(j).Selected = True
            Else
                MsgBox("Sin errores de tiempo", MsgBoxStyle.Information, "Resultado")
            End If
    
        End Sub
        Private Function reader(input_path As String, grid_box As DataGridView, auxiliar As Integer) 'Se le pide: donde esta el archivo y en donde colocar lo que se realiza'
            Dim splitLine() As String 'Auxiliar lector'
            Dim aux As Integer 'Auxiliar'
            Dim progress As Integer 'porcentaje'
            Dim t_inicio As String 'Tiempo Inicial'
            Dim t_final As String 'Tiempo Final'
            Dim words As String = "" 'Dialogo'
            Dim i As Integer 'contador de lineas de dialogo'
            Dim j As Integer 'contador de lineas de archivo'
            Dim k As Integer 'contador de lineas de archivo por linea de dialogo'
            Dim real_line As Boolean
            Dim linenum As Boolean
            Dim tmpLines() As String = File.ReadAllLines(input_path)
            i = 0
            j = 0
            If tmpLines.Length > 0 Then
                If auxiliar = 1 Then
                    P1_Revnum.Enabled = True
                    P1_Revtie.Enabled = True
                    P1_Clear.Enabled = True
                    P1_Save.Enabled = True
                    P1_PBar.Minimum = 0
                    P1_PBar.Maximum = tmpLines.Count - 1
                End If
                If auxiliar = 2 Then
                    P2_Revnum.Enabled = True
                    P2_revtie.Enabled = True
                    P2_Clear.Enabled = True
                    P2_Save.Enabled = True
                    P2_PBar.Minimum = 0
                    P2_PBar.Maximum = tmpLines.Count - 1
                End If
                Using objReader As New StreamReader(input_path)
                    k = 1
                    Do While objReader.Peek() <> -1
                        If auxiliar = 1 Then
                            P1_PBar.Value = j
                            progress = Math.Truncate((j / P1_PBar.Maximum) * 100)
                            P1_PP.Text = progress & "%"
                            P1_PP.Refresh()
                        End If
                        If auxiliar = 2 Then
                            P2_PBar.Value = j
                            progress = Math.Truncate((j / P2_PBar.Maximum) * 100)
                            P2_PP.Text = progress & "%"
                            P2_PP.Refresh()
                        End If
                        linenum = False 'auxiliar'
                        real_line = False 'auxiliar de linea en blanco'
                        Dim line As String = objReader.ReadLine()
    
                        'linea en blanco'
                        Try
                            If line.Length > 0 Then
                                real_line = True
                            End If
                        Catch ex As Exception
                        End Try
    
                        If (real_line) Then
    
                            'linea de dialogo'
                            Try
                                'caga cuando el subtitulo tiene solo numeros escritos en numero'
                                aux = CInt(line)
                                If (aux > 0) Then
                                    i = i + 1
                                    k = 1
                                    linenum = True
                                    grid_box.Rows.Add()
                                End If
                            Catch ex As Exception
                            End Try
    
                            If linenum Then
                                grid_box.Rows(i - 1).Cells(0).Value = aux
                            End If
                            If line.Contains(" --> ") Then
                                splitLine = line.Split(" --> ")
                                t_inicio = splitLine(0)
                                t_final = splitLine(2)
                                grid_box.Rows(i - 1).Cells(1).Value = t_inicio
                                grid_box.Rows(i - 1).Cells(2).Value = t_final
                            Else
                                If (Not linenum) Then
                                    If (k = 1) Then
                                        words = line
                                        k = k + 1
                                    Else
                                        words = words & "<br>" & line
                                    End If
                                    grid_box.Rows(i - 1).Cells(3).Value = words
                                Else
                                    words = ""
                                End If
                            End If
                        End If
                        j = j + 1
                    Loop
                    If auxiliar = 1 Then
                        P1_PP.Text = "Ok!"
                    End If
                    If auxiliar = 2 Then
                        P2_PP.Text = "Ok!"
                    End If
                End Using
            Else
                MsgBox("El archivo esta vacio", MsgBoxStyle.Information, "Error")
            End If
            Return {i, j}
        End Function
    
        Private Sub Play_Click(sender As System.Object, e As System.EventArgs) Handles Play.Click
            Player.Show()
        End Sub
    End Class

    Player.vb
    Code:
    Imports System.IO
    Public Class Player
        Private Sub wmp_Enter(sender As System.Object, e As System.EventArgs) Handles wmp.Enter
            Dim File_Path As String
            Dim FolderBrowserDialog As New OpenFileDialog
            With FolderBrowserDialog
                .Filter = "Videos (*.avi)|*.avi|Videos (*.mkv)|*.mkv|Videos (*.mp4)|*.mp4|Videos (*.wmv)|*.wmv|Todos (*.*)|*.*"
                .FilterIndex = 1
                If .ShowDialog = DialogResult.OK Then
                    File_Path = .FileName
                    wmp.URL = .FileName
                    GroupBox1.Text = .Title
                End If
            End With
        End Sub
    End Class
    Cheers
    Attached Files Attached Files

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,676

    Re: Subtitle SRT Editor

    Is there a question here?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    2

    Re: Subtitle SRT Editor

    Quote Originally Posted by Jenner View Post
    Is there a question here?
    No, Not really something specific like you ask. Just thoughts, an opinion about functions that are outdated or maybe can be done better doing something else. I thought thats what a forum is for, more than just an ask and response thing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.