Recent

Author Topic: [Solved] Process parameters  (Read 1019 times)

Espectr0

  • Full Member
  • ***
  • Posts: 175
[Solved] Process parameters
« on: April 23, 2023, 12:17:30 am »
Could it be that using TProcess and RunCommandLoop I should convert the string that I pass as a parameter or something similar?
I have the problem with ffmpeg, from the terminal I execute it correctly, but from my process it gives me an error with the parameters (not valid).
And I execute the same in both cases.
« Last Edit: April 23, 2023, 04:08:43 pm by Espectr0 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11080
  • FPC developer.
Re: Process parameters
« Reply #1 on: April 23, 2023, 12:20:07 am »
That is very well possible. A shell might doing additional processing before passing it on to the kernel call, while Tprocess general doesn't.

This is also why it is prefered to pass the parameters to the TProcess instances one at a time, and not use the now deprecated commandline property.

Espectr0

  • Full Member
  • ***
  • Posts: 175
Re: Process parameters
« Reply #2 on: April 23, 2023, 12:26:20 am »
Yes, I pass it as I should (I think):

Code: Pascal  [Select][+][-]
  1.     for i := 0 to High(AParams) do
  2.       AProcess.Parameters.Add(AParams[i]);
  3.  

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11080
  • FPC developer.
Re: Process parameters
« Reply #3 on: April 23, 2023, 10:30:13 am »
I assume that should work then.

Note that things like

blabla  -i bla

are two parameters, "-i" and "bla". Show some code?

Espectr0

  • Full Member
  • ***
  • Posts: 175
Re: Process parameters
« Reply #4 on: April 23, 2023, 12:28:15 pm »
Code: Pascal  [Select][+][-]
  1. const
  2.   FFMPEG_SCParams = '-hide_banner -i "%input" -vf "select=''gt(scene,0.4)'',showinfo" -f null -';
  3.  
  4. var
  5.   AParamArray: TStringArray;
  6.   sl: TStringList = NIL;    
  7.   i: Integer;
  8.  
  9. ...
  10.  
  11. AParamArray := FFMPEG_SCParams.Split(' ');
  12.  
  13.     for i := 0 to High(AParamArray) do
  14.       AParamArray[i] := StringReplace(AParamArray[i], '%input', AFileName, []);      
  15.  
  16. ExecuteAppLoop(GetExtractAppFile, AParamArray, sl, NIL)
  17.  


Code: Pascal  [Select][+][-]
  1. function ExecuteAppLoop(const AAppFileName: String; const AParams: TStringArray; var Output: TStringList; const ACB: TOnRunCommandEvent = NIL): Boolean;
  2. var
  3.   AProcess  : TProcess;
  4.   sOutput   : String;
  5.   StdError  : String;
  6.   i, Status : Integer;
  7. begin
  8.   AProcess := TProcess.Create(NIL);
  9.   try
  10.     AProcess.ShowWindow := swoHide;
  11.     AProcess.Options := AProcess.Options + [poRunIdle, poStderrToOutPut];
  12.     AProcess.Executable := AAppFileName;
  13.     AProcess.OnRunCommandEvent := ACB;
  14.  
  15.     for i := 0 to High(AParams) do
  16.       AProcess.Parameters.Add(AParams[i]);
  17.  
  18.     if not Assigned(Output) then
  19.       Output := TStringList.Create;
  20.  
  21.     Result := AProcess.RunCommandLoop(sOutput, StdError, Status) = 0;
  22.     Output.Text := sOutput;
  23.   finally
  24.     AProcess.Free;
  25.   end;
  26. end;
  27.  
  28.  

Could there have been a mistake in the code?
Or is it something I'm missing for mac?

from lazarus it tells me that the "select=" filter is not found but from terminal it works.
« Last Edit: April 23, 2023, 12:31:39 pm by Espectr0 »

TRon

  • Hero Member
  • *****
  • Posts: 1862
Re: Process parameters
« Reply #5 on: April 23, 2023, 02:45:07 pm »
from lazarus it tells me that the "select=" filter is not found but from terminal it works.
I am unable to find the select= in the (official ffmpeg) documentation as well. Do you perhaps have a reference that can be used to verify ? found it here: https://ffmpeg.org/ffmpeg-filters.html#select_002c-aselect

I am also a bit unsure on the use of "%input". What does that stand for ? I Overlooked in your code that you replace the %input parameter.
« Last Edit: April 23, 2023, 03:15:37 pm by TRon »

Espectr0

  • Full Member
  • ***
  • Posts: 175
