欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-02 16:26:52
ipad中的oplayer只支持srt格式的字幕,而动画一般使用的是ass/ssa格式的字幕,所以需要将ass/ssa批量转换srt。 google了一下,在《ass2s...

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,选择字幕所在的文件夹即可。

原文:

上一篇:

下一篇: