Convert Text to SRT subtitle format

Creating Extension - Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This forum is not for asking questions about writing your own macros.

Convert Text to SRT subtitle format

Postby Robert Tucker » Thu Mar 20, 2014 5:05 pm

If you have the script for a talk or song edited so that you have a paragraph in a Writer document for each subtitle, this macro will produce an .srt styled document making the assumption that the speaker or singer is talking or singing at a constant character per second rate.

Code: Select all   Expand viewCollapse view
REM  *****  BASIC  *****

Sub Text2SRT
Dim oDoc as Object
Dim oTextEnum As Object
Dim oTextElement As Object
Dim oText As Object
Dim nAllChars as Integer
Dim nLnChars as Integer
Dim nStrStTime as String
Dim nStTime as Single
Dim nStrEndTime as String
Dim nEndTime as Single
Dim nVidTime as Single
Dim nLnTime as Single
Dim LnString as String
Dim nStrLnStTime as String
Dim nStrLnTime as String
Dim nStrLnEndTime as String
Dim n as Integer

     oDoc = ThisComponent


     
     nStrStTime  = InputBox("Start time of video in seconds:")
     nStTime = Val(nStrStTime)
     nStrEndTime  = InputBox("End time of video in seconds:")
     nEndTime = Val(nStrEndTime)
     nVidTime = nEndTime - nStTime

     oText = thisComponent.getText     
     oTextEnum =oText.createEnumeration 


     nAllChars = oDoc.CharacterCount

     n=0
     nLnStTime = nStTime
     
     
     While oTextEnum.hasMoreElements()
 
     n = n + 1
     oTextElement = oTextEnum.nextElement()
     LnString = oTextElement.string
     nLnChars = Len(oTextElement.string)
     nLnTime = nVidTime*(nLnChars/nAllChars)
     nLnEndTime = nLNStTime + nLnTime
     nLETms = FIX(nLnEndTime * 1000)
     nLSTms = FIX(nLnStTime * 1000)
     ooFmt = "00"
     oooFmt = "000"
     nLEThh = FIX(nLETms / (60 * 60 * 1000))
     nLETmm = FIX(nLETms / (60 * 1000) - (NLEThh * 60))
     nLETss = FIX((nLETms / 1000) - (nLEThh * 60 * 60) - (nLETmm * 60))
     nLETtt = FIX(nLETms - (nLEThh * 1000 * 60 * 60) - (nLETmm * 1000 * 60) - (nLETss * 1000))
     nLSThh = FIX(nLSTms / (60 * 60 * 1000))
     nLSTmm = FIX(nLSTms / (60 * 1000) - (nLSThh * 60))
     nLSTss = FIX((nLSTms / 1000) - (nLSThh * 60 * 60) - (nLSTmm * 60))
     nLSTtt = FIX(nLSTms - (nLSThh * 1000 * 60 * 60) - (nLSTmm * 1000 * 60) - (nLSTss * 1000))
     nStrLnStTime = CStr(FORMAT(nLSThh, ooFmt))&":"&CStr(FORMAT(nLSTmm, ooFmt))&":"&CStr(FORMAT(nLSTss, ooFmt))&","&CStr(FORMAT(nLSTtt, oooFmt))
     nStrLnEndTime = CStr(FORMAT(nLEThh, ooFmt))&":"&CStr(FORMAT(nLETmm, ooFmt))&":"&CStr(FORMAT(nLETss, ooFmt))&","&CStr(FORMAT(nLETtt, oooFmt))
     If n =1 then
     oTextElement.string = CStr(n)&chr(13)&CStr(nStrLnStTime)&" --> "&CStr(nStrLnEndTime)&chr(13)&CStr(LnString)&chr(13)
     else
     oTextElement.string = CStr(n)&chr(13)&CStr(nStrLnStTime)&" --> "&CStr(nStrLnEndTime)&chr(13)&CStr(LnString)
     End if
     nLnStTime = nLnEndTime
     
     Wend
     

End Sub


[Anyone wondering what I'm talking about: http://scriptease.wix.com/subtitling#!page2/cjg9]
LibreOffice 5.x.x on Fedora 27 and Ubuntu 17.10 (Dual Boot)
User avatar
Robert Tucker
Volunteer
 
Posts: 1246
Joined: Mon Oct 08, 2007 1:34 am
Location: Manchester UK

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 3 guests