本帖最后由 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
- }
- }
复制代码
|