Download sample SRT files

' Synchronize two SRT files (subtitles)
' Video

Set WshShell = CreateObject("Wscript.Shell")

Set toolkit = CreateObject("VbsEdit.Toolkit")
files=toolkit.OpenFileDialog("","SRT Files (*.srt)|*.srt",False,"Open a reference subtitle file")
If UBound(files)<0 Then
  Wscript.Quit
EndIf

refSource = files(0)

Set toolkit = CreateObject("VbsEdit.Toolkit")
files=toolkit.OpenFileDialog("","SRT Files (*.srt)|*.srt",False,"Open a subtitle file to synchronize")
If UBound(files)<0 Then
  Wscript.Quit
EndIf

source = files(0)

s = InputBox("Enter a few pairs of subtitles to synchronize","Synchronize","2-2,500-500,1000-1000")
If s=""Then
  Wscript.Quit
EndIf

arr_tt = Split(s,",")

sum_mult=0
sum_offset=0
count=0
For ii=0 To UBound(arr_tt)-1
  For jj=ii+1 To UBound(arr_tt)
    sub1 = Split(arr_tt(ii),"-")
    sub2 = Split(arr_tt(jj),"-")

    st1 = GetSubtitleTime(sub1(1),source)
    WScript.Echo st1
    ref1 = GetSubtitleTime(sub1(0),refsource)
    WScript.Echo ref1

    st2 = GetSubtitleTime(sub2(1),source)
    WScript.Echo st2
  
    ref2 = GetSubtitleTime(sub2(0),refsource)
    WScript.Echo ref2
    
    mult = (st1-st2)/(ref1-ref2)
    offset = st1-mult*ref1

    WScript.Echo mult & " " & offset
    WScript.Echo
     
    sum_mult=sum_mult+mult
    sum_offset=sum_offset+offset
    count=count+1
  Next
Next

mult=sum_mult/count
offset=sum_offset/count

WScript.Echo mult
WScript.Echo offset


Set fso = CreateObject("Scripting.Filesystemobject")

IfNot(fso.FileExists(source & ".bak")) Then
  fso.CopyFile source,source & ".bak",False
EndIf

dest = files(0) & ".txt"

from_time= ""
until_time = ""

shift_from=0
If from_time<>""Then
  shift_from = StringToSeconds(from_time)
EndIf

shift_until=10^8
If until_time<>""Then
  shift_until = StringToSeconds(until_time)
EndIf


Set objOutput = CreateObject("ADODB.Stream")
objOutput.Charset = "utf-8"
objOutput.Type = 2
objOutput.Open
objOutput.LineSeparator = -1


Set objInput = CreateObject("ADODB.Stream")
objInput.Charset = toolkit.Charset(source)
objInput.Type = 2
objInput.Open
objInput.LineSeparator = toolkit.lineseparator(source)
objInput.LoadFromFile source

DoWhileTrue

  IfNot(objInput.EOS) Then
    num = objInput.ReadText(-2)
  Else
    ExitDo
  EndIf
   
  objOutput.WriteText num,1

  IfNot(objInput.EOS) Then
    tt = objInput.ReadText(-2)
  Else
    ExitDo
  EndIf

  pos = InStr(tt," --> ")
  arr1=Split(Left(tt,pos),":")
  arr2=Split(Mid(tt,pos+5),":")

  t1 = StringToSeconds(Left(tt,pos))
  t2 = StringToSeconds(Mid(tt,pos+5))
   
  If t1>shift_from And t1<shift_until Then
    'WScript.Echo thetime(t1) & " --> " & thetime(t2)
    t1 = (t1-offset)/mult
    t2 = (t2-offset)/mult
  EndIf

  objOutput.WriteText SecondsToString(t1) & " --> " & SecondsToString(t2),1
  'WScript.Echo

  theText=""
  DoWhileNot(objInput.EOS)
    text = objInput.ReadText(-2)
   
   objOutput.WriteText text,1

    If text=""Then
      ExitDo
    EndIf

  Loop


Loop

objInput.Close
objOutput.SaveToFile dest,2
objOutput.Close

Function GetSubtitleTime(myNum,sourceFile)

  theTime=0

  Set obj = CreateObject("ADODB.Stream")
  obj.Charset = Toolkit.Charset(sourceFile)
  obj.LineSeparator = Toolkit.lineseparator(sourceFile)
  obj.Type = 2
  obj.Open
  obj.LoadFromFile sourceFile

  DoWhileTrue

    IfNot(obj.EOS) Then
      num = obj.ReadText(-2)
    Else
      ExitDo
    EndIf
   

    IfNot(obj.EOS) Then
      tt = obj.ReadText(-2)
    Else
      ExitDo
    EndIf

    pos = InStr(tt," --> ")
    arr1=Split(Left(tt,pos),":")
    arr2=Split(Mid(tt,pos+5),":")

    t1 = StringToSeconds(Left(tt,pos))
    t2 = StringToSeconds(Mid(tt,pos+5))
   
    If num=myNum Then
   
      theTime = (t1+t2)/2
      ExitDo
    EndIf

    theText=""
    DoWhileNot(obj.EOS)
      text = obj.ReadText(-2)
   
      If text=""Then
        ExitDo
      EndIf

    Loop


  Loop

  obj.Close

  GetSubtitleTime=theTime
EndFunction

Function SecondsToString(seconds)
  Dim t
  t=seconds
  h=Int(t/3600)
  t=t-h*3600
  If Len(h)=1 Then
    h="0" & h
  EndIf
  n= Int(t/60)
  t=t-n*60
  If Len(n)=1 Then
    n="0" & n
  EndIf
  s = Int(t)
  If Len(s)=1 Then
    s="0" & s
  EndIf
  m=t-s
  m=FormatNumber(m,3)

  SecondsToString = h & ":" & n & ":" & s & "," & Mid(m,3)
EndFunction


Function StringToSeconds(str)

  arr=Split(str,":")

  t = arr(0)*3600 + arr(1)*60

  s = Split(arr(2),",")

  If UBound(s)>=0 Then
  t = t + s(0)

    If UBound(s)>=1 Then
      t = t +  s(1)/1000
    EndIf
  EndIf
   
  StringToSeconds = t
EndFunction

search for scripts

VbsEdit contains all these sample scripts!


Download Now!



Download   Home   Scripts

Copyright © 2001-2024 adersοft