hudy boy
hudy boy

Reputation: 59

executing ffmpeg.exe from delphi with shellExecute

I am trying to make a video from list of bmp images, when I execute from command line ffmpeg -i img%5d.bmp -r 20 -c:v libx264 -preset slow -crf 22 output.mkv everything is ok, but when I try to execute from delphi project, it doesn't work. I've tried:

parameters := '-i img%5d.bmp -r 20 -c:v libx264 -preset slow -crf 22  output.mkv';
ShellExecute(0, nil, PChar('c:\videos\ffmpeg.exe'), PChar(parameters), nil, SW_NORMAL); 

I can't use TProcess, it has to be done with ShellExecute Please help me with this one, tnx in advance :)

Upvotes: 1

Views: 1626

Answers (1)

Blurry Sterk
Blurry Sterk

Reputation: 1607

Here is a nice little piece of code I use. I am giving you this code but expect you to NOT just copy and paste. Please go through it to understand what it does.

The CreateProcess is what David spoke about. It is the best way to create and handle a process in Windows. Also note the WaitForSingleObject. That causes your execution to wait for Windows to finish before it returns after creating the process. You may of course remove the wait part. Also note the create flags.

It is called ExecuteAndWait. When you call it it will create a process and wait for it to finish:

function ExecuteAndWait(AFilename : String; AParameter : string;
                        ACmdShow : Integer; var AErrorOrExitCode : Cardinal): Boolean;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  S : String;
begin
  FillChar(StartupInfo,Sizeof(StartupInfo),0);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := ACmdShow;
  S := AParameter;
  UniqueString(S);
  if not CreateProcess(PChar(AFilename),PChar(S),nil,nil,False,
                       CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,nil,nil,
                       StartupInfo,ProcessInfo) then
    begin
      Result := False;
      AErrorOrExitCode := GetLastError;
    end
  else
    begin
      Result := True;
      WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
      GetExitCodeProcess(ProcessInfo.hProcess,AErrorOrExitCode);
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);
    end;
end;

Example:

var
  ErrorOrExitCode : Cardinal;
begin
  If not ExecuteAndWait('c:\windows\regedit.exe','"c:\windows\regedit.exe" "c:\test.reg"',SW_SHOWDEFAULT,ErrorOrExitCode) then
    Showmessage(Inttostr(ErrorOrExitCode));

Here I tell regedit to load a exported reg file. Note the double-quotes and also note that I specify the executable as the filename and as part of the parameter.

Adapt it to your needs.

Upvotes: 1

Related Questions