onlyou13

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
uses
  SysUtils, Classes, DateUtils;

const
  LineBreak = #13#10;

type
  TSubtitle = record
    Start, Stop: TDateTime;
    Text: string;
  end;

function ParseASS(const FileName: string; var Subtitles: TArray<TSubtitle>): Boolean;
var
  SL: TStringList;
  I, P: Integer;
  Start, Stop: TDateTime;
  Line, Text: string;
begin
  Result := False;
  Subtitles := nil;
  SL := TStringList.Create;
  try
    SL.LoadFromFile(FileName, TEncoding.UTF8);
    I := 0;
    while (I < SL.Count) and (SL[I] <> '[Events]') do
      Inc(I);
    Inc(I);
    while I < SL.Count do
    begin
      Line := SL[I];
      if Line = '' then
        Break;
      if Line[1] = 'D' then
      begin
        Inc(I);
        Text := '';
        while (I < SL.Count) and (SL[I] <> '') do
        begin
          Text := Text + SL[I];
          Inc(I);
        end;
        P := Pos(',', Line);
        Start := EncodeTime(StrToIntDef(Copy(Line, P + 1, 2), 0),
          StrToIntDef(Copy(Line, P + 4, 2), 0),
          StrToIntDef(Copy(Line, P + 7, 2), 0),
          StrToIntDef(Copy(Line, P + 10, 3), 0));
        P := Pos(',', Line, P + 1);
        Stop := EncodeTime(StrToIntDef(Copy(Line, P + 1, 2), 0),
          StrToIntDef(Copy(Line, P + 4, 2), 0),
          StrToIntDef(Copy(Line, P + 7, 2), 0),
          StrToIntDef(Copy(Line, P + 10, 3), 0));
        SetLength(Subtitles, Length(Subtitles) + 1);
        Subtitles[Length(Subtitles) - 1].Start := Start;
        Subtitles[Length(Subtitles) - 1].Stop := Stop;
        Subtitles[Length(Subtitles) - 1].Text := Text;
      end;
      Inc(I);
    end;
    Result := True;
  finally
    SL.Free;
  end;
end;

procedure SaveSRT(const FileName: string; const Subtitles: TArray<TSubtitle>);
var
  SL: TStringList;
  I: Integer;
begin
  SL := TStringList.Create;
  try
    for I := 0 to Length(Subtitles) - 1 do
    begin
      SL.Add(IntToStr(I + 1));
      SL

//它没有写完,不过看样子还是解析出来了ass

posted on 2023-02-10 15:13  onlyou13  阅读(111)  评论(0编辑  收藏  举报