[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] [已解决]求助批处理提取转换时间和提取文字

本帖最后由 tianzi 于 2023-12-18 22:21 编辑

这是一种xml字幕格式,需要转换为srt字幕格式。想法是拖入xml字幕到批处理,就自动转换xml文件所在目录中所有的xml字幕文件为srt字幕文件,并自动退出。谢谢大佬出手相助
srt格式样本
  1. 1
  2. 00:01:02,875 --> 00:01:03,833
  3. 瞎子
  4. 2
  5. 00:01:04,708 --> 00:01:06,792
  6. 赢了赌坊这么多钱
  7. 3
  8. 00:01:07,333 --> 00:01:08,292
  9. 还不走啊
  10. 4
  11. 00:01:13,000 --> 00:01:14,750
  12. 我们来赌个大的如何
  13. 5
  14. 00:01:19,833 --> 00:01:21,167
  15. 赢了
  16. 6
  17. 00:01:21,250 --> 00:01:23,167
  18. 桌上的钱翻倍
  19. 7
  20. 00:01:24,000 --> 00:01:25,042
  21. 输了
复制代码
xml代码示例
  1. <xml>
  2. <dia>
  3. <st>39980</st>
  4. <et>40950</et>
  5. <sub>
  6. <![CDATA[ 瞎子 ]]>
  7. </sub>
  8. <style name="style" version="2">
  9. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  10. </style>
  11. </dia>
  12. <dia>
  13. <st>41980</st>
  14. <et>43990</et>
  15. <sub>
  16. <![CDATA[ 贏了賭坊這麼多錢 ]]>
  17. </sub>
  18. <style name="style" version="2">
  19. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  20. </style>
  21. </dia>
  22. <dia>
  23. <st>44350</st>
  24. <et>45180</et>
  25. <sub>
  26. <![CDATA[ 還不走啊 ]]>
  27. </sub>
  28. <style name="style" version="2">
  29. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  30. </style>
  31. </dia>
  32. <dia>
  33. <st>49870</st>
  34. <et>51690</et>
  35. <sub>
  36. <![CDATA[ 我們來賭個大的如何 ]]>
  37. </sub>
  38. <style name="style" version="2">
  39. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  40. </style>
  41. </dia>
  42. <dia>
  43. <st>56390</st>
  44. <et>60270</et>
  45. <sub>
  46. <![CDATA[ 贏了 桌上的錢翻倍 ]]>
  47. </sub>
  48. <style name="style" version="2">
  49. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  50. </style>
  51. </dia>
  52. <dia>
  53. <st>60350</st>
  54. <et>64510</et>
  55. <sub>
  56. <![CDATA[ 輸了 得留下你這隻手 ]]>
  57. </sub>
  58. <style name="style" version="2">
  59. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  60. </style>
  61. </dia>
  62. <dia>
  63. <st>66430</st>
  64. <et>67830</et>
  65. <sub>
  66. <![CDATA[ 左手還是右手 ]]>
  67. </sub>
  68. <style name="style" version="2">
  69. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  70. </style>
  71. </dia>
  72. <dia>
  73. <st>68270</st>
  74. <et>69710</et>
  75. <sub>
  76. <![CDATA[ 這瞎子行不行啊 ]]>
  77. </sub>
  78. <style name="style" version="2">
  79. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  80. </style>
  81. </dia>
  82. <dia>
  83. <st>86350</st>
  84. <et>89430</et>
  85. <sub>
  86. <![CDATA[ 瞎子 你輸了 ]]>
  87. </sub>
  88. <style name="style" version="2">
  89. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  90. </style>
  91. </dia>
  92. <dia>
  93. <st>99670</st>
  94. <et>100509</et>
  95. <sub>
  96. <![CDATA[ 全是白的 ]]>
  97. </sub>
  98. <style name="style" version="2">
  99. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  100. </style>
  101. </dia>
  102. <dia>
  103. <st>100509</st>
  104. <et>102190</et>
  105. <sub>
  106. <![CDATA[ 這麼多明眼人看著 ]]>
  107. </sub>
  108. <style name="style" version="2">
  109. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  110. </style>
  111. </dia>
  112. <dia>
  113. <st>102580</st>
  114. <et>104229</et>
  115. <sub>
  116. <![CDATA[ 你們就欺負我這個瞎子 ]]>
  117. </sub>
  118. <style name="style" version="2">
  119. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  120. </style>
  121. </dia>
  122. <dia>
  123. <st>110910</st>
  124. <et>113229</et>
  125. <sub>
  126. <![CDATA[ 在這裡我是莊家 ]]>
  127. </sub>
  128. <style name="style" version="2">
  129. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  130. </style>
  131. </dia>
  132. <dia>
  133. <st>113509</st>
  134. <et>114750</et>
  135. <sub>
  136. <![CDATA[ 我說了算 ]]>
  137. </sub>
  138. <style name="style" version="2">
  139. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  140. </style>
  141. </dia>
  142. <dia>
  143. <st>115910</st>
  144. <et>116830</et>
  145. <sub>
  146. <![CDATA[ 來人 ]]>
  147. </sub>
  148. <style name="style" version="2">
  149. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  150. </style>
  151. </dia>
  152. <dia>
  153. <st>3040870</st>
  154. <et>3042270</et>
  155. <sub>
  156. <![CDATA[ 全城設伏 ]]>
  157. </sub>
  158. <style name="style" version="2">
  159. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  160. </style>
  161. </dia>
  162. <dia>
  163. <st>3042830</st>
  164. <et>3043750</et>
  165. <sub>
  166. <![CDATA[ 另外 ]]>
  167. </sub>
  168. <style name="style" version="2">
  169. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  170. </style>
  171. </dia>
  172. <dia>
  173. <st>3043950</st>
  174. <et>3045990</et>
  175. <sub>
  176. <![CDATA[ 琵琶女帶人跑不了多遠 ]]>
  177. </sub>
  178. <style name="style" version="2">
  179. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  180. </style>
  181. </dia>
  182. <dia>
  183. <st>3046430</st>
  184. <et>3047550</et>
  185. <sub>
  186. <![CDATA[ 沿途追擊 ]]>
  187. </sub>
  188. <style name="style" version="2">
  189. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  190. </style>
  191. </dia>
  192. <dia>
  193. <st>3050350</st>
  194. <et>3052230</et>
  195. <sub>
  196. <![CDATA[ 不惜一切代價 ]]>
  197. </sub>
  198. <style name="style" version="2">
  199. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  200. </style>
  201. </dia>
  202. <dia>
  203. <st>4195390</st>
  204. <et>4197710</et>
  205. <sub>
  206. <![CDATA[ 那年與成瞎子相識 ]]>
  207. </sub>
  208. <style name="style" version="2">
  209. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  210. </style>
  211. </dia>
  212. <dia>
  213. <st>4198790</st>
  214. <et>4200110</et>
  215. <sub>
  216. <![CDATA[ 我十九歲 ]]>
  217. </sub>
  218. <style name="style" version="2">
  219. <position alignment="BottomCenter" horizontal-margin="50%" vertical-margin="94%"/>
  220. </style>
  221. </dia>
  222. </xml>
