-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubtitleHelper.cs
29 lines (24 loc) · 1.17 KB
/
SubtitleHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public static class SubtitleHelper
{
/// <summary>
/// Convert WebVTT subtitles to Srt subtitles
/// </summary>
/// <param name="webvttContent">WebVTT string</param>
/// <returns>SRT result</returns>
public static String ConvertWebvttToSrt(String webvttContent)
{
if (webvttContent == null)
throw new ArgumentNullException("webvttContent");
String srtResult = webvttContent;
Int32 srtPartLineNumber = 0;
srtResult = Regex.Replace(srtResult, @"(WEBVTT\s+)(\d{2}:)", "$2"); // Removes 'WEBVTT' word
srtResult = Regex.Replace(srtResult, @"(\d{2}:\d{2}:\d{2})\.(\d{3}\s+)-->(\s+\d{2}:\d{2}:\d{2})\.(\d{3}\s*)", match =>
{
srtPartLineNumber++;
return srtPartLineNumber.ToString() + Environment.NewLine +
Regex.Replace(match.Value, @"(\d{2}:\d{2}:\d{2})\.(\d{3}\s+)-->(\s+\d{2}:\d{2}:\d{2})\.(\d{3}\s*)", "$1,$2-->$3,$4");
// Writes '00:00:19.620' instead of '00:00:19,620'
}); // Writes Srt section numbers for each section
return srtResult;
}
}