-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathugenericsubtitlefile.pas
306 lines (268 loc) · 8.73 KB
/
ugenericsubtitlefile.pas
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
unit uGenericSubtitleFile;
{ base generic class for a text based subtitle file. has some abstract methods
for descendant classes. look at uSubripFile for example.
Copyright (C) 2017 Mohammadreza Bahrami [email protected]
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
{$mode objfpc}{$H+}{$modeswitch advancedrecords}
interface
uses
Classes, SysUtils, uMinimalList, uTimeSlice;
type
{ TPlainSubtitleEvent }
TPlainSubtitleEvent = record
TimeSlice: TTimeSlice; // <-- any subtitle event type should contain this.
Text: String;
end;
{ TGenericSubtitleFile }
generic TGenericSubtitleFile<T> = class
private
type TGenericSubtitleEvents = specialize TArray<T>;
type TCustomGenericSubtitleEventList = specialize TMinimalList<T>;
type TGenericSubtitleEventList = class(TCustomGenericSubtitleEventList)
private
procedure SetItem(AIndex: Integer; AItem: T); override;
end;
strict private
FEvents: TGenericSubtitleEventList;
FTimeSlice: TTimeSlice;
private
function EventsInRange(ARange: TTimeSlice): TGenericSubtitleEvents;
public
procedure Clear;
procedure Cleanup; virtual;
procedure LoadFromFile(const AFileName: String); overload;
procedure LoadFromFile(const AFileName: String; AEncoding: TEncoding); overload;
procedure LoadFromString(const AContents: String); virtual; abstract;
procedure SaveToFile(const AFileName: String); overload;
procedure SaveToFile(const AFileName: String; AEncoding: TEncoding); overload;
procedure SaveToString(out AContents: String); virtual; abstract;
function MakeNewFromRanges(ARanges: TTimeSliceList; AFinalStartOffset
: Double = 0): TGenericSubtitleEvents;
procedure FixOverlapsForward;
procedure FixOverlapsBackward;
public
property Events: TGenericSubtitleEventList read FEvents write FEvents;
property TimeSlice: TTimeSlice read FTimeSlice write FTimeSlice;
constructor Create; virtual;
destructor Destroy; override;
end;
implementation
{ TGenericSubtitleFile.TGenericSubtitleEventList }
procedure TGenericSubtitleFile.TGenericSubtitleEventList.SetItem(
AIndex: Integer; AItem: T);
begin
inherited SetItem(AIndex, AItem);
if Assigned(Owner) then
with (Owner as TGenericSubtitleFile) do
begin
FTimeSlice.Value.StartPos.Value := AItem.TimeSlice.Value.StartPos.Value;
FTimeSlice.Value.EndPos.Value := AItem.TimeSlice.Value.EndPos.Value;
PItems[AIndex]^.TimeSlice := FTimeSlice;
end;
end;
{ TGenericSubtitleFile }
function TGenericSubtitleFile.EventsInRange(ARange: TTimeSlice
): TGenericSubtitleEvents;
var
i,j: Integer;
begin
SetLength(Result, FEvents.Count);
j := 0;
for i := 0 to FEvents.Count-1 do
begin
FEvents.PItems[i]^.TimeSlice.Delay := ARange.Delay;
if ((FEvents[i].TimeSlice.ValueWithDelay.StartPos.ValueAsDouble >= ARange.Value.StartPos.ValueAsDouble)
and (FEvents[i].TimeSlice.ValueWithDelay.StartPos.ValueAsDouble < ARange.Value.EndPos.ValueAsDouble))
or ((FEvents[i].TimeSlice.ValueWithDelay.StartPos.ValueAsDouble < ARange.Value.StartPos.ValueAsDouble)
and (FEvents[i].TimeSlice.ValueWithDelay.EndPos.ValueAsDouble > ARange.Value.StartPos.ValueAsDouble)) then
begin
FEvents.PItems[i]^.TimeSlice.Delay := 0;
Result[j] := FEvents[i];
Inc(j);
end;
end;
SetLength(Result, j);
for i := 0 to j-1 do
begin
if Result[i].TimeSlice.Value.StartPos.ValueAsDouble < ARange.Value.StartPos.ValueAsDouble then
Result[i].TimeSlice.Value.StartPos.Value := ARange.Value.StartPos.Value
else if Result[i].TimeSlice.Value.EndPos.ValueAsDouble > ARange.Value.EndPos.ValueAsDouble then
Result[i].TimeSlice.Value.EndPos.Value := ARange.Value.EndPos.Value;
end;
end;
procedure TGenericSubtitleFile.Clear;
begin
FEvents.Clear;
end;
procedure TGenericSubtitleFile.Cleanup;
var
i: Integer;
begin
for i := FEvents.Count-1 downto 0 do
if not FEvents[i].TimeSlice.Valid then
FEvents.Remove(i);
end;
procedure TGenericSubtitleFile.LoadFromFile(const AFileName: String);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.LoadFromFile(AFileName);
LoadFromString(sl.Text);
finally
sl.Free;
end;
end;
procedure TGenericSubtitleFile.LoadFromFile(const AFileName: String;
AEncoding: TEncoding);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.LoadFromFile(AFileName, AEncoding);
LoadFromString(sl.Text);
finally
sl.Free;
end;
end;
procedure TGenericSubtitleFile.SaveToFile(const AFileName: String);
var
sl: TStringList;
s: String;
begin
SaveToString(s);
sl := TStringList.Create;
try
sl.Text := s;
sl.SaveToFile(AFileName);
finally
sl.Free;
end;
end;
procedure TGenericSubtitleFile.SaveToFile(const AFileName: String;
AEncoding: TEncoding);
var
sl: TStringList;
s: String;
begin
SaveToString(s);
sl := TStringList.Create;
try
sl.DefaultEncoding := AEncoding;
sl.Text := s;
sl.SaveToFile(AFileName, AEncoding);
finally
sl.Free;
end;
end;
function TGenericSubtitleFile.MakeNewFromRanges(ARanges: TTimeSliceList;
AFinalStartOffset: Double): TGenericSubtitleEvents;
var
dlgs, dlgsr: TGenericSubtitleEvents;
ts: TTimeSlice;
i,j,k: Integer;
Offset: Double;
begin
Result := nil;
if not ARanges.Valid then Exit;
if Events.Count < 1 then Exit;
ARanges.Initialize(FTimeSlice.TimeSliceFormat);
SetLength(dlgs, Events.Count);
k := 0;
Offset := 0;
for i := 0 to ARanges.Count-1 do
begin
dlgsr := EventsInRange(ARanges.Values[i]);
if Length(dlgsr) < 1 then Continue;
ts.Reset;
if i > 0 then
ts.Value.StartPos.Value := ARanges.Values[i-1].Value.EndPos.Value;
ts.Value.EndPos.Value := ARanges.Values[i].Value.StartPos.Value;
Offset := Offset +ts.Duration.ValueAsDouble;
for j := 0 to High(dlgsr) do
begin
dlgs[k] := dlgsr[j];
dlgs[k].TimeSlice.Delay := -Offset +ARanges.Values[i].Delay;
Inc(k);
end;
end;
SetLength(dlgs, k);
if AFinalStartOffset > 0 then
for i := 0 to k-1 do
dlgs[i].TimeSlice.Delay := dlgs[i].TimeSlice.Delay +AFinalStartOffset;
for i := 0 to k-1 do
begin
dlgs[i].TimeSlice.Value := dlgs[i].TimeSlice.ValueWithDelay;
dlgs[i].TimeSlice.Delay := 0;
end;
Result := dlgs;
end;
procedure TGenericSubtitleFile.FixOverlapsForward;
var
i: Integer;
begin
i := -1;
while i < FEvents.Count-2 do
begin
Inc(i);
if FEvents[i+1].TimeSlice.Value.StartPos.ValueAsDouble
< FEvents[i].TimeSlice.Value.EndPos.ValueAsDouble then
begin
FEvents.PItems[i+1]^.TimeSlice.Value.StartPos.ValueAsDouble :=
FEvents[i].TimeSlice.Value.EndPos.ValueAsDouble+0.001;
Cleanup;
i := -1;
end;
end;
end;
procedure TGenericSubtitleFile.FixOverlapsBackward;
var
i: Integer;
begin
i := FEvents.Count;
while i > 1 do
begin
Dec(i);
if FEvents[i].TimeSlice.Value.StartPos.ValueAsDouble
< FEvents[i-1].TimeSlice.Value.EndPos.ValueAsDouble then
begin
FEvents.PItems[i-1]^.TimeSlice.Value.EndPos.ValueAsDouble :=
FEvents[i].TimeSlice.Value.StartPos.ValueAsDouble-0.001;
Cleanup;
i := FEvents.Count;
end;
end;
end;
constructor TGenericSubtitleFile.Create;
begin
FEvents := TGenericSubtitleEventList.Create(Self);
end;
destructor TGenericSubtitleFile.Destroy;
begin
if Assigned(FEvents) then FEvents.Free;
inherited Destroy;
end;
end.