复制代码
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

回复 17# aloha20200628


    谢谢,我再试试

TOP

回复 16# tianzi

我的测试环境是win8.1简中系统
代码存为批处理脚本文件 test.cmd
测试数据文件采用一楼提供的222行样本数据,存为1.xml,采用简中编码或记事本默认ANSI编码
命令行用法》test.cmd 1.xml 预览结果如下(共22行)
  1. 1
  2. 00:00:39,980 --> 00:00:40,950
  3. 瞎子
  4. 2
  5. 00:00:41,980 --> 00:00:43,990
  6. 贏了賭坊這麼多錢
  7. 3
  8. 00:00:44,350 --> 00:00:45,180
  9. 還不走啊
  10. 4
  11. 00:00:49,870 --> 00:00:51,690
  12. 我們來賭個大的如何
  13. 5
  14. 00:00:56,390 --> 00:01:00,270
  15. 贏了 桌上的錢翻倍
  16. 6
  17. 00:01:00,350 --> 00:01:04,510
  18. 輸了 得留下你這隻手
  19. 7
  20. 00:01:06,430 --> 00:01:07,830
  21. 左手還是右手
  22. 8
  23. 00:01:08,270 --> 00:01:09,710
  24. 這瞎子行不行啊
  25. 9
  26. 00:01:26,350 --> 00:01:29,430
  27. 瞎子 你輸了
  28. 10
  29. 00:01:39,670 --> 00:01:40,509
  30. 全是白的
  31. 11
  32. 00:01:40,509 --> 00:01:42,190
  33. 這麼多明眼人看著
  34. 12
  35. 00:01:42,580 --> 00:01:44,229
  36. 你們就欺負我這個瞎子
  37. 13
  38. 00:01:50,910 --> 00:01:53,229
  39. 在這裡我是莊家
  40. 14
  41. 00:01:53,509 --> 00:01:54,750
  42. 我說了算
  43. 15
  44. 00:01:55,910 --> 00:01:56,830
  45. 來人
  46. 16
  47. 00:50:40,870 --> 00:50:42,270
  48. 全城設伏
  49. 17
  50. 00:50:42,830 --> 00:50:43,750
  51. 另外
  52. 18
  53. 00:50:43,950 --> 00:50:45,990
  54. 琵琶女帶人跑不了多遠
  55. 19
  56. 00:50:46,430 --> 00:50:47,550
  57. 沿途追擊
  58. 20
  59. 00:50:50,350 --> 00:50:52,230
  60. 不惜一切代價
  61. 21
  62. 01:09:55,390 --> 01:09:57,710
  63. 那年與成瞎子相識
  64. 22
  65. 01:09:58,790 --> 01:10:00,110
  66. 我十九歲
