Skip to content

Instantly share code, notes, and snippets.

@go2tom42
Last active November 10, 2024 16:49
Show Gist options
  • Save go2tom42/53b7c43f28860f054bba017b00aeb438 to your computer and use it in GitHub Desktop.
Save go2tom42/53b7c43f28860f054bba017b00aeb438 to your computer and use it in GitHub Desktop.
Change framerate for SRT subtitle files
function Sync-SRT {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[double]$SourceFPS,
[Parameter(Mandatory=$false)]
[double]$TargetFPS,
[Parameter(Mandatory=$false)]
[double]$milliseconds,
[Parameter(Mandatory=$true)]
$file
)
$file = get-item -literalpath $file
$content = Get-Content -literalpath $file
# Check which process is required
if ($SourceFPS -and $TargetFPS) {
# Process 1: $a and $b are required
if (-not $SourceFPS -or -not $TargetFPS) {
Write-Error "Parameters `$SourceFPS and `$TargetFPS are required for this process."
return
}
for($i=0; $i -le $content.Length; ++$i)
{
if($content[$i] | Select-String -Pattern "^\d+:")
{
$timestart = $content[$i].Split(" --> ")[0]
$timeend = $content[$i].Split(" --> ")[1]
$timestart_timespan = ([datetime] $timestart).TimeOfDay
$timeend_timespan = ([datetime] $timeend).TimeOfDay
$diff = $SourceFPS / $TargetFPS
$new_timestart = "{0:hh\:mm\:ss\.fff}" -f ($timestart_timespan * $diff)
$new_timeend = "{0:hh\:mm\:ss\.fff}" -f ($timeend_timespan * $diff)
$timestart = $new_timestart
$timeend = $new_timeend
$content[$i] = $timestart.ToString() + " --> " + $timeend.ToString()
$content[$i] = $content[$i] -replace "\.",","
}
}
$content | Set-Content (join-path $($file.DirectoryName) "$($file.basename).FIXED$($file.extension)")
} elseif ($milliseconds) {
# Process 2: Either $c or $d is required
if (-not $milliseconds) {
Write-Error "`$milliseconds is required for this process."
return
}
for($i=0; $i -le $content.Length; ++$i)
{
if($content[$i] | Select-String -Pattern "^\d+:")
{
$timestart = $content[$i].Split(" --> ")[0]
$timeend = $content[$i].Split(" --> ")[1]
$timestart_timespan = ([datetime] $timestart).TimeOfDay
$timeend_timespan = ([datetime] $timeend).TimeOfDay
$new_timestart = "{0:hh\:mm\:ss\.fff}" -f ($timestart_timespan.add([System.TimeSpan]::FromMilliseconds($milliseconds)))
$new_timeend = "{0:hh\:mm\:ss\.fff}" -f ($timeend_timespan.add([System.TimeSpan]::FromMilliseconds($milliseconds)))
$timestart = $new_timestart
$timeend = $new_timeend
$content[$i] = $timestart.ToString() + " --> " + $timeend.ToString()
$content[$i] = $content[$i] -replace "\.",","
}
}
$content | Set-Content (join-path $($file.DirectoryName) "$($file.basename).FIXED$($file.extension)")
} else {
Write-Error "You must specify parameters for either Process 1 (`$SourceFPS and `$TargetFPS) or Process 2 (`$milliseconds)."
}
}
$new_timestart = "{0:hh\:mm\:ss\.fff}" -f ($timestart_timespan * $diff)
$new_timeend = "{0:hh\:mm\:ss\.fff}" -f ($timeend_timespan * $diff)
$timestart = $new_timestart
$timeend = $new_timeend
$content[$i] = $timestart.ToString() + " --> " + $timeend.ToString()
$content[$i] = $content[$i] -replace "\.",","
}
}
$content | Set-Content (join-path $($file.DirectoryName) "$($file.basename).FIXED$($file.extension)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment