-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSrtFile.cs
More file actions
106 lines (85 loc) · 2.81 KB
/
Copy pathSrtFile.cs
File metadata and controls
106 lines (85 loc) · 2.81 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SrtMerge
{
public class SrtBlock
{
public int Index;
public TimeSpan Start;
public TimeSpan End;
public string Content;
public SrtBlock( List<string> text )
{
var toks = text[1].Split( "-->", StringSplitOptions.TrimEntries );
Index = int.Parse( text[0] );
Start = TimeSpan.Parse( toks[0].Replace( ",", "." ), CultureInfo.InvariantCulture );
End = TimeSpan.Parse( toks[1].Replace( ",", "." ), CultureInfo.InvariantCulture );
Content = string.Join( Environment.NewLine, text.Skip( 2 ) );
}
public SrtBlock()
{
}
public SrtBlock( int index, TimeSpan start, TimeSpan end, string content )
{
Index = index;
Start = start;
End = end;
Content = content;
}
public string GetBlock( bool flattenBlock )
{
string output = Index.ToString() + Environment.NewLine;
output += $"{Start.ToString( "hh\\:mm\\:ss\\,fff" )} --> {End.ToString( "hh\\:mm\\:ss\\,fff" )}" + Environment.NewLine;
if ( flattenBlock )
{
var cf = Content.Split( ["\r", "\n"], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries );
Content = string.Join( " ", cf );
}
output += Content + Environment.NewLine;
return output;
}
}
public class SrtFile
{
public List<SrtBlock> Blocks;
public SrtFile()
{
Blocks = new();
}
public SrtFile( List<string> data )
{
Blocks = new();
for ( int i = 0; i < data.Count; i++ )
{
var t = data[i];
if ( !string.IsNullOrWhiteSpace( t ) && int.TryParse( t, out var _ ) )
{
for ( int j = i + 1; j < data.Count; j++ )
{
var t2 = data[j];
if ( string.IsNullOrWhiteSpace( t2 ) )
{
Blocks.Add( new SrtBlock( data.Skip( i ).Take( j - i ).ToList() ) );
i = j;
break;
}
}
}
}
}
public string GetFile( bool flattenBlock )
{
StringWriter output = new();
foreach ( var b in Blocks )
{
output.Write( b.GetBlock( flattenBlock ) );
output.Write( Environment.NewLine );
}
return output.ToString();
}
}
}