WarPie90

FFMPEG & Recording with Simba

May 16th, 2018
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.41 KB | None | 0 0
  1. program new;
  2.  
  3. type
  4.   TGameRecorder = record
  5.     Timer: TMMLTimer;
  6.     Path: String;
  7.     FPS, Duration: Int32;
  8.     Frames: array of TMufasaBitmap;
  9.     CanFree: Boolean;
  10.   end;
  11.  
  12. var
  13.   GameRecorder: TGameRecorder;
  14.  
  15.  
  16. procedure TGameRecorder.Capture(Junk, Sender: Pointer); static;
  17. var
  18.   i,W,H, FrameCount: Int32;
  19.   Proc: TProcess;
  20. begin
  21.   GameRecorder.CanFree := False;
  22.   FrameCount := Length(GameRecorder.Frames);
  23.  
  24.   if FrameCount * (1000 div GameRecorder.FPS) >= GameRecorder.Duration then
  25.   begin
  26.     for i:=0 to FrameCount-1 do
  27.     begin
  28.       GameRecorder.Frames[i].SaveToFile(Format('%s/tmp/img%d.jpg', [GameRecorder.Path, i+1]));
  29.       GameRecorder.Frames[i].Free();
  30.     end;
  31.     SetLength(GameRecorder.Frames, 0);
  32.  
  33.     client.WriteLn('Generating video...');
  34.     Proc.Init(nil);
  35.     Proc.SetExecutable('ffmpeg -vtag xvid -framerate ' + ToStr(GameRecorder.FPS) + ' -i "'+GameRecorder.Path+'/tmp/img%01d.jpg" "'+GameRecorder.Path+'/'+FormatDateTime('dd.mm.yy hh.nn.ss',Now())+'.avi"');
  36.     Proc.SetOptions(Proc.GetOptions + [poWaitOnExit]);
  37.     Proc.Execute();
  38.     Proc.Free();
  39.   end else
  40.   begin
  41.     SetLength(GameRecorder.Frames, FrameCount+1);
  42.     Client.GetIOManager.GetDimensions(W,H);
  43.     GameRecorder.Frames[FrameCount].Init(client.GetMBitmaps);
  44.     if W mod 2 = 1 then Dec(W);
  45.     if H mod 2 = 1 then Dec(H);
  46.     GameRecorder.Frames[FrameCount].CopyClientToBitmap(client.GetIOManager, True, 0,0, W-1, H-1);
  47.   end;
  48.   GameRecorder.CanFree := True;
  49. end;
  50.  
  51.  
  52. procedure TGameRecorder.Init(ClipPath: String; ClipFPS, ClipDuration: Int32);
  53. begin
  54.   Self.Path     := ClipPath;
  55.   Self.FPS      := ClipFPS;
  56.   Self.Duration := ClipDuration;
  57.  
  58.   CreateDirectory(Self.Path+'/');
  59.   CreateDirectory(Self.Path+'/tmp/');
  60.  
  61.   Self.Timer.Init();
  62.   Self.Timer.SetInterval(1000 div Self.FPS);
  63.   Self.Timer.SetOnTimer(natify(@TGameRecorder.Capture));
  64. end;
  65.  
  66. procedure TGameRecorder.Start();
  67. begin
  68.   Self.Timer.SetEnabled(True);
  69. end;
  70.  
  71. procedure TGameRecorder.Stop();
  72. begin
  73.   Self.Timer.SetEnabled(False);
  74. end;
  75.  
  76. procedure TGameRecorder.Free();
  77. var i: Int32;
  78. begin
  79.   Self.Timer.SetEnabled(False);
  80.   while not Self.CanFree do Wait(5);
  81.   Self.Timer.Free();
  82.   for i:=0 to High(self.Frames) do Self.Frames[i].Free();
  83.   SetLength(Self.Frames, 0);
  84. end;
  85.  
  86.  
  87. begin
  88.   GameRecorder.Init('C:/Simba/Videos', 10, 15*1000);
  89.   GameRecorder.Start();
  90.   Wait(17*1000);
  91.   GameRecorder.Stop();
  92.   GameRecorder.Free();
  93. end.
Add Comment
Please, Sign In to add comment