Convert Text to SRT subtitle format

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 section is not for asking questions about writing your own macros.
Post Reply
User avatar
Robert Tucker
Volunteer
Posts: 1250
Joined: Mon Oct 08, 2007 1:34 am
Location: Manchester UK

Convert Text to SRT subtitle format

Post by Robert Tucker »

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

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
LibreOffice 7.x.x on Arch and Fedora.
Post Reply