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

问题描述 投票:0回答:2

如何向前或向后修正/移动字幕时间?字幕时间格式如下:

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.
delphi shift subtitle
2个回答
8
投票

假设输入中每一行的格式始终为

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

编辑:

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


0
投票

如果您使用免费的字幕编辑器(例如 SubtitleEdit 或 Subtitle Workshop)打开字幕文件,您可以轻松地一次调整所有字幕的时间。

对于字幕编辑,您需要转到同步 > 调整所有时间(显示更早/更晚) > 调整您需要移动的时间量 > 选择是否要将其应用于所有行、仅选定的行或选定的行开始 > 选择“较早显示”或“稍后显示”> 完成!

© www.soinside.com 2019 - 2024. All rights reserved.