【问题标题】:VBScript to remove duplicate numbers from subtitle file by re-numberingVBScript通过重新编号从字幕文件中删除重复的数字
【发布时间】:2017-12-18 08:51:20
【问题描述】:

我有一个如下所示的字幕 (.srt) 文件:

2
00:04:22,504 --> 00:04:23,520
Hello?

3
00:04:27,860 --> 00:04:29,112
Hey wait!
Hello!

3
00:06:18,860 --> 00:06:21,112
Uhh!

3
00:06:29,860 --> 00:06:32,112
Ah!

4
00:07:19,232 --> 00:07:21,284
What are you doing here?

5
00:07:21,608 --> 00:07:22,708
Tell me!

...

如您所见,数字 3 在该文件中重复了三次,我想通过重新编号整个字幕文件来替换它(因为我猜这是唯一的选择,因为这种重复在多个地方这个文件)。

我在下面创建了用于选择该文件的脚本,并试图用新生成的数字(迭代数字)替换重复的数字,但它不起作用。

Dim strFile, objFS

strFile = SelectFile( )
If strFile = "" Then
    WScript.Echo "No file selected."
End If


Function SelectFile( )
    Dim objExec, strMSHTA, wshShell

    SelectFile = ""

    strMSHTA = "mshta.exe ""about:" & "<" & "input type=file id=FILE>" _
             & "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _
             & ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>"""

    Set wshShell = CreateObject( "WScript.Shell" )
    Set objExec = wshShell.Exec( strMSHTA )

    SelectFile = objExec.StdOut.ReadLine( )

    Set objExec = Nothing
    Set wshShell = Nothing
End Function

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile(strFile)
Set objFile2 = objFS.OpenTextFile(strFile, 8, True)
x = 0
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    Set objRegEx = CreateObject("VBScript.RegExp")
    objRegEx.Global = True
    objRegEx.Pattern = "^\d+$"
    Set colMatches = objRegEx.Execute(strLine)
    If colMatches.Count > 0 Then
        x = x + 1
        strLine = x
        strNewLine = Replace(strLine,strLine,x)
        objFile2.WriteLine strLine
    End If
Loop

任何人都可以帮忙,弄清楚如何使它工作?

【问题讨论】:

  • 可以用vbscript以外的其他技术来完成吗?
  • 因为到目前为止我已经创建了 VBScript,所以我更喜欢基于 vbscript 的答案,但是如果您有有效的解决方案,请在评论中添加您的答案或将网址添加到您的脚本中。

标签: regex replace vbscript pattern-matching subtitle


【解决方案1】:

在 VBScript 中使用 regular expressionreplacement function 和一个全局计数器:

f = "C:\path\to\your.srt"
n = 1  'global counter

Function Renumber(m, g1, g2, pos, src)
  Renumber = g1 & n & g2
  n = n + 1  'increment global counter after current value was used
End Function

Set re = New RegExp
re.Pattern = "(^|\r\n\r\n)\d+(\r\n)"
re.Global = True

Set fso = CreateObject("Scripting.FileSystemObject")
txt = fso.OpenTextFile(f).ReadAll
txt = re.Replace(txt, GetRef("Renumber"))
fso.OpenTextFile(f, 2).Write txt

【讨论】:

    【解决方案2】:

    如果你有一个Unix 盒子,或者一个Unix 虚拟机,或者如果你可以用awk 模拟一个Unix 环境,它可以在一行中完成:

    命令:

    awk 'BEGIN{c=1} $0~/^[0-9]+$/ {print c++} $0~/[a-zA-Z,:\-!?]|^$/{print}' input_sub.txt > output_sub.txt
    

    测试日期:

    2
    00:04:22,504 --> 00:04:23,520
    Hello?
    
    3
    00:04:27,860 --> 00:04:29,112
    Hey wait!
    Hello!
    
    3
    00:06:18,860 --> 00:06:21,112
    Uhh!
    
    3
    00:06:29,860 --> 00:06:32,112
    Ah!
    
    4
    00:07:19,232 --> 00:07:21,284
    What are you doing here?
    
    5
    00:07:21,608 --> 00:07:22,708
    Tell me!
    

    输出:

    1
    00:04:22,504 --> 00:04:23,520
    Hello?
    
    2
    00:04:27,860 --> 00:04:29,112
    Hey wait!
    Hello!
    
    3
    00:06:18,860 --> 00:06:21,112
    Uhh!
    
    4
    00:06:29,860 --> 00:06:32,112
    Ah!
    
    5
    00:07:19,232 --> 00:07:21,284
    What are you doing here?
    
    6
    00:07:21,608 --> 00:07:22,708
    Tell me!
    

    【讨论】:

      猜你喜欢
      • 2017-06-20
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      相关资源
      最近更新 更多