[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$InputPath, [Parameter(Mandatory = $true)] [ValidateRange(-86400.0, 86400.0)] [double]$Seconds, [string]$OutputPath ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $InputPath -PathType Leaf)) { throw "Файл субтитров не найден: $InputPath" } $source = (Resolve-Path -LiteralPath $InputPath).Path if (-not $OutputPath) { $directory = Split-Path -Parent $source $name = [IO.Path]::GetFileNameWithoutExtension($source) $OutputPath = Join-Path $directory "${name}_shifted.srt" } $target = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputPath) if ($target -eq $source) { throw 'Результат не должен совпадать с исходным файлом.' } function Format-SrtTime { param([int64]$Milliseconds) if ($Milliseconds -lt 0) { $Milliseconds = 0 } $hours = [int64][math]::Floor($Milliseconds / 3600000) $minutes = [int64][math]::Floor(($Milliseconds % 3600000) / 60000) $secondsPart = [int64][math]::Floor(($Milliseconds % 60000) / 1000) $millis = [int64]($Milliseconds % 1000) return ('{0:00}:{1:00}:{2:00},{3:000}' -f $hours, $minutes, $secondsPart, $millis) } $pattern = '^(\d{2}):(\d{2}):(\d{2}),(\d{3}) --> (\d{2}):(\d{2}):(\d{2}),(\d{3})(.*)$' $offsetMs = [int64][math]::Round($Seconds * 1000) $lines = Get-Content -LiteralPath $source -Encoding UTF8 $output = New-Object System.Collections.Generic.List[string] $shifted = 0 $clamped = 0 foreach ($line in $lines) { if ($line -match $pattern) { $startMs = (([int64]$matches[1] * 3600 + [int64]$matches[2] * 60 + [int64]$matches[3]) * 1000) + [int64]$matches[4] $endMs = (([int64]$matches[5] * 3600 + [int64]$matches[6] * 60 + [int64]$matches[7]) * 1000) + [int64]$matches[8] $newStart = $startMs + $offsetMs $newEnd = $endMs + $offsetMs if ($newStart -lt 0) { $newStart = 0 $clamped++ } if ($newEnd -lt 0) { $newEnd = 0 $clamped++ } $line = '{0} --> {1}{2}' -f (Format-SrtTime $newStart), (Format-SrtTime $newEnd), $matches[9] $shifted++ } $output.Add($line) } $directory = Split-Path -Parent $target if (-not (Test-Path -LiteralPath $directory)) { New-Item -ItemType Directory -Path $directory -Force | Out-Null } $encoding = [System.Text.UTF8Encoding]::new($false) [System.IO.File]::WriteAllLines($target, $output, $encoding) Write-Host "Сдвинуто строк: $shifted, обрезано по нулю: $clamped" Write-Host "Готово: $target"