复制代码

TOP

回复 15# aloha20200628


    谢谢大佬,我运行无结果

TOP

本帖最后由 aloha20200628 于 2023-12-18 23:23 编辑


也给一个cmd+jscript混编版本,存为批处理脚本如 test.cmd 或 test.bat
预览结果的用法示例》test.cmd 1.xml (亦可将 1.xml 直接托给 test.cmd)
保存结果文件的用法示例》test.cmd 1.xml>1.srt
假设输入文件 1.xml 是简中编码,内容为一楼示例样本
  1. @set @v=1 /*
  2. @echo off
  3. if "%~1"=="" exit/b
  4. findstr /bi "<st> <et> <!" "%~1" | cscript /e:jscript "%~f0" & exit/b
  5. */
  6. k=0, n=0, ws=WSH.stdin;
  7. while (!ws.atendofstream) {
  8. n++; line=ws.readline();
  9. t=parseInt(line.replace(/[^\d]*(\d+)</, '$1'));
  10. if (n == 1) { st=mssToHMS(t); }
  11. else if (n == 2) { et=mssToHMS(t); WSH.echo(++k +'\n'+ st +' --> '+ et); }
  12. else { n=0; sub=line.replace(/[^ ]+\[ (.*) \].*/, '$1'); WSH.echo(sub+'\n'); }
  13. }
  14. // 毫秒格式化为 hh:mm:ss,nnn
  15. function mssToHMS (mss) {
  16. var h=parseInt((mss%86400000)/3600000);
  17. var m=parseInt((mss%3600000)/60000);
  18. var s=(mss%60000)/1000;
  19. h=h<10 ? '0'+h : h; //补零
  20. m=m<10 ? '0'+m : m; //补零
  21. s=s<10 ? '0'+s.toFixed(3) : s.toFixed(3); //补零
  22. return h +':'+ m +':'+ s.replace('.', ',');
  23. }
复制代码

TOP

本帖最后由 tianzi 于 2023-12-18 22:20 编辑

回复 13# idwma


    可以了,向你学习

TOP

