[Lazarus] send complex command to linux

Andrew Haines AndrewD207 at aol.com
Tue Dec 21 21:29:13 CET 2010


On 12/19/10 03:44, ugaciaka wrote:
> 2010/12/18 Andrew Haines <AndrewD207 at aol.com>:
>> On 12/18/10 13:49, ugaciaka wrote:
>>> Hi,
>>>
>>> this is an example of what I do
>>> ===================================================
> 
> @Andrew Haines
> tnx and very beautiful solution :-D
> 
> --

You are welcome :)

I played with this a bit more the other day and added a progressbar that
gets updated as the file is converted. Flac writes to stderr too and we
can read this to get the progress percent.

regards

Andrew

====== code ========
procedure TForm1.ConvertFile(AFile: String);
var
  Flac: TProcess;
  Lame: TProcess;
  NewFile: String;
  Line: String;
  procedure FillStdErr;
  var
    Buf: Char;
    FPos, i: Integer;
  begin
    while Flac.Stderr.NumBytesAvailable > 0 do
    begin
      Flac.Stderr.Read(Buf,1);
      if Buf in  [#13, #10] then
        begin
          if Length(Line) > 0 then
            ListBox1.Items.Add(Line);
          FPos := Pos('%', Line);
          if FPos > 0 then
            begin
              i := FPos;
              while Line[i] <> ' ' do
                dec (i);
              Line := Copy(Line, i, FPos-i);
              ProgressBar1.Position:=StrToInt(Line); // 0 to 100
representing percent
            end;
          Line := '';
          Application.ProcessMessages; // so progress bar is updated
        end
      else
        Line := Line + Buf;
    end;
  end;
  procedure PipeOutput;
  var
    Buffer: array[0..127] of byte;
    RCount: Integer;
  begin
    if Flac.Output.NumBytesAvailable > 0 then
    begin
      RCount := Flac.Output.Read(Buffer, Min(128,
Flac.Output.NumBytesAvailable));
      Lame.Input.Write(Buffer, RCount);
    end
    else
      Sleep(1);
    FillStdErr;
  end;


begin
  NewFile := ExtractFileNameWithoutExt(AFile)+'.mp3';
  Flac := TProcess.Create(nil);
  Lame := TProcess.Create(nil);
  Flac.CommandLine := 'flac -cd '+ AFile;
  Lame.CommandLine := 'lame -h -b 320 - '+ NewFile;

  Flac.Options := [poUsePipes];
  Lame.Options := [poUsePipes];

  Flac.Execute;
  Lame.Execute;

  while Flac.Running or (Flac.Output.NumBytesAvailable > 0) do
    PipeOutput;


  ListBox1.Items.Add(Line);

  Flac.Free;
  Lame.Free;
end;




More information about the Lazarus mailing list