unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, VfW, Shellapi, IniFiles, jpeg, FileCtrl, ComCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    GroupBox2: TGroupBox;
    Edit1: TEdit;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    OpenDialog2: TOpenDialog;
    GroupBox3: TGroupBox;
    Edit2: TEdit;
    Button4: TButton;
    Memo2: TMemo;
    Memo3: TMemo;
    GroupBox4: TGroupBox;
    Panel1: TPanel;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    CheckBox1: TCheckBox;
    Edit7: TEdit;
    Edit8: TEdit;
    Label1: TLabel;
    GroupBox5: TGroupBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    Edit9: TEdit;
    Edit10: TEdit;
    Bevel1: TBevel;
    VDubPath: TEdit;
    GroupBox7: TGroupBox;
    Memo5: TMemo;
    Button6: TButton;
    Button7: TButton;
    Bevel3: TBevel;
    Button8: TButton;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    Bevel4: TBevel;
    GroupBox8: TGroupBox;
    Memo4: TMemo;
    Button5: TButton;
    Button9: TButton;
    Bevel2: TBevel;
    Button10: TButton;
    GroupBox9: TGroupBox;
    Edit11: TEdit;
    Label2: TLabel;
    ComboBox2: TComboBox;
    OpenDialog3: TOpenDialog;
    OpenDialog4: TOpenDialog;
    OpenDialog5: TOpenDialog;
    BS_Profiles: TMemo;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    ComboBox4: TComboBox;
    Bevel6: TBevel;
    Bevel7: TBevel;
    ComboBox1: TComboBox;
    function  ExecuteAndWait(const aCmdLine: String; const Pfad : String;Hidden, doWait: Boolean): Boolean;
    procedure Clear(Sender: TObject);
    procedure AVIBrowser(Sender: TObject);
    procedure VCFBrowse(Sender: TObject);
    procedure SaveAVS(Sender: TObject);
    procedure Init(Sender: TObject);
    procedure ProcessAudio(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure SaveIni(Sender: TObject; var Action: TCloseAction);
    procedure Button9Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure RenameAVS(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure GenerateAVS(MPEGFile,AVSFile : String);
    procedure AddToSource(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1     : TForm1;
  VidFolder,
  TargetDir,
  AVISplitFolder : String;

implementation

{$R *.DFM}
//------------------------------------------------------------------------------
Function GetDelay (Filename : String) : String;
Var Delay : String;
Begin
  Delay := '0';
  If (StrPos(PChar(Filename),PChar('DELAY'))<>NIL) Then
  Begin
    Delay := StrPos(PChar(Filename),PChar('DELAY'));
    Delay := StringReplace(Delay,'DELAY ','',[rfReplaceAll]);
    Delay := StringReplace(Delay,'ms.mpa','',[rfReplaceAll]);
  End;
  Result := Delay
End;
//------------------------------------------------------------------------------
Function GetMPAFileName (Dir : String) : String;
Var sr : TSearchRec;
    FileAttrs : Integer;
Begin
  Result := '';
  FileAttrs := FileAttrs + faAnyFile;
  if FindFirst(Dir+'*.mpa', FileAttrs, sr) = 0 then
  begin
  If (StrPos(PChar(sr.Name),PChar('mpa'))<>NIL) Then Result := ExtractFileDir(Dir)+'\'+sr.Name;
    repeat
      if (sr.Attr and FileAttrs) = sr.Attr then
      begin
        If (StrPos(PChar(sr.Name),PChar('mpa'))<>NIL) Then Result := ExtractFileDir(Dir)+'\'+sr.Name;
      end;
    until (FindNext(sr) <> 0);
    FindClose(sr);
  end;
  If (Result = '') Then Result := 'Not_Found';
End;
//------------------------------------------------------------------------------
Function SplitAVI (AVISplitFolder, SourceFile, SplitSize : String) : Boolean;
Begin
  Result := Form1.ExecuteAndWait(AVISplitFolder+'avisplit.exe "'+SourceFile+'" '+SplitSize+' -x -n -l', AVISplitFolder, False, True);
End;
//------------------------------------------------------------------------------
Function VDubConvertDir (Pfad : String) : String;
Var BPfad : String;
    sl,i  : Integer;
Begin
  If (Pfad = '') Then Result := '';
  BPfad := '';
  sl := StrLen(PChar(Pfad));
  For i := 1 To sl Do
  Begin
    BPfad := BPfad + Pfad[i];
    If (Pfad[i]='\') Then BPfad := BPfad + '\';
  End;
  Result := BPfad;
End;
//------------------------------------------------------------------------------
Procedure GenSaveWaveVCF (QuellDatei, ScriptDatei : String);
Var S         : Text;
    Command,
    WaveDatei : String;
    Error     : Integer;
Begin
  Error := 0;
  Try
    AssignFile(S,ScriptDatei);
    Rewrite(S);
  Except
    Error := 1;
  End;

  If ((Error = 0) And FileExists(QuellDatei))Then
  Begin
    WaveDatei  := ChangeFileExt(QuellDatei,'');
    QuellDatei := VDubConvertDir(QuellDatei);
    WaveDatei  := VDubConvertDir(WaveDatei);
    Command    := 'VirtualDub.Open("'+QuellDatei+'",0,0);';
    WriteLn(S, Command);
    WriteLn(S,'VirtualDub.audio.SetSource(1);');
    WriteLn(S,'VirtualDub.audio.SetMode(0);');
    WriteLn(S,'VirtualDub.audio.SetInterleave(1,500,1,0,0);');
    WriteLn(S,'VirtualDub.audio.SetClipMode(1,1);');
    WriteLn(S,'VirtualDub.audio.SetConversion(0,0,0,0,0);');
    Command    := 'VirtualDub.SaveWAV("'+WaveDatei+'.WAV");';
    WriteLn(S, Command);
  End;
  CloseFile(S);
End;
//------------------------------------------------------------------------------
Procedure ExecuteVCF (VDubPath , VCF : String);
Begin
  Form1.ExecuteAndWait(VDubPath+'virtualdub.exe /s'+VCF+' /x', VDubPath, False, True);
End;
//------------------------------------------------------------------------------
// Execute Funktionen von TheWef
// Danke nochmals ! :)
function TForm1.ExecuteAndWait(const aCmdLine: String; const Pfad : String;Hidden, doWait: Boolean): Boolean;
var
   StartupInfo  : TStartupInfo;
   ProcessInfo  : TProcessInformation;
   CreationFlag : DWORD;
begin
   {setup the startup information for the application }
    FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
   with StartupInfo do
    begin
     cb:= SizeOf(TStartupInfo);
     dwFlags:= STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
     if ( Hidden ) then
      begin
        wShowWindow:= SW_SHOWMINNOACTIVE;
      end
      else begin
        wShowWindow:= SW_SHOWNORMAL;
      end;
    end;

    CreationFlag:=NORMAL_PRIORITY_CLASS;

    Result := CreateProcess(nil,PChar(aCmdLine), nil, nil, False, CreationFlag, nil, PChar(Pfad), StartupInfo, ProcessInfo);
    if doWait then
      if Result then
      begin
        WaitForInputIdle(ProcessInfo.hProcess,INFINITE);
        WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
      end;
end;
//------------------------------------------------------------------------------
procedure TForm1.Clear(Sender: TObject);
begin
  Memo1.Lines.Clear;
end;
//------------------------------------------------------------------------------
procedure TForm1.AVIBrowser(Sender: TObject);
VAR I : Integer;
    Extension,AVS,
    AVS_File      : String;
begin
 if OpenDialog1.Execute then
 Begin
   Extension := StrUpper(PChar(ExtractFileExt(OpenDialog1.Files.Strings[0])));
   CheckBox7.Enabled := (Extension = '.AVS');

   If (Extension <> '.MPG') And (Extension <> '.VOB') And (Extension <> '.MPV') And (Extension <> '.M2V') Then
   Begin
     with OpenDialog1.Files do
     for I := 0 to Count - 1 do Memo1.Lines.Add(Strings[I]);
     Edit2.Text := ChangeFileExt(OpenDialog1.Files.Strings[0],'.AVS');
     VidFolder := ExtractFilePath(OpenDialog1.Files.Strings[0]);
   End
   Else
   Begin
     AVS_File := ChangeFileExt(OpenDialog1.Files.Strings[0],'.AVS');
     CheckBox7.Enabled := True;

     with OpenDialog1.Files do
     for I := 0 to Count - 1 do Memo1.Lines.Add(Strings[I]);
     If (Memo1.Lines.Count > 1) Then
     Begin
       Memo1.Lines.Text := StringReplace(Memo1.Lines.Text,#10+#13,',',[rfReplaceAll]);
       GenerateAVS (Memo1.Lines.Text,AVS_File);
     End
     Else
     Begin
       GenerateAVS (Memo1.Lines.Strings[0],AVS_File);
     End;
     Memo1.Lines.Clear;
     Memo1.Lines.Add(ChangeFileExt(OpenDialog1.Files.Strings[0],'.AVS'));
   End;
 End;
end;
//------------------------------------------------------------------------------
procedure TForm1.VCFBrowse(Sender: TObject);
Var param    : String;
    F        : TextFile;
    S        : String;
    s_width,
    s_height,
    Resultat : Integer;
begin
  if OpenDialog2.Execute then
  If FileExists(OpenDialog2.Filename) Then
  Begin
    Edit1.Text := OpenDialog2.Filename;
    Memo2.Lines.Clear;
    Memo3.Lines.Clear;
    If FileExists(Edit1.Text) Then
    Begin
      AssignFile(F, Edit1.Text);
      Reset(F);
      while not Eof(F) do
      Begin
        Readln(F, S);
        If (StrLComp(PChar(S), PChar('VirtualDub.subset.AddRange'),26)=0) Then
        Begin
          S := StringReplace(S,');','',[rfReplaceAll]);
          S := StringReplace(S,'VirtualDub.subset.AddRange(','',[rfReplaceAll]);
          S := StringReplace(S,',',':',[rfReplaceAll]);
          Memo3.Lines.Add(S);
          CheckBox3.Enabled := True;
        End;

        If (StrPos(PChar(S), PChar('SetClipping('))<>NIL) Then
        Begin
          S := StringReplace(S,');','',[rfReplaceAll]);
          S := StringReplace(S,'VirtualDub.video.filters.instance[0].SetClipping(','',[rfReplaceAll]);
          S := StringReplace(S,',',#13+#10,[rfReplaceAll]);
          Memo2.Lines.Add(S);

          // Write Cropping Values to Edit-Fields
          Edit5.Text := Memo2.Lines.Strings[0];
          Edit3.Text := Memo2.Lines.Strings[1];
          Edit6.Text := Memo2.Lines.Strings[2];
          Edit4.Text := Memo2.Lines.Strings[3];

          CheckBox1.Enabled := True;

          GetAVIFileRes (Memo1.Lines.Strings[0],s_height,s_width,Resultat);

          Edit7.Text := IntToStr(s_width-StrToInt(Edit5.Text)-StrToInt(Edit6.Text));
          Edit8.Text := IntToStr(s_height-StrToInt(Edit3.Text)-StrToInt(Edit4.Text));
          If ((StrToInt(Edit7.Text) MOD 4) > 0) Then
          Begin
            Edit5.Text := IntToStr(StrToInt(Edit5.Text)+(StrToInt(Edit7.Text) MOD 4));
            Edit7.Text := IntToStr(s_width-StrToInt(Edit5.Text)-StrToInt(Edit6.Text));
          End;
         End;
      End;
      CloseFile(F);
    End;
  End;
end;
//------------------------------------------------------------------------------
procedure TForm1.SaveAVS(Sender: TObject);
Var param     : String;
    F         : TextFile;
    S         : String;
    I         : Integer;
    Start,
    Duration  : String;
    Frames    : Integer;
    FluxSmooth : Array[1..3] Of String;
begin
  // Abbrechen, wenn keine Quelldatei ausgewählt
  If Not FileExists(Memo1.Lines.Strings[0]) Then Exit;

  // Memobox für AVISynth Script löschen
  Memo2.Lines.Clear;

  // Wenn gewünscht, dann Settings ans Quell AVISynthscript anhängen
  If CheckBox7.Checked Then Memo2.Lines.LoadFromFile(Memo1.Lines.Strings[0]);

  param := '';

  If Not CheckBox7.Checked Then
  Begin
     // Wenn Quelle AVI dann mit AVISource laden
     If ((StrPos(PChar(AnsiUpperCase(Memo1.Lines.Strings[0])),PChar('AVI'))<>NIL)
     Or  (StrPos(PChar(AnsiUpperCase(Memo1.Lines.Strings[0])),PChar('AVS'))<>NIL)) Then
     for i := 0 To Memo1.Lines.Count Do
     Begin
          If (Memo1.Lines.Strings[i] <> '') Then
          Begin
               If (i>0) Then param := param + '+';
               param := param + 'AVISource("'+Memo1.Lines.Strings[i]+'")';
          End;
     End;

     // Video dem Script hinzufügen
     Memo2.Lines.Add(param);
  End;

  If FileExists(Edit1.Text) Then
  Begin
    Frames := 0;
    param := '';

    for I := 0 to Memo3.Lines.Count - 1 do
    begin
      Duration := StrRScan(PChar(Memo3.Lines.Strings[I]),':');
      Duration := StringReplace(Duration,':','',[rfReplaceAll]);
      Frames := Frames+StrToInt(Duration);
      Start    := StringReplace(Memo3.Lines.Strings[I],':'+Duration,'',[rfReplaceAll]);

      //Wenn CrossFade aktiviert
      If CheckBox3.Checked Then
      Begin
        If ( param='') Then param := 'Trim('+IntToStr(StrToInt(Start))+','+IntToStr(StrToInt(Start)+StrToInt(Duration)-1)+').FadeOut2(%x_fade)'
        else param := param+'+Trim('+IntToStr(StrToInt(Start))+','+IntToStr(StrToInt(Start)+StrToInt(Duration)-1)+').Reverse.FadeOut(%x_fade).Reverse.FadeOut2(%x_fade)'
      End
      else
      Begin
         If ( param='') Then param := 'Trim('+IntToStr(StrToInt(Start))+','+IntToStr(StrToInt(Start)+StrToInt(Duration)-1)+')'
         else param := param+'+Trim('+IntToStr(StrToInt(Start))+','+IntToStr(StrToInt(Start)+StrToInt(Duration)-1)+')'
      End;
    End;

    // Wenn CrossFade aktivert, dann den X_Fade Platzhalter durch die angegebene Frameanzahl ersetzen
    If CheckBox3.Checked Then
       Param := StringReplace(Param,'%x_fade',Edit10.Text,[rfReplaceAll]);

    Memo2.Lines.Add(param);

    // Deinterlace, wenn gewümscht
    If (ComboBox4.Text = 'FieldDeinterlace') Then
    Begin
      // DeComb für Deinterlace laden
      param := 'LoadPlugin("'+ExtractFilePath(Application.Exename)+'Plugins\Decomb.dll")';
      Memo2.Lines.Add(param);
      param := 'FieldDeinterlace(blend=false)';
      Memo2.Lines.Add(param);
    End;

    If (ComboBox4.Text = 'SmoothDeinterlace') Then
    Begin
      param := 'LoadPlugin("'+ExtractFilePath(Application.Exename)+'Plugins\SmoothDeinterlacer.dll")';
      Memo2.Lines.Add(param);
      param := 'SmoothDeinterlace(lacethresh=15, edgethresh=15)';
      Memo2.Lines.Add(param);
    End;


    If (ComboBox1.Text <> 'No Denoise') Then
    Begin
      Memo2.Lines.Add('LoadPlugin("'+ExtractFilePath(Application.Exename)+'Plugins\FluxSmooth.dll")');
      If (ComboBox1.ItemIndex=1) Then Memo2.Lines.Add('FluxSmooth(5,7)');
      If (ComboBox1.ItemIndex=2) Then Memo2.Lines.Add('FluxSmooth(7,7)');
      If (ComboBox1.ItemIndex=3) Then Memo2.Lines.Add('FluxSmooth(10,15)');
    End;

    If (ComboBox1.Text = 'FFT3D') Then
    Begin
      Memo2.Lines.Add('ConvertToYV12()');
      Memo2.Lines.Add('fft3dgpu(OH=16,OW=16,BH=48,BW=48,BT=4,plane=4,Sigma=2,Sharpen=1)');
    End;


    // Cropping, wenn aktivert
    If CheckBox1.Checked Then
    Begin
      Param := 'Crop('+Edit5.Text+','+Edit3.Text+','+Edit7.Text+','+Edit8.Text+')';
      Memo2.Lines.Add(param);
    End;
  End;

  // Wenn gewünscht, dann Ein= und Ausfaden
  If CheckBox2.Checked Then
  Begin
    Param := 'Reverse.FadeOut(%Fade).Reverse.FadeOut2(%Fade)';
    Param := StringReplace(Param,'%Fade',Edit9.Text,[rfReplaceAll]);
    Memo2.Lines.Add(param);
  End;

  // Wenn gewünscht, dann AutoCropping nutzen
  If CheckBox8.Checked Then
  Begin
     param := 'LoadPlugin("'+ExtractFilePath(Application.Exename)+'Plugins\AutoCrop.dll")';
    Memo2.Lines.Add(param);
    Param := 'AutoCrop(0, 4, 2, 2, 2, 2, 2, 40, 10)';
    Memo2.Lines.Add(param);
  End;

  // AVISynth File speichern
  If Not CheckBox7.Checked Then
    Memo2.Lines.SaveToFile (Edit2.Text)
  Else
    Memo2.Lines.SaveToFile (Memo1.Lines.Strings[0]);

  // Zu Audio Processing-Liste
  If CheckBox6.Checked Then
  Begin
  If Not CheckBox7.Checked Then
      Memo5.Lines.Add(Edit2.Text)
    Else
      Memo5.Lines.Add(Memo1.Lines.Strings[0]);
  End;
end;
//------------------------------------------------------------------------------
procedure TForm1.Init(Sender: TObject);
Var AppDir,
    VidDir,
    IniFile   : String;
    Ini       : TIniFile;
    SourceDir : String;
    i,
    s_width,
    s_height,
    Resultat  : Integer;
begin
  TargetDir := '';
  AppDir    := ExtractFilePath(Application.Exename);

  If (FileExists(AppDir+'VCF2AVS.ini')) Then IniFile := AppDir+'VCF2AVS.ini'
  Else
  Begin
    MessageBox(Form1.Handle,PChar('No Ini-File found !!!!'),PChar('WARNING:'),MB_OK);
    Application.Terminate;
  End;

  // Wenn Ini-Datei vorhanden, dann einlesen
  // Inidatei existiert, dann öffnen
  If FileExists(IniFile) Then
  Begin
   try
     // Ini-Datei öffnen
     Ini:=TIniFile.create(IniFile);
     VDubPath.Text  := Ini.ReadString('Common','VDubPath',AppDir);
     VidDir         := Ini.ReadString('Common','VidPath',AppDir);
     AVISplitFolder := Ini.ReadString('Common','AVISplitPath',AppDir);

     // BeSweet Audioprofile einlesen
     BS_Profiles.Lines.Clear;
     ComboBox2.Items.Clear;

     // Profilenamen einlesen
     Ini.ReadSection ('BeSweet',BS_Profiles.Lines);

     // Profilnamen in die Combobox kopieren
     for I := 0 to BS_Profiles.Lines.Count - 1 do
       ComboBox2.Items.Add(BS_Profiles.Lines.Strings[i]);
     ComboBox2.Text := BS_Profiles.Lines.Strings[0];

     // BeSweet Commandlines für die jeweiligen Profile einlesen
     BS_Profiles.Lines.Clear;
     Ini.ReadSectionValues('BeSweet',BS_Profiles.Lines);

     // Fading Werte einlesen
     Edit9.Text     := Ini.ReadString('Fading','IOFade','50');
     Edit10.Text    := Ini.ReadString('Fading','XFade','25');

     // TemporalSmoother Werte einlesen
     ComboBox1.ItemIndex := Ini.ReadInteger('Denoise','Value',0);
   
   finally
     // Ini-Datei wieder freigeben und schließen
     Ini.free;
   end;
  End;

  If DirectoryExists(VidDir) Then
    OpenDialog1.InitialDir := VidDir
  else
    OpenDialog1.InitialDir := AppDir;

  // SaveWave & BeSweet Support ebenfalls aktivieren
  CheckBox4.Enabled  := FileExists(AppDir+'BeSweet.exe');
  CheckBox5.Enabled  := FileExists(AppDir+'BeSweet.exe');
  CheckBox4.Checked  := FileExists(AppDir+'BeSweet.exe');
  CheckBox5.Checked  := FileExists(AppDir+'BeSweet.exe');

  // AVISplit Button nur aktivieren, wenn avisplit.exe gefunden
  Button10.Enabled := FileExists(AVISplitFolder+'avisplit.exe');

  // Audio Processing nur aktivieren, wenn VirtualDub.exe gefunden wird
  Button8.Enabled := FileExists(VDubPath.Text+'VirtualDub.exe');

  // PVR2AVS Codeteil
  If (ParamCount > 0) And (ParamStr(1) = '/PVR2AVS') Then
  Begin
    SourceDir := ParamStr(2);
    TargetDir := ParamStr(3);
  End;
end;
//------------------------------------------------------------------------------
procedure TForm1.ProcessAudio(Sender: TObject);
Var i,c     : Integer;
    Param,
    Appdir,
    DName    : String;
    AHandle  : LongWord;
    BS_Index : Integer;
begin
  c      := Memo5.Lines.Count;
  Appdir := ExtractFilePath(Application.Exename);
  if (c > 0) Then
  Begin
    Button8.Enabled := False;
    // Alle AVI/AVS-Dateien aus der Audiodatei-Memobox abarbeiten
    For i := 0 To c-1 Do
    Begin
      DName := Memo5.Lines.Strings[i];

      // SaveWave Block nur ausführen, wenn keine Wavedatei
      // hinzugefügt wurde.
      If (AnsiUpperCase(ExtractFileExt(DName))<> '.WAV') Then
      Begin
        ExecuteAndWait('"'+AppDir+'avs2wav.exe" "'+ DName + '" "'+ChangeFileExt(DName,'.WAV')+'"',AppDir, True, True);
      End;

      // Wenn Processing durch BeSweet aktiviert, dann entsprechende Zeile in Batchdatei einfügen
      If (CheckBox4.Checked) Then
      Begin
        // Hier gemäss Ausgabeformat den entsprechenden BeSweet Commandstring verwenden
        If (ComboBox2.ItemIndex = -1) Then
           BS_Index := 0
        Else
           BS_Index := ComboBox2.ItemIndex;

        Param := BS_Profiles.Lines.Strings[BS_Index];

        If (AnsiUpperCase(ExtractFileExt(DName))<> '.WAV') Then
          Param := StringReplace( Param,'%in',ChangeFileExt(DName,'.WAV'),[rfReplaceAll])
        Else
          Param := StringReplace( Param,'%in',DName,[rfReplaceAll]);

        Param := StringReplace( Param,'%out',ChangeFileExt(DName,''),[rfReplaceAll]);
        ExecuteAndWait(Appdir+'BeSweet.exe '+Param,Appdir,False,True);
      End;

      // Wenn gewünscht, dann Wavedatei nach Konvertierung mit BeSweet löschen
      If ((CheckBox4.Checked) And (CheckBox5.Checked) And (AnsiUpperCase(ExtractFileExt(DName))<> '.WAV')) Then DeleteFile(ChangeFileExt(Memo5.Lines.Strings[i],'.WAV'));
    End;

    // Temporäre Datei löschen
    DeleteFile(VDubPath.Text+'swave.vcf');
    Button8.Enabled := True;
  End;
end;
//------------------------------------------------------------------------------
procedure TForm1.Button6Click(Sender: TObject);
begin
  Memo5.Lines.Clear;
end;
//------------------------------------------------------------------------------
procedure TForm1.Button7Click(Sender: TObject);
VAR I : Integer;
begin
 if OpenDialog5.Execute then
  with OpenDialog5.Files do
  for I := 0 to Count - 1 do
     Memo5.Lines.Add(Strings[I]);
end;
//------------------------------------------------------------------------------
procedure TForm1.SaveIni(Sender: TObject; var Action: TCloseAction);
Var Appdir : String;
    Ini    : TIniFile;
begin
  // Pfad der Applikation feststellen
  Appdir := ExtractFilePath(Application.Exename);
  try
    // Ini-Datei öffnen
    Ini:=TIniFile.create(AppDir+'VCF2AVS.ini');
    Ini.WriteString('Common','VidPath',VidFolder);
    Ini.WriteString('Fading','IOFade',Edit9.Text);
    Ini.WriteString('Fading','XFade',Edit10.Text);
    If (ComboBox1.ItemIndex <> -1) Then
      Ini.WriteInteger('Denoise','Value',ComboBox1.ItemIndex)
    Else
      Ini.WriteInteger('Denoise','Value',0);

  finally
    // Ini-Datei wieder freigeben und schließen
    Ini.free;
  end;
end;
//------------------------------------------------------------------------------
procedure TForm1.Button9Click(Sender: TObject);
begin
  Memo4.Lines.Clear;
end;
//------------------------------------------------------------------------------
procedure TForm1.Button5Click(Sender: TObject);
VAR I : Integer;
begin
 if OpenDialog1.Execute then
  with OpenDialog1.Files do
  for I := 0 to Count - 1 do
     Memo4.Lines.Add(Strings[I]);
end;
//------------------------------------------------------------------------------
procedure TForm1.Button10Click(Sender: TObject);
Var i,c     : Integer;
    Param,
    Appdir,
    DName   : String;
    AHandle : LongWord;
begin
  c      := Memo4.Lines.Count;
  Appdir := ExtractFilePath(Application.Exename);
  if (c > 0) Then
  Begin
    Button10.Enabled := False;
    // Alle AVI/AVS-Dateien aus der AVISplit-Memobox abarbeiten
    For i := 0 To c-1 Do
    Begin
      DName := Memo4.Lines.Strings[i];
      // AVISplit starten
      ExecuteAndWait(AVISplitFolder+'avisplit.exe "'+Memo4.Lines.Strings[i]+'" '+Edit11.Text+' -x -n -l', AVISplitFolder,False, True);
    End;
    Button10.Enabled := True;
  End;
end;
//------------------------------------------------------------------------------
procedure TForm1.RenameAVS(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  // Rename des Ziel AVS nach dem Namen des VCF-Files
  If ((Key=Ord('R')) And (Shift=[ssCtrl])) Then Edit2.Text := ChangeFileExt(Edit1.Text,'.AVS');
end;
//------------------------------------------------------------------------------
procedure TForm1.GenerateAVS(MPEGFile,AVSFile : String);
VAR F          : TextFile;
    i,j        : Integer;
    Audio,
    Video,
    Delay,
    AppPath,
    D2V_Project,
    MPA_File,
    WAV_File,
    DVD2AVI_CL : String;
    CommandSuccess : Boolean;
begin
  AppPath := ExtractFilePath(Application.Exename);
  D2V_Project := ChangeFileExt(AVSFile,'');
  DVD2AVI_CL := '-IA=1 -FO=0 -YR=2 -TN=1 -OM=1 -IF=['+MPEGFile+'] -DSD=0 -DRC=0 -DSA=0 -OF=['+D2V_Project+'] -exit';

  CommandSuccess := ExecuteAndWait(AppPath+'\DGIndex\DGIndex.exe '+DVD2AVI_CL, AppPath,False, True);

  If Not CommandSuccess Then Exit;

  MPA_File := '';
  MPA_File := GetMPAFileName(D2V_Project);

  If (MPA_File <> 'Not_Found') Then
  Begin
    Delay := GetDelay(MPA_File);
    Audio := 'A = MPASource("'+MPA_File+'")';
  End;

  // Save AVS
  AssignFile(F,AVSFile);
  Rewrite(F);
  WriteLn(F,'LoadPlugin("'+AppPath+'Plugins\DGDecode.dll")');
  WriteLn(F,'LoadPlugin("'+AppPath+'Plugins\mpasource.dll")');

  If (MPA_File <> 'Not_Found') Then
  Begin
    WriteLn(F,'V=mpeg2source("'+D2V_Project+'.D2V")');
    WriteLn(F, Audio);
    WriteLn(F, 'AudioDub(V,A)');
    WriteLn(F, 'DelayAudio('+StringReplace(FormatFloat('##0.000', (StrToInt(Delay)/1000)),',','.',[rfReplaceAll]) +')');
  End
  Else
    WriteLn(F,'mpeg2source("'+D2V_Project+'.D2V")');

  WriteLn(F,'ConvertToYUY2()');
  CloseFile(F);
  Memo1.Lines.Add('AVISynth Datei erstellt: '+AVSFile);

end;
//------------------------------------------------------------------------------
procedure TForm1.AddToSource(Sender: TObject);
begin
  Edit2.Enabled := Not CheckBox7.Checked;
end;

end.