Re: Process parameters
« Reply #6 on: April 23, 2023, 03:19:42 pm »
I replace "%input" with the video file in the code and I see you found the reference :)
This is what I get in both cases...

in my Lazarus process:
Code: Pascal  [Select][+][-]
  1. [mpeg @ 0x7fe1ea818a00] Packet corrupt (stream = 0, dts = 19384521).
  2. Input #0, mpeg, from 'A HA - TAKE ON ME.mpg':
  3.   Duration: 00:03:35.48, start: 0.374689, bitrate: 1459 kb/s
  4.   Stream #0:0[0x1e0]: Video: mpeg1video, yuv420p(tv), 352x240 [SAR 1:1 DAR 22:15], 1180 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 29.97 tbc
  5.   Stream #0:1[0x1c0]: Audio: mp2, 44100 Hz, stereo, s16p, 256 kb/s
  6. Stream mapping:
  7.   Stream #0:0 -> #0:0 (mpeg1video (native) -> wrapped_avframe (native))
  8.   Stream #0:1 -> #0:1 (mp2 (native) -> pcm_s16le (native))
  9. Press [q] to stop, [?] for help
  10. [AVFilterGraph @ 0x7fe1e97111c0] No such filter: '"select'
  11. Error reinitializing filters!
  12. Failed to inject frame into filter network: Invalid argument
  13. Error while processing the decoded data for stream #0:0
  14. Conversion failed!
  15.  

in Terminal (cut because it is long):
Code: Pascal  [Select][+][-]
  1. ffmpeg -hide_banner -i "A HA - TAKE ON ME.mpg" -vf "select='gt(scene,0.4)',showinfo" -f null -
  2. [mpeg @ 0x7fc79680a200] Packet corrupt (stream = 0, dts = 19384521).
  3. Input #0, mpeg, from 'A HA - TAKE ON ME.mpg':
  4.   Duration: 00:03:35.48, start: 0.374689, bitrate: 1459 kb/s
  5.   Stream #0:0[0x1e0]: Video: mpeg1video, yuv420p(tv), 352x240 [SAR 1:1 DAR 22:15], 1180 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 29.97 tbc
  6.   Stream #0:1[0x1c0]: Audio: mp2, 44100 Hz, stereo, s16p, 256 kb/s
  7. Stream mapping:
  8.   Stream #0:0 -> #0:0 (mpeg1video (native) -> wrapped_avframe (native))
  9.   Stream #0:1 -> #0:1 (mp2 (native) -> pcm_s16le (native))
  10. Press [q] to stop, [?] for help
  11. [Parsed_showinfo_1 @ 0x7fc796707900] config in time_base: 1/90000, frame_rate: 30000/1001
  12. [Parsed_showinfo_1 @ 0x7fc796707900] config out time_base: 0/0, frame_rate: 0/0
  13. [Parsed_showinfo_1 @ 0x7fc796707900] n:   0 pts: 314782 pts_time:3.49758 pos:   661474 fmt:yuv420p sar:1/1 s:352x240 i:P iskey:0 type:B checksum:A4BD5CE1 plane_checksum:[6D8AA134 E38B7F73 C0FB3C2B] mean:[171 129 125] stdev:[44.0 1.0 1.5]
  14. [Parsed_showinfo_1 @ 0x7fc796707900]   side data - pan/scan
  15. [Parsed_showinfo_1 @ 0x7fc796707900] color_range:tv color_space:unknown color_primaries:unknown color_trc:unknown
  16. Output #0, null, to 'pipe:':
  17.   Metadata:
  18.     encoder         : Lavf58.76.100
  19.   Stream #0:0: Video: wrapped_avframe, yuv420p(tv, progressive), 352x240 [SAR 1:1 DAR 22:15], q=2-31, 200 kb/s, 29.97 fps, 29.97 tbn
  20.     Metadata:
  21.       encoder         : Lavc58.134.100 wrapped_avframe
  22.   Stream #0:1: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
  23.     Metadata:
  24.       encoder         : Lavc58.134.100 pcm_s16le
  25. [Parsed_showinfo_1 @ 0x7fc796707900] n:   1 pts: 633100 pts_time:7.03444 pos:  1304546 fmt:yuv420p sar:1/1 s:352x240 i:P iskey:0 type:B checksum:5F81A68B plane_checksum:[35FC9F4E C139986B C1426EC3] mean:[148 129 125] stdev:[46.2 0.9 1.2]
  26.  


TRon

  • Hero Member
  • *****
  • Posts: 1862
