首页 > 其他分享 >ChatGPT: delphi ass字幕转srt字幕

ChatGPT: delphi ass字幕转srt字幕

时间:2023-02-10 15:23:31浏览次数:42  
标签:ass Text 字幕 srt SL Line Copy StrToIntDef Subtitles

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

标签:ass,Text,字幕,srt,SL,Line,Copy,StrToIntDef,Subtitles
From: https://www.cnblogs.com/onlyou13/p/17109033.html

相关文章