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

[文本处理] [已解决]求联想语音带时间轴的txt转srt的批处理

本帖最后由 vwfgh 于 2022-2-9 09:37 编辑

新人第一次发帖,想求论坛里的大佬解答个问题!

原文本格式是这样的,星号后面是时间轴,只有开始时间,结束时间用下一句的开始时间就行:

2022/2/6 13:01:56 **** 00:00:01.9180000 **** english **** chinese
xxxxxxxx

或者是这样的:
2022/2/6 14:28:40 **** 00:00:06.0180000
xxxxxxxxx



想转换成srt的格式,我知道缺少每句话的结束时间,结束时间就用下一句的开始时间就行:

1
00:00:13,089 --> 00:00:21,649
xxxxxxxxx


源格式有两种,哪个方便转换成srt就用哪个,谢谢大佬。不限于批处理,python等其他方法只要能实现都行。

顺便说一下,这是联想语音助手生成的字幕文件。语音转文字准确率挺高的,就是字幕格式太迷了。

  1. #@&cls&powershell "type %~s0|out-string|iex"&pause&exit
  2. sc a.srt $(
  3. gc a.txt|%{
  4. if($_ -replace '\.',',' -match '\d\d:\d\d:\d\d,\d{3}'){
  5. $a+=@($matches[0])
  6. if($a.count -eq 3){$i++;$i;'{0} --> {1}' -f $a[0],$a[2];$a[1],'';$a=@($a[2])}
  7. }else{$a+=@($_)}
  8. }
  9. $i++;$i;$a
  10. )
复制代码

TOP

本帖最后由 vwfgh 于 2022-2-6 18:36 编辑

回复 2# idwma


谢谢大佬,有个问题转过来的是乱码。我源文件是utf-8格式的,改成ansi以后正常了,但是我的文件很多,一个个改太麻烦了,能修改下脚本吗?

还有我想请问一下如果源文件是两行的双语字幕,批处理该怎么写?

TOP

  1. <#*,:&cls
  2. @echo off
  3. cd /d "%~dp0"
  4. powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. pause
  6. exit /b
  7. #>
  8. # 功能:当前目录txt转换成srt的格式,我知道缺少每句话的结束时间,结束时间就用下一句的开始时间就行:
  9. function GenSrt {
  10.   param(
  11.     [string]$Source,
  12.     [string]$Output
  13.   )
  14.   $re = New-Object System.Text.RegularExpressions.Regex -ArgumentList @('^[^*\n]+\*+\s*(?<begin1>\d{2}:\d{2}:\d{2})\.(?<begin2>\d{3}).*$\n(?<talk>[\s\S]*?)(?=^[^*\n]+\*+\s*(?<end1>\d{2}:\d{2}:\d{2})\.(?<end2>\d{3})|\z)', 'Multiline,Compiled')
  15.   $evaluator = [System.Text.RegularExpressions.MatchEvaluator] {
  16.     param([System.Text.RegularExpressions.Match]$m)
  17.     $Script:ctr++
  18.     # Write-Host ($m.Groups['talk'].Value) -ForegroundColor Green
  19.     if ($m.Groups['end1'].Success) {
  20.       $Script:format -f $Script:ctr, $m.Groups['begin1'].Value, $m.Groups['begin2'].Value, $m.Groups['end1'].Value, $m.Groups['end2'].Value, $m.Groups['talk'].Value
  21.     } else {
  22.       $Script:format -f $Script:ctr, $m.Groups['begin1'].Value, $m.Groups['begin2'].Value, $m.Groups['begin1'].Value, $m.Groups['begin2'].Value, $m.Groups['talk'].Value
  23.     }
  24.   }
  25.   $Script:ctr = 0
  26.   [System.IO.File]::WriteAllText($Output, ($re.Replace( [System.IO.File]::ReadAllText($Source, $encoding), $evaluator )), $encoding)
  27. }
  28. $encoding = [System.Text.Encoding]::UTF8
  29. $Script:format = "{0}`r`n{1},{2} --> {3},{4}`r`n{5}`r`n"
  30. Get-ChildItem -Path "*.txt" | ForEach-Object {
  31.   if (-not $_.PSIsContainer) {
  32.     $dstfile = $_.BaseName + ".srt"
  33.     Write-Host "$($_.Name) --> $dstfile"
  34.     GenSrt -Source $_ -Output $dstfile
  35.   }
  36. }
复制代码
微信:flashercs
QQ:49908356

TOP

回复 4# flashercs

谢谢大佬,分毫不差!

TOP

回复 3# vwfgh
  1. #@&cls&powershell "type %~s0|out-string|iex"&pause&exit
  2. foreach($j in dir *.txt){
  3. sc -enc utf8 $($j.basename+'.srt') $(
  4. gc -enc utf8 $j|%{
  5. if($_ -replace '\.',',' -match '\d\d:\d\d:\d\d,\d{3}'){
  6. $a+=@($matches[0])
  7. if($a.count -gt 1){$i++;$i;'{0} --> {1}' -f $a[0],$a[-1];$a[1..$($a.count-2)],'';$a=@($a[-1])}
  8. }else{$a+=@($_)}
  9. }
  10. $i++;$i;$a;rv a,i
  11. )
  12. }
复制代码

TOP

回复 6# idwma


    非常感谢!

TOP

返回列表