Jump to content
Marty001

How do I execute FFMPEG from inside a VCL App?

Recommended Posts

I am wanting to execute a CLI command to convert all FLAC files to MP3, after looking on google and the usual places I have come up with 

 

procedure TForm1.act_MUSIC_CONVERSION_FLACStoMP3Execute(Sender: TObject);
begin
  ShellExecute(0, nil, 'cmd.exe',PWideCHar( 'find '+PwideChar(form1.FileListBox1.Directory)+'. -name "*.flac" -exec ffmpeg -i {} -ab 320k -map_metadata 0 -id3v2_version 3 {}.mp3 \;'), nil, SW_HIDE);
end;

however the command window opens but nothing is executed :(

Share this post


Link to post

In Windows command line Find searches for text in files.

 

In BASH it does look more like your find command, it would need to be run with WSL.EXE not CMD.EXE if you are using Windows Subsystem for Linux. The path probably needs to be put in double quotes in case it contains spaces and be fixed up to be what it would be looking for the Linux side.

Edited by Brian Evans

Share this post


Link to post
Posted (edited)
On 12/30/2023 at 7:54 PM, Marty001 said:

I am wanting to execute a CLI command to convert all FLAC files to MP3, after looking on google and the usual places I have come up with 

...

however the command window opens but nothing is executed 😞

I would not suggest putting it all on the OS to run that entire command in one go.  You should break it up into steps and then use appropriate APIs to run each step directly in your code, eg:

uses
  ..., System.SysUtils;

procedure TForm1.act_MUSIC_CONVERSION_FLACStoMP3Execute(Sender: TObject);
var
  dir: string;
  sr: TSearchRec;
begin
  dir := IncludeTrailingPathDelimiter(FileListBox1.Directory);
  if FindFirst(dir + '*.flac', faAnyFile, sr) <> 0 then
  try
    repeat
      if (sr.Attr and faDirectory) = 0 then
      begin
        ShellExecute(0, nil, 'ffmpeg.exe', PChar('-i "' + sr.Name + '" -ab 320k -map_metadata 0 -id3v2_version 3 "' + sr.Name + '.mp3"'), PChar(dir), SW_HIDE);
      end;
    until FindNext(sr) <> 0;
  finally
    FindClose(sr);
  end;
end;

Alternatively:

uses
  ..., System.SysUtils, System.IOUtils;

procedure TForm1.act_MUSIC_CONVERSION_FLACStoMP3Execute(Sender: TObject);
var
  dir, fileName: string;
begin
  dir := IncludeTrailingPathDelimiter(FileListBox1.Directory);
  for fileName in TDirectory.GetFiles(dir, '*.flac') do
    ShellExecute(0, nil, 'ffmpeg.exe', PChar('-i "' + fileName + '" -ab 320k -map_metadata 0 -id3v2_version 3 "' + fileName + '.mp3"'), PChar(dir), SW_HIDE);
end;

Yes, its' more work, but it gives you much more control.

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×