Re: Process parameters
« Reply #7 on: April 23, 2023, 03:28:00 pm »
Thank you for the additional information. I was thinking I could solve this without having to use my terminal... now experimenting  :-[ At first glance ffmpeg seems picky on how you feed it parameters. It has to be exact quoting or it will fail (at least on my setup). I seem to remember there is something related with quotes and tprocess parameters (but I do not know what it was exactly).
« Last Edit: April 23, 2023, 03:29:34 pm by TRon »

Espectr0

  • Full Member
  • ***
  • Posts: 175
Re: Process parameters
« Reply #8 on: April 23, 2023, 03:32:31 pm »
Thanks @TRon, I hope you can find my fault because I don't know what to try now  %)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11080
  • FPC developer.
Re: Process parameters
« Reply #9 on: April 23, 2023, 03:45:34 pm »
Don't use split. Use Parameters.Add for every item, and lose all quoting.

Those are for shell parsing purposes only, and this doesn't go the shell, something like:

Code: [Select]
Parameters.Add ('-hide_banner');
Parameters.Add ('-i');
Parameters.Add (AFilename');
Parameters.Add ('-vf');
Parameters.Add ('select=gt(scene,0.4),showinfo');  // not sure about this one
Parameters.Add ('-f');
Parameters.Add ('null');
Parameters.Add ('-');

Don't overcomplicate with own parsing before you have the base code working.

TRon

  • Hero Member
  • *****
  • Posts: 1862
Re: Process parameters
« Reply #10 on: April 23, 2023, 04:01:38 pm »
marcov's advise is a good one.

It is Indeed as suspected. try
Code: Pascal  [Select][+][-]
  1. const
  2.   FFMPEG_SCParams = '-hide_banner -i %input -vf select=''gt(scene,0.4)'',showinfo -f null -';
  3.  
That seem to work for me.

Espectr0

  • Full Member
  • ***
  • Posts: 175
Re: Process parameters
« Reply #11 on: April 23, 2023, 04:08:27 pm »
You are correct, removing the quotes works correctly.

Thanks to both!

TRon

  • Hero Member
  • *****
  • Posts: 1862
Re: [Solved] Process parameters
« Reply #12 on: April 23, 2023, 04:21:16 pm »
You're more than welcome Espectr0. Glad to hear you got it solved.

As a general note for someone who stumbles on this kind of issues.

The shell/terminal processed the provided arguments in its own (invisible) way. You can verify that in a very simple manner.

Code: Pascal  [Select][+][-]
  1. program showarguments;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. var
  6.   index: integer;
  7. begin
  8.  for index:= 1 to ParamCount
  9.    do writeln('argument[', index, '] = <', ParamStr(index), '>');
  10. end.
  11.  

Now let's run that program from the shell with the arguments as TS used.
Code: Text  [Select][+][-]
  1. ./showarguments -hide_banner -i "test.mp4" -vf "select=''gt(scene,0.4)'',showinfo" -f null -
  2.  
And the program outputs
Code: Text  [Select][+][-]
  1. argument[1] = <-hide_banner>
  2. argument[2] = <-i>
  3. argument[3] = <test.mp4>
  4. argument[4] = <-vf>
  5. argument[5] = <select=''gt(scene,0.4)'',showinfo>
  6. argument[6] = <-f>
  7. argument[7] = <null>
  8. argument[8] = <->
  9.  

Note the omission of the (double) quotes that are provided at the terminal.

Now invoke ./showarguments using TProcess with the same commandline (arguments) and the output will be:
Code: Text  [Select][+][-]
  1. argument[1] = <-hide_banner>
  2. argument[2] = <-i>
  3. argument[3] = <"test.mp4">
  4. argument[4] = <-vf>
  5. argument[5] = <"select='gt(scene,0.4)',showinfo">
  6. argument[6] = <-f>
  7. argument[7] = <null>
  8. argument[8] = <->
  9.  
Which does include the (double) quotes.

Ergo, supplying commandline parameters for tprocess <> arguments (quoting) shell/terminal.
« Last Edit: April 23, 2023, 04:23:09 pm by TRon »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11080
  • FPC developer.
Re: [Solved] Process parameters
« Reply #13 on: April 23, 2023, 04:30:26 pm »
(More advanced users can inspect what gets passed to the OS using ktrace/strace/truss. I don't know what Mac OS X uses nowadays, my last Mac is PPC64)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: [Solved] Process parameters
« Reply #14 on: May 12, 2023, 01:49:48 pm »
There's dtrace and dtruss but various macOS security mechanisms need to first be defeated before they can be used in many cases especially on system binaries.

See https://poweruser.blog/using-dtrace-with-sip-enabled-3826a352e64b for details.

 

TinyPortal © 2005-2018