ass2srt. VBS (script for batch conversion of SRT from Ass / SSA)

Time:2022-1-5

Oplayer in iPad only supports subtitles in SRT format, while animation generally uses subtitles in ass / SSA format, so ass / SSA needs to be converted to SRT in batch.

Google found an ass2srt in ass2srt [ass / SSA batch conversion SRT] WSF script, as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<job id="ass2srt">
  <script language="JScript">
  cInput="unicode";  // You can find them from:
  cOutput="utf-8";  // HKEY_CLASSES_ROOT\MIME\Database\Charset
  function rrr(){
    re = /Dialogue: [^,.]*[0-9]*,([1-9]?[0-9]*:[0-9]*:[0-9]*.[0-9]*),([1-9]?[0-9]*:[0-9]*:[0-9]*.[0-9]*),[^,.]*,[^,.]*,[0-9]*,[0-9]*,[0-9]*,[^,.]*,(.*)/gi;
    rv = ss.match(re);
    t1 = RegExp.$1;
    t2 = RegExp.$2;
    t3 = RegExp.$3;
    rg = /\{[^}.]*(\\pos\([0-9]*,[0-9]*\))[^}.]*}/gi;
    t3 = t3.replace(rg,"$1" + "}");
    rg =/\{[^}.]*}/gi;
    t3 = t3.replace(rg,"");
    rg =/(\\pos\([0-9]*,[0-9]*\)})/gi;
    t3 = t3.replace(rg,"{" + "$1");
    }
  </Script>
  <script language="VBScript">
  set ad=CreateObject("adodb.stream")
  set af=CreateObject("adodb.stream")
  set ass=CreateObject("adodb.stream")
  ad.open
  af.open
  ass.open
  ad.Charset=cInput
  af.Charset=cOutput
  ass.Charset=cOutput
  Set objArgs = WScript.Arguments
  For I = 0 to objArgs.Count - 1
    ad.LoadFromFile(objArgs(I))
    z=0
    gg=left(objArgs(I),len(objArgs(I))-3)&"srt"
    Do While ad.eos <> True
      ss =ad.ReadText(-2)
      if left(ss,8)="Dialogue" then
        ss=replace(ss,",,",",d,")
        rrr
        t3=replace(t3,"\n",vbcrlf)
        t3=replace(t3,"\N",vbcrlf)
        z=z+1
        af.writetext z,1
        af.writetext t1 & " --> " & t2,1
        af.writetext t3 & vbcrlf & vbcrlf
      else
        ass.writetext ss,1
      end if
    Loop
    af.savetofile gg,2
    ass.savetofile gg&".style",2
  Next
  if i=0 then
    msgbox "Please drag files to me!",,"Error!"
  else
    msgbox "Converted "&i&" file(s).",,"All Over!"
  end if
  </Script>
</job>

Windows Script (. WSF) file is a text document containing extensible markup language (XML) code. It combines several functions to improve the flexibility of script programming. Because Windows script files are not limited to specific engines, they can contain scripts of all script engines that follow ActiveX (R) specifications.

The above script file contains both JScript and VBScript code. The question is, is this necessary? Simply using JScript or VBScript can be realized. Why mix different languages? JScript’s role in the code is only regular expression. A reasonable inference is that the author will not VBScript’s regular expression, or dislike VBScript’s regular expression too much trouble. Even leaving aside the language mix, the style of the above code is really not flattering.

Here is my ass2srt VBS, maybe a little better than the above code.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Option Explicit
Const Encoding = "unicode" 'assume unicode
 
'Author: Demon
'Website: http://demon.tw
'Date: 2012/6/16
 
Dim shell, folder, fso, ext, i, args
Set shell = CreateObject("Shell.Application")
Set fso = CreateObject("scripting.filesystemobject")
Set args = WScript.Arguments
 
If args.Count = 0 Then
  Set folder = shell.BrowseForFolder(0, "Please select the folder where ass subtitles are located", 1)
  If folder Is Nothing Then WScript.Quit
  For Each i In fso.GetFolder(folder.Self.Path).Files
    ext = LCase(fso.GetExtensionName(i.Path))
    If ext = "ass" Or ext = "ssa" Then
      ASS2SRT i.Path, Encoding
    End If
  Next
Else
  For i = 0 To args.Count - 1
    ASS2SRT args(i), Encoding
  Next
End If
 
MsgBox CInt(i) & " file(s) Converted!", vbInformation
 
Function ASS2SRT(path, charset)
  Const adTypeText = 2
  Const adReadLine = -2
  Const adSaveCreateOverWrite = 2
  Dim ass, srt, re, str, arr, s, e, t, i
  
  Set ass = CreateObject("ADODB.Stream")
  Set srt = CreateObject("ADODB.Stream")
  Set re = New RegExp
  
  re.Global = True
  re.IgnoreCase = True
  re.Pattern = "\{.*?\}"
  
  ass.Type = adTypeText
  ass.Charset = charset
  ass.Open
  ass.LoadFromFile path
  
  srt.Type = adTypeText
  srt.Charset = "utf-8"
  srt.Open
  
  i = 0
  
  Do Until ass.EOS
    str = ass.ReadText(adReadLine)
    If Left(str, 8) = "Dialogue" Then
      i = i + 1
      arr = Split(str, ",", 10)
      s = "0" & arr(1) & "0" 'Start time
      e = "0" & arr(2) & "0" 'End time
      t = arr(9) 'Text
      s = Replace(s, ".", ",")
      e = Replace(e, ".", ",")
      t = re.Replace(t, "")
      t = Replace(t, "\n", vbCrLf)
      t = Replace(t, "\N", vbCrLf)
      srt.WriteText i & vbCrLf
      srt.WriteText s & " --> " & e & vbCrLf
      srt.WriteText t & vbCrLf & vbCrLf
    End If
  Loop
  
  path = Left(path, Len(path) - 3) & "srt"
  srt.SaveToFile path, adSaveCreateOverWrite
End Function

Save the above code as ass2srt VBS, and then drag the ass / SSA subtitle to ass2srt Vbs script. If you need to batch convert more ass / SSA subtitles, you can first put them in the same folder, and then directly double-click to run ass2srt VBS, select the folder where the caption is located.

Original text: http://demon.tw/my-work/ass2srt.html