如何在SRT(SubRip)文件中纠正/移动字幕时间?

时间:2012-10-12 17:15:34

标签: delphi shift subtitle

如何正确地向前和向后移动字幕时间?字幕时间格式如下所示: 00:00:52,656 --> 00:00:56,326

如果字幕和音频不同步,例如,字幕在语音/音频之前显示,那么字幕行的所有时间(时间格式:00:00:52,656 --> 00:00:56,326)都应该被纠正。

因此,如果所有字幕行的时间必须改变/移位2秒。然后,这次为字幕行:00:00:52,656 --> 00:00:56,326应更改为: 00:00:54,656 --> 00:00:58,326

这指的是字幕文件中的所有时间,而不仅仅是一行文字/一次。


SubRip(.srt)文件的示例:

1
00:00:52,656 --> 00:00:56,326
Kanalska Zona: Panama

2
00:00:56,335 --> 00:00:59,755
Francuzi su pokušali da izgrade
kanal pre Amerikanaca.

1 个答案:

答案 0 :(得分:7)

假设输入中每行的格式始终为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;

enter image description here

修改

由于您的编辑,现在输入可能包含不需要转换的“错误”行。