批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[文本处理] [已解决]求助bat批处理更改时间

本帖最后由 tianzi 于 2021-11-25 22:27 编辑

有一批如下的格式的srt字幕,需要修正其时间轴:
1
00:00:55,997 --> 00:00:58,866
当人们发现你是一个团队成员的时候

2
00:00:58,933 --> 00:01:00,433
他们问的第一个问题便是

3
00:01:00,501 --> 00:01:03,336
“你杀了多少人”

4
00:01:03,403 --> 00:01:07,374
从来不会有人问过你救了多少人

5
00:01:32,265 --> 00:01:35,402
女士 你没事儿吧

6
00:01:35,468 --> 00:01:36,737
感到恶心吗

7
00:01:38,238 --> 00:01:40,273
这是我头一回坐直升机

8
00:01:42,075 --> 00:01:43,678
只需要保持深呼吸就好了 明白吗

9
00:01:43,744 --> 00:01:45,178
眼睛望着地平线

10
00:02:13,841 --> 00:02:15,308
我很好

1.如第1行 00:00:58,866,延后150毫秒左右,可取整
   如第2行 00:00:58,933,提前500毫秒左右,可取整
   后面的依次类推,形成新的时间轴文件
2.新生成的文件删除开始时间与结束时间差小于1秒的行
3.最后剩下的行保存为:原名-副本.srt
谢谢!

本帖最后由 flashercs 于 2021-11-19 05:45 编辑
  1. <#*,:&cls
  2. @echo off
  3. pushd "%~dp0"
  4. powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. popd
  6. exit /b
  7. #>
  8. function GenSrt {
  9.   param(
  10.     [string]$Source,
  11.     [string]$Output,
  12.     [timespan]$EndTimeDiff = ([timespan]::FromMilliseconds(150)),
  13.     [timespan]$StartTimeDiff = ([timespan]::FromMilliseconds(-500)),
  14.     [timespan]$MinDiff = ([timespan]::FromMilliseconds(1000))
  15.   )
  16.   $re = New-Object System.Text.RegularExpressions.Regex -ArgumentList @("(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})", [System.Text.RegularExpressions.RegexOptions]::Compiled)
  17.   # $re = [regex]"(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})"
  18.   $format = "hh\:mm\:ss\,fff"
  19.   $timelineformat = "{0:$format} --> {1:$format}"
  20.   Get-Content -Path $Source -Encoding utf8 | ForEach-Object -Begin {
  21.     $lineCtr = 0
  22.     $section = @([timespan]::Zero.Add($EndTimeDiff), $null)
  23.     $sectionIndex = 1
  24.   } -Process {
  25.     if ($lineCtr -eq 1) {
  26.       $match = $re.Match($_)
  27.       if ($match.Success) {
  28.         $section[1] = [timespan]::ParseExact($match.Groups[1].Value, $format, $null).Add($StartTimeDiff)
  29.         if ($section[1].Subtract($section[0]).Duration() -ge $MinDiff ) {
  30.           $sectionIndex
  31.           $timelineformat -f $section
  32.           ""
  33.           ""
  34.           $sectionIndex++
  35.         }
  36.         # $timelineformat -f $section | Write-Host -ForegroundColor Green
  37.         $section[0] = [timespan]::ParseExact($match.Groups[2].Value, $format, $null).Add($EndTimeDiff)
  38.       }
  39.     }
  40.     if ($lineCtr -ge 3) {
  41.       $lineCtr = 0
  42.     } else {
  43.       $lineCtr++
  44.     }
  45.   } | Set-Content -Path $Output -Encoding utf8
  46. }
  47. # [System.IO.Directory]::CreateDirectory($dstdir) | Out-Null
  48. Get-ChildItem -Path "*.srt" -Filter "*.srt" -Exclude "*-副本.srt" | ForEach-Object {
  49.   if (-not $_.PSIsContainer) {
  50.     $dstfile = $_.BaseName + "-副本.srt"
  51.     Write-Host "$($_.Name) --> $dstfile"
  52.     GenSrt -Source $_ -Output $dstfile
  53.   }
  54. }
复制代码
微信:flashercs
QQ:49908356

TOP

本帖最后由 tianzi 于 2021-11-25 22:28 编辑

