Last active
November 7, 2021 01:58
-
-
Save brianonn/455bce106bd86c9587d223acfbbe9751 to your computer and use it in GitHub Desktop.
Windows PowerShell script to Convert WebVTT to SRT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" | |
"" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the inspiration.
https://gist.github.com/joegasper/e862f71b5a2658fae21fd36f7231b33c