vtt to srt. Video subtittles

On the Cousera platform I downloaded the subtittles of the courses, but they only were offering the *.vtt file, which is a superset of the *.srt that MediaPlayerClassics handle for subs. I was on the need to convert all from vtt to srt, but did not want to go through all files, delete the heading and save as new file, so, here is a macro that “hardly” goes for each folder and gets the job done.
Option Explicit
            
Function fFileLoad(ByRef strFullPathFile As String) As String()
    Dim iFile As Integer
    Dim lgLine As Long
    Dim aLine() As String
    Dim strLine As String

    iFile = VBA.FreeFile()
    Open strFullPathFile For Input Shared As #iFile
    Line Input #iFile, strLine
    Close #iFile
    'lgLine = 0
    'Do Until EOF(iFile)
        'lgLine = lgLine + 1
        'ReDim Preserve aLine(1 To lgLine)
        'aLine(lgLine) = VBA.Replace(strLine, vbLf, vbCrLf)
    'Loop
    aLine() = VBA.Split(strLine, vbLf)
    fFileLoad = aLine()
    Erase aLine()
End Function

Function fFoldersLoad(ByRef strPathBase As String) As String()
' get folders...
    Dim strPath As String
    Dim aFolder() As String
    Dim lgFolder As Long
    
    strPath = VBA.Dir(strPathBase, vbDirectory)
    lgFolder = 0
    Do
        If Not strPath Like ".*" Then
        If VBA.GetAttr(strPathBase & strPath) And vbDirectory Then
            lgFolder = lgFolder + 1
            ReDim Preserve aFolder(1 To lgFolder)
            aFolder(lgFolder) = strPathBase & strPath & "\"
        End If
        End If
        strPath = VBA.Dir
    Loop Until strPath = vbNullString

    fFoldersLoad = aFolder()
    Erase aFolder()
End Function

Function fFilesLoad(ByVal strPathBase As String, _
                    Optional ByVal strFilter As String = "*.*") As String()
' get Files...
    Dim strPath As String
    Dim aFile() As String
    Dim strFile As String
    Dim lgFile As Long
    
    strFile = VBA.Dir(strPathBase & strFilter, vbArchive)
    If strFile = vbNullString Then Exit Function
    lgFile = 0
    Do
        lgFile = lgFile + 1
        ReDim Preserve aFile(1 To lgFile)
        aFile(lgFile) = strPath & strFile
        strFile = VBA.Dir
    Loop Until strFile = vbNullString

    fFilesLoad = aFile()
    Erase aFile()
End Function

Function fVttToSrt(ByVal strFullPathFile As String) As Boolean
    Dim iFile As Integer
    Dim aLine() As String
    Dim lgLine As Long
    
    iFile = VBA.FreeFile()
    aLine() = fFileLoad(strFullPathFile)
    
    Open VBA.Replace(strFullPathFile, ".vtt", ".srt") For Output Shared As #iFile
    For lgLine = (LBound(aLine) + 2) To UBound(aLine)
        Print #iFile, aLine(lgLine)
    Next lgLine
    Close #iFile
End Function

Sub sVttToSrt()
    Dim aFile() As String
    Dim aFolder() As String
    Dim aSubFolder() As String
    Dim lgFolder As Long
    'Dim strFullPathFile As String
    Dim oFile As Variant
    Dim oFolder As Variant
    Dim oSubFolder As Variant
    Dim strPath As String
    Dim strPathBase As String
    Dim strFile As String
    Dim aLine() As String
    Dim lgLine As Long
    
    strPathBase = VBA.Environ$("UserProfile") & "\Downloads\" & "ANN\Andrew Ng_DeepLearning_Course\"
    Erase aFolder()
    aFolder() = fFoldersLoad(strPathBase)
    
    For Each oFolder In aFolder()
        strPathBase = VBA.CStr(oFolder)
        
        ' get subfolders
        Erase aSubFolder()
        aSubFolder() = fFoldersLoad(strPathBase)
        
        ' get files in root
        Erase aFile()
        aFile() = fFilesLoad(strPathBase, "*.vtt")
        If Not (Not aFile) Then
            For Each oFile In aFile()
                ' convert vtt to srt
                Call fVttToSrt(strPathBase & VBA.CStr(oFile))
            Next oFile
        End If
        
        ' go for subfolders
        If Not (Not aSubFolder) Then
            For Each oSubFolder In aSubFolder()
                strPathBase = VBA.CStr(oSubFolder)
                
                ' get files
                Erase aFile()
                aFile() = fFilesLoad(VBA.CStr(oSubFolder), "*.vtt")
                If Not (Not aFile) Then
                    For Each oFile In aFile()
                        ' convert vtt to srt
                        Call fVttToSrt(strPathBase & VBA.CStr(oFile))
                    Next oFile
                End If
            Next oSubFolder
        End If
    Next oFolder

End Sub
As mentioned, it’s not very sophisticated, but at least, finds the first subfolder structure in a folder, gets the files on the root and does the silly things to get the srt working on MPC. For a more advanced macro, that recursively gets all the folder structure, better look at this post.

Leave a Reply

Your email address will not be published. Required fields are marked *