回复 2# flashercs
谢谢,可以生成副本srt,只是生成的数量少了
另外,可以直接在字幕目录双击bat完成,直接在源字幕文件夹生存,不用单独再生成文件夹吗,运行完毕后自动关闭吗?

TOP

回复 3# tianzi


    已修改了.
微信:flashercs
QQ:49908356

TOP

本帖最后由 tianzi 于 2021-11-25 22:28 编辑

回复 4# flashercs
这次能有80行了,从第5行差异就拉大了
手动:
    1
00:00:00,150 --> 00:01:00,460


2
00:01:32,310 --> 00:01:33,860


3
00:01:36,350 --> 00:01:39,000


4
00:01:52,130 --> 00:01:53,680


5
00:02:10,850 --> 00:02:12,770


6
00:02:14,150 --> 00:02:15,700


7
00:02:26,260 --> 00:02:27,810


8
00:02:35,070 --> 00:02:38,830


9
00:02:44,250 --> 00:02:59,380


10
00:03:39,300 --> 00:03:42,320

bat:
1
00:00:00,150 --> 00:01:00,460


2
00:01:32,308 --> 00:01:33,860


3
00:01:36,346 --> 00:01:38,999


4
00:01:52,128 --> 00:01:53,680


5
00:02:03,873 --> 00:08:17,597


6
00:08:20,449 --> 00:08:28,241


7
00:08:42,104 --> 00:09:44,584


8
00:09:51,107 --> 00:09:52,659


9
00:10:19,001 --> 00:13:00,580


10
00:13:55,184 --> 00:17:09,428

TOP

  1. @if(0)==(0) echo off
  2. dir /b *.srt | findstr /i /v /e "\-副本.srt" | cscript //nologo //e:jscript "%~f0"
  3. pause & exit
  4. @end
  5. var fso = new ActiveXObject('Scripting.FileSystemObject');
  6. while (!WSH.StdIn.AtEndOfStream){
  7.     var srcFile = WSH.StdIn.ReadLine();
  8.     var objFile = fso.OpenTextFile(srcFile, 1);
  9.     var out = [];
  10.     var i = 0;
  11.     var eTime = '00:00:00,000';
  12.     while (!objFile.AtEndOfStream){
  13.         var s = objFile.ReadLine();
  14.         var m = s.match(/^(\d\d:\d\d:\d\d,\d\d\d) --> (\d\d:\d\d:\d\d,\d\d\d)$/);
  15.         if (!m) continue;
  16.         var t1 = getTotalMillisecond(eTime, 150);
  17.         var t2 = getTotalMillisecond(m[1], -500);
  18.         if ( t2 - t1 > 1000 ) {
  19.             var s1 = formatTime(t1);
  20.             var s2 = formatTime(t2);
  21.             out.push( ++i + '\r\n' + s1 + ' --> ' + s2);
  22.         }
  23.         eTime = m[2];
  24.     }
  25.     var dstFile = srcFile.replace(/\.srt$/i, '-副本$&');
  26.     fso.OpenTextFile(dstFile, 2, true).WriteLine( out.join('\r\n\r\n') );
  27. }
  28. function getTotalMillisecond(t, x){
  29.     var arr = t.split(/[:,]/);
  30.     return ( 3600 * arr[0] + 60 * arr[1] + 1 * arr[2] ) * 1000 + 1 * arr[3] + x;
  31. }
  32.    
  33. function formatTime(t){
  34.     var ms =  10 * ( t % 1000 / 10 ).toFixed() + 1000;
  35.     t = Math.floor( t / 1000 );
  36.     var HH = Math.floor( t / 3600 ) + 100;
  37.     var mm = Math.floor( t % 3600 / 60 ) + 100;
  38.     var ss =  t % 60 + 100;
  39.     HH = ( '' + HH ).substr(1);
  40.     mm = ( '' + mm ).substr(1);
  41.     ss = ( '' + ss ).substr(1);
  42.     ms = ( '' + ms ).substr(1);
  43.     return HH + ':' + mm + ':' + ss + ',' + ms;
  44. }
复制代码

TOP

回复 6# WHY


    谢谢大神,完美解决!

TOP

非常感谢两位帮忙!

TOP

返回列表