标题: [文本处理] [已解決]求助bat批处理更改时间 [打印本页]
作者: tianzi 时间: 2021-11-18 21:14 标题: [已解決]求助bat批处理更改时间
本帖最后由 tianzi 于 2022-5-16 16:11 编辑
有一批如下的格式的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 01:21
本帖最后由 flashercs 于 2021-11-19 05:45 编辑
- <#*,:&cls
- @echo off
- pushd "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- popd
- exit /b
- #>
-
- function GenSrt {
- param(
- [string]$Source,
- [string]$Output,
- [timespan]$EndTimeDiff = ([timespan]::FromMilliseconds(150)),
- [timespan]$StartTimeDiff = ([timespan]::FromMilliseconds(-500)),
- [timespan]$MinDiff = ([timespan]::FromMilliseconds(1000))
- )
- $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)
- # $re = [regex]"(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})"
- $format = "hh\:mm\:ss\,fff"
- $timelineformat = "{0:$format} --> {1:$format}"
- Get-Content -Path $Source -Encoding utf8 | ForEach-Object -Begin {
- $lineCtr = 0
- $section = @([timespan]::Zero.Add($EndTimeDiff), $null)
- $sectionIndex = 1
- } -Process {
- if ($lineCtr -eq 1) {
- $match = $re.Match($_)
- if ($match.Success) {
- $section[1] = [timespan]::ParseExact($match.Groups[1].Value, $format, $null).Add($StartTimeDiff)
- if ($section[1].Subtract($section[0]).Duration() -ge $MinDiff ) {
- $sectionIndex
- $timelineformat -f $section
- ""
- ""
- $sectionIndex++
- }
- # $timelineformat -f $section | Write-Host -ForegroundColor Green
- $section[0] = [timespan]::ParseExact($match.Groups[2].Value, $format, $null).Add($EndTimeDiff)
- }
- }
- if ($lineCtr -ge 3) {
- $lineCtr = 0
- } else {
- $lineCtr++
- }
- } | Set-Content -Path $Output -Encoding utf8
- }
-
- # [System.IO.Directory]::CreateDirectory($dstdir) | Out-Null
- Get-ChildItem -Path "*.srt" -Filter "*.srt" -Exclude "*-副本.srt" | ForEach-Object {
- if (-not $_.PSIsContainer) {
- $dstfile = $_.BaseName + "-副本.srt"
- Write-Host "$($_.Name) --> $dstfile"
- GenSrt -Source $_ -Output $dstfile
- }
- }
复制代码
作者: tianzi 时间: 2021-11-19 03:23
本帖最后由 tianzi 于 2021-11-25 22:28 编辑
回复 2# flashercs
谢谢,可以生成副本srt,只是生成的数量少了
另外,可以直接在字幕目录双击bat完成,直接在源字幕文件夹生存,不用单独再生成文件夹吗,运行完毕后自动关闭吗?
作者: flashercs 时间: 2021-11-19 05:47
回复 3# tianzi
已修改了.
作者: tianzi 时间: 2021-11-19 13:14
本帖最后由 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
作者: WHY 时间: 2021-11-19 22:41
- @if(0)==(0) echo off
- dir /b *.srt | findstr /i /v /e "\-副本.srt" | cscript //nologo //e:jscript "%~f0"
- pause & exit
- @end
-
- var fso = new ActiveXObject('Scripting.FileSystemObject');
-
- while (!WSH.StdIn.AtEndOfStream){
- var srcFile = WSH.StdIn.ReadLine();
- var objFile = fso.OpenTextFile(srcFile, 1);
-
- var out = [];
- var i = 0;
- var eTime = '00:00:00,000';
-
- while (!objFile.AtEndOfStream){
- var s = objFile.ReadLine();
- var m = s.match(/^(\d\d:\d\d:\d\d,\d\d\d) --> (\d\d:\d\d:\d\d,\d\d\d)$/);
- if (!m) continue;
- var t1 = getTotalMillisecond(eTime, 150);
- var t2 = getTotalMillisecond(m[1], -500);
- if ( t2 - t1 > 1000 ) {
- var s1 = formatTime(t1);
- var s2 = formatTime(t2);
- out.push( ++i + '\r\n' + s1 + ' --> ' + s2);
- }
- eTime = m[2];
- }
-
- var dstFile = srcFile.replace(/\.srt$/i, '-副本$&');
- fso.OpenTextFile(dstFile, 2, true).WriteLine( out.join('\r\n\r\n') );
- }
-
- function getTotalMillisecond(t, x){
- var arr = t.split(/[:,]/);
- return ( 3600 * arr[0] + 60 * arr[1] + 1 * arr[2] ) * 1000 + 1 * arr[3] + x;
- }
-
- function formatTime(t){
- var ms = 10 * ( t % 1000 / 10 ).toFixed() + 1000;
- t = Math.floor( t / 1000 );
- var HH = Math.floor( t / 3600 ) + 100;
- var mm = Math.floor( t % 3600 / 60 ) + 100;
- var ss = t % 60 + 100;
- HH = ( '' + HH ).substr(1);
- mm = ( '' + mm ).substr(1);
- ss = ( '' + ss ).substr(1);
- ms = ( '' + ms ).substr(1);
- return HH + ':' + mm + ':' + ss + ',' + ms;
- }
复制代码
作者: tianzi 时间: 2021-11-19 23:04
回复 6# WHY
谢谢大神,完美解决!
作者: tianzi 时间: 2021-11-19 23:06
非常感谢两位帮忙!
作者: tianzi 时间: 2022-4-25 18:20
本帖最后由 tianzi 于 2022-4-27 08:44 编辑
回复 6# WHY
大佬,现在有新的需求,需要延长最后1行时间3分钟
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |