我正在制作一个.srt转换工具,该工具将抓取.srt文件的每个文本行,通过Google翻译进行翻译,然后将替换内容转换为新文件。除了进行实际替换外,我已完成所有工作。似乎应该很容易,但是我正在为迭代而苦苦挣扎。
这是我的代码:
Set IE = CreateObject("internetexplorer.application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceCaptionFile = objFSO.GetParentFolderName(WScript.ScriptFullName) & "\captions.srt"
OutputCaptionFile = objFSO.GetParentFolderName(WScript.ScriptFullName) & "\NEW_captions.srt"
on error resume next
langURL_es = "https://translate.google.com/#view=home&op=translate&sl=en&tl=es"
'============================================================
Set objFile = objFSO.OpenTextFile(SourceCaptionFile, 1, true) 'for reading
Do until objFile.AtEndOfStream
if instr(objFile.Readline,">") then
capText = capText & objFile.ReadLine & vbcrlf
end if
loop
objFile.Close
'============================================================
'Navigate to translator and get translations
IE.Visible = true
IE.Navigate langURL_es
Do While IE.Busy or IE.ReadyState <> 4: WScript.sleep 100: Loop
Do Until IE.Document.ReadyState = "complete": WScript.sleep 100: Loop
wscript.sleep 1000
capline = split(capText,vbcrlf)
for i = 0 to ubound(capline)
ie.document.getelementbyid("source").innertext = capline(i)
Do While IE.Busy or IE.ReadyState <> 4: WScript.sleep 100: Loop
Do Until IE.Document.ReadyState = "complete": WScript.sleep 100: Loop
for each div in ie.document.getelementsbytagname("div")
if div.getAttribute("class") = "result-shield-container tlid-copy-target" then
captrans = captrans & capline(i-1) & "," & div.innertext & vbcrlf
end if
next
next
'============================================================
'**************THIS IS THE SECTION THAT'S NOT WORKING RIGHT**************
'compare translations against captions.srt file and make replacements
Set objFile = objFSO.OpenTextFile(SourceCaptionFile, 1, true) 'for reading
splitfile = split(objfile.readall,vbcrlf)
for each a in splitfile
arrCaptrans = split(captrans,",")
for i = 0 to ubound(arrCaptrans)
if a = arrCaptrans(i) then
newline = newline & arrCaptrans(i+1) & vbcrlf
else
newline = newline & a & vbcrlf
end if
next
next
objFile.Close
wscript.echo newline
'============================================================
'Write translated file
Set objTransFile = objFSO.OpenTextFile(OutputCaptionFile, 2, true) 'for writing
objTransFile.write newline
objTransFile.Close
'============================================================
wscript.echo "Done"
这是我期望的输出:
1
00: 00: 06,800 -> 00: 00: 11,040
-¡Tenemos que averiguar cómo abrir esto!
2
00: 00: 11,080 -> 00: 00: 13,040
-¿Qué haces con ese nickel?
3
00: 00: 13,160 -> 00: 00: 20,440
-Por eso necesitamos sacar el dinero del chocolate de aquí.
4
00: 00: 20,440 -> 00: 00: 22,080
-No hay chocolate dentro de eso.
5
00: 00: 22,580 -> 00: 00: 23,960
-Sí hay
这是我的源“ captions.srt”文件的内容:
1
00:00:06,800 --> 00:00:11,040
-We have to figure out how to open this!
2
00:00:11,080 --> 00:00:13,040
-What are you doing with that nickel?
3
00:00:13,160 --> 00:00:20,440
-That's why we need to get the chocolate money out of here
4
00:00:20,440 --> 00:00:22,080
-There's no chocolate inside that.
5
00:00:22,580 --> 00:00:23,960
-Yes there is
*编辑: 我正在尝试将源.srt文件的每一行与我名为“ captrans”的字符串进行比较。 “ captrans”包含“ sourcetext,translatedtext”行。我用逗号分割,读取源文件的每一行,如果源文件行与captrans匹配(在逗号之前),然后替换为(逗号)在captrans中。希望这有意义...
感谢您提供的任何帮助!