[Lazarus] send complex command to linux

Lukasz Sokol el.es.cr at gmail.com
Wed Dec 22 10:51:00 CET 2010


Hi, I know you're not the original author, but...

On 21/12/2010 20:29, Andrew Haines wrote:
> 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;
> 
// should this 

>   while Flac.Running or (Flac.Output.NumBytesAvailable > 0) do
>     PipeOutput;
> 
// or can this not happen ? And if it can, is it dangerous ?
  Flac.Running = false;
  Flac.Output.NumBytesAvailable > 0

// maybe the above condition should be split and we should wait yntil Running=true :

   while not Flac.Running do
     Sleep(1);

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


// ?

Lukasz

> 
>   ListBox1.Items.Add(Line);
> 
>   Flac.Free;
>   Lame.Free;
> end;
> 
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
> 






More information about the Lazarus mailing list