ass2srt.vbs(ass/ssa批量转换srt)

标签: , , , , , ,

iPad中的OPlayer只支持srt格式的字幕,而动画一般使用的是ass/ssa格式的字幕,所以需要将ass/ssa批量转换srt。

Google了一下,在《ass2srt[ass/ssa批量转换srt]》中找到一个ass2srt.wsf脚本,内容如下:

<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 脚本 (.wsf) 文件是一个包含可扩展标记语言(XML)代码的文本文档,它结合了若干功能,提高了脚本编程的灵活性。由于 Windows 脚本文件并不局限于特定的引擎,它们能够包含所有遵循 ActiveX(R)规范的脚本引擎的脚本。

上面的脚本文件同时包含了JScript和VBScript的代码。问题在于,有这个必要么?单纯用JScript或者VBScript都可以实现,为什么要混用不同的语言呢?JScript在代码中的作用仅仅是正则表达式而已,一个合理的推断是作者不会VBScript的正则表达式,或者嫌VBScript的正则表达式太麻烦。就算撇开语言混杂不说,上面代码的风格实在是不敢恭维。

下面是我写的ass2srt.vbs,也许比上面的代码好一点点罢。

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, "请选择ASS字幕所在的文件夹", 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

把上面代码保存为ass2srt.vbs,然后将需要转换的ass/ssa字幕拖动到ass2srt.vbs脚本上即可。如果需要批量转换的ass/ssa字幕比较多,可以先把它们放到同一个文件夹里,然后直接双击运行ass2srt.vbs,选择字幕所在的文件夹即可。

随机文章:

  1. PuTTY SSH Tunnel设置
  2. VB6拾遗:序
  3. 用VBS判断无BOM头的文件是否UTF-8编码
  4. 跨编译器的 C 语言 NaN 支持
  5. NDS上的MD模拟器jEnesisDS

4 条评论 发表在“ass2srt.vbs(ass/ssa批量转换srt)”上

  1. mike说道:

    楼主酱的东东字幕组人员肯定大爱呀~

  2. aric说道:

    发现楼主是个好人,更是技术牛人,用了你的这个ass2srt后别的软件一概弃用,谢谢啊。用了一段时间很好,特别是一般转换软件对于中英字幕,转换后就一行,而楼主这个可以换行,非常喜欢!不过今天发现不能转换成功的一个例子http://shooter.cn/xml/sub/246/246192.xml,转换完后文件啥内容也没有,楼主可以看看。

    • aric说道:

      发现问题了,原来的字幕编码UTF-8,改成UTF-16LE就可以了,我试了试将原来的ass改成UTF-8都是转换为空文件,不知道为何?

  3. 李磊说道:

    楼主是好人,那咱们来了就多点点广告。给楼主更好的支持。呵。

留下回复