忘记要拖放了
  1. #@&cls&powershell "$a='%~1';type '%~f0'|out-string|iex"&pause&exit
  2. [regex]::Matches(
  3. (gc -enc utf8 $a) -join "`n",
  4. '<st>(\d+)</st>\s*<et>(\d+)</et>[\s\S]+?<!\[\w+\[\s*(.+?)\s*\]\]>'
  5. )|%{
  6. $m=$_.Groups
  7. $st=[timespan]::FromMilliseconds($m[1].value)
  8. $et=[timespan]::FromMilliseconds($m[2].value)
  9. "{0}`r`n{1:hh\:mm\:ss\,fff} --> {2:hh\:mm\:ss\,fff}`r`n{3}`r`n" -f ++$i, $st, $et, $m[3].value
  10. }|sc ($a -replace '.xml$','.str')
复制代码
1

评分人数

TOP

回复 11# WHY


    厉害,可以将拖入的那个输出

TOP

本帖最后由 WHY 于 2023-12-18 23:26 编辑

Test.js
  1. var arg = WSH.Arguments;
  2. var fso = new ActiveXObject('Scripting.FileSystemObject');
  3. if (arg.length == 0 || fso.folderExists(arg(0)) || !/\.xml$/i.test(arg(0))){
  4.     WSH.Echo('拖入一个xml文件');
  5.     WSH.Quit();
  6. }
  7. var formatTime = function(t) {
  8.     var ms = ('' + (1000 + t % 1000)).substr(1);                 //毫秒
  9.     t = Math.floor(t/1000);                                      //总秒数
  10.     var hh = ('' + (100 + Math.floor(t / 3600))).substr(1);      //时
  11.     var mm = ('' + (100 + Math.floor(t % 3600 / 60))).substr(1); //分
  12.     var ss = ('' + (100 + t % 60)).substr(1);                    //秒
  13.     return hh + ':' + mm + ':' + ss + ',' + ms;
  14. }
  15. var arr = [];
  16. var xml = new ActiveXObject('Microsoft.XMLDOM');
  17. xml.load(arg(0));
  18. var nodes = xml.getElementsByTagName('dia');
  19. for(var i=0; i<nodes.length; i++) {
  20.     var st = nodes[i].selectSingleNode('st').text;        //开始时间
  21.     var et = nodes[i].selectSingleNode('et').text;        //结束时间
  22.     var t1 = formatTime(st) + ' --> ' + formatTime(et);   //时间格式化
  23.     var s1 = nodes[i].selectSingleNode('sub').text;       //字幕
  24.     arr.push(i + 1 + '\r\n' + t1 + '\r\n' + s1);
  25. }
  26. var dstFile = arg(0).replace(/xml$/i, 'srt');              //srt文件名
  27. fso.CreateTextFile(dstFile, true, true).WriteLine(arr.join('\r\n\r\n'));  //保存
复制代码

TOP

回复 9# idwma


    大佬厉害,文件名改成1.xml后能够转出来!

TOP

  1. #@&cls&powershell "type '%~f0'|out-string|iex"&pause&exit
  2. [regex]::Matches(
  3. (gc -enc utf8 1.xml) -join "`n",
  4. '<st>(\d+)</st>\s*<et>(\d+)</et>[\s\S]+?<!\[\w+\[\s*(.+?)\s*\]\]>'
  5. )|%{
  6. $m=$_.Groups
  7. $st=[timespan]::FromMilliseconds($m[1].value)
  8. $et=[timespan]::FromMilliseconds($m[2].value)
  9. "{0}`r`n{1:hh\:mm\:ss\,fff} --> {2:hh\:mm\:ss\,fff}`r`n{3}`r`n" -f ++$i, $st, $et, $m[3].value
  10. }|sc 1.srt
复制代码

TOP

回复 7# pd1

我也没有拿到正确的时间,猜测是1000 60 60 60这样的进率关系,xml是毫秒计数,然后需要按进率进行转换,还原成xx.xx.xx.xxx的格式

TOP

回复 5# tianzi


    那你是要别人去猜2种格式时间的对应关系啊,阻挠问题解决啊

TOP

回复 5# tianzi


    那么,你的实际需求是根据什么规律进行时间转换呢?
测试代码之前请做好备份

TOP

本帖最后由 tianzi 于 2023-12-18 15:52 编辑

回复 4# ShowCode


    这是两个不同的文件,第一个srt不一定是由下面的xml转换而来,只是示范一下srt文件的格式

TOP

返回列表