假设输入中每一行的格式始终为00:00:00,000 --> 00:00:00,000
,则此例程将字符串时间转换为TDateTime
,添加或减去移位,并重写该行:
procedure ShiftSubtitleTimes(Lines: TStrings; Diff: TTime);
var
FS: TFormatSettings;
I: Integer;
T1: TDateTime;
T2: TDateTime;
begin
// Ensure using the correct time separator
FS.TimeSeparator := ':';
// Parse each line separately
for I := 0 to Lines.Count - 1 do
begin
// Convert the two time strings to time values
if not TryStrToTime(Copy(Lines[I], 1, 8), T1, FS) then
// But skip line in case of wrong format
Continue;
T1 := T1 + StrToInt(Copy(Lines[I], 10, 3)) / MSecsPerDay;
T2 := StrToTime(Copy(Lines[I], 18, 8), FS);
T2 := T2 + StrToInt(Copy(Lines[I], 27, 3)) / MSecsPerDay;
// Add the shift
T1 := T1 + Diff;
T2 := T2 + Diff;
// Rewrite the line
Lines[I] := FormatDateTime('hh:nn:ss,zzz --> ', T1, FS) +
FormatDateTime('hh:nn:ss,zzz', T2, FS);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
List: TStringList;
begin
List := TStringList.Create;
try
List.LoadFromFile('Filename.dat');
Memo1.Lines.Add('Input:');
Memo1.Lines.AddStrings(List);
Memo1.Lines.Add('');
// Shift 3,5 seconds backwards:
ShiftSubtitleTimes(List, -3.5 / SecsPerDay);
Memo1.Lines.Add('Output:');
Memo1.Lines.AddStrings(List);
finally
List.Free;
end;
end;
编辑:
由于您的编辑,现在输入可能包含不需要转换的“错误”行。