Skip to content

Instantly share code, notes, and snippets.

@brianonn
Last active November 7, 2021 01:58
Show Gist options
  • Save brianonn/455bce106bd86c9587d223acfbbe9751 to your computer and use it in GitHub Desktop.
Save brianonn/455bce106bd86c9587d223acfbbe9751 to your computer and use it in GitHub Desktop.
Windows PowerShell script to Convert WebVTT to SRT
# convertWebVTT
#
# This is a very basic and incomplete WebVTT to SRT converter.
# It does not parse or understand the WebVTT elements NOTE, STYLE, REGION or any C-style comments in the WebVTT file.
#
# Save this file to your desktop as ConvertWebVTT.ps1
#
# Then create a shortcut for powershell to execute this file (change your username)
# C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -noexit -ExecutionPolicy ByPass -file C:\Users\brian\Desktop\ConvertWebVTT.ps1
# you can drag and drop a .vtt file onto the shortcut and it will convert the file.
#
# Brian A. Onn
param ( [parameter(Mandatory=$true,ValueFromRemainingArguments=$true)]
[string[]] $Paths )
ForEach ($Path in $Paths) {
$Outfile = $Path -replace '(\.vtt|\.txt)','.srt'
'' > $Outfile
Get-Content $Path |
ForEach-Object {
$_ = $_ -replace '^WEBVTT.*$', ''
if ($_ -match ':\d\d\.\d{1,3}.*-->') {
$_ = $_ -replace '(^|\s)(\d\d):(\d\d)\.(\d{1,3})', '${1}00:$2:$3,$4'
$_ = $_ -replace ':(\d\d)\.(\d{1,3})', ':$1,$2'
}
$_ >> $Outfile
}
"Converted $Path to $Outfile"
""
}
@joegasper
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment