0

I want to invoke mpg123 from PHP (using exec) and monitor the diagnostic output generated by the program while it is running.

I have been searching the Internet and cannot find any way to see the redirected output of a command line program while it is running.

Instead, the output file is always written out AFTER the process finishes, but I need to access the output while it still running, hence my question.

Testing with:

mpg123.exe http://148.251.184.14:8192/stream | tee.exe streaming.txt

... file streaming.txt` is always empty while running the exe.

[Editors note: and so it would be, mpg123 sends diagnostic output to stderr].

Also, I tested this:

mpg123.exe http://148.251.184.14:8192/stream > streaming.txt

... and still no luck, because again, file streaming.txt is always empty while mpg123 is still running.

[Editor's note: of course, for the same reason as above, the command should be:

mpg123.exe http://148.251.184.14:8192/stream 2> streaming.txt

But still you see nothing in file streaming.txt until the program terminates.

end note]

Is there a way to do this? Seems to be a hard nut or not even possible... Thank you for any help.

PS:

Using static binary from: https://mpg123.de/download/win64/1.25.10/

Tee.exe: https://sourceforge.net/projects/unxutils/files/unxutils/current/

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
SiL3NC3
  • 690
  • 6
  • 29
  • Get `tee` from, for example, GnuWin32; then do something like `your-command | tee output.file`. – AlexP Jun 11 '18 at 12:34
  • Also was testing this ... no luck. :( Wanna run mpg123.exe [StreamURL] > streaming.txt. – SiL3NC3 Jun 11 '18 at 12:36
  • It will stay 0 bytes until closed. This is not what you asked. You asked how to see it while `mpg123` is running. Also, UnxUtils is ancient. – AlexP Jun 11 '18 at 12:51
  • Also was trying latest GnuWin32 tools without luck... :( – SiL3NC3 Jun 11 '18 at 13:33

3 Answers3

3

You could, for example, get tail from GnuWin32 (it's in package coreutils). Then:

  • In one command prompt window run tail -F output-file. This will initially sit there because there is no output-file yet. Let it sit.

  • In another command prompt window run your-command > output.file.

  • In the first command prompt window tail will display the contents of output-file as it is generated.

Note 1: The program your-command may buffer its output, so that it written in chunks. Some programs have options to minimize output buffering, for example sed -u or grep --line-buffered.

Note 2: tail works as fast as it can, but console output is quite slow on Windows. It is perfectly possible for a program to generate output much faster than tail can display it.

I have tested this procedure with dir /s C:\ > Ls-lR.txt and tail Ls-lR.txt.

The quirks of MPG123

The specific program which the querent wants to monitor is MPG123. This program:

  • Does not normally write to standard output, and it actually closes stdandard output unless it wants to write WAV data.

  • Writes diagnostic messages to standard error, but only if standard error is not redirected or the option -v is given.

So...

  • Open a command prompt window and type tail -F mpg123.out. Since there is no file named mpg123.out, tail will sit and wait. Let it wait.

     C> tail -F MPG123.out
    
  • Open a second command prompt window, and run mpg123

    • Redirecting stdandard error to mpg123.out, and

    • With the option -v.

      C> mpg123.exe 2>MPG123.out -v "\path\to\the\music\file.mp3"
      
  • In the first window, watch the diagnostic messages of MPG123.

AlexP
  • 4,370
  • 15
  • 15
0

Perhaps the "tee" already on the machine could be used. I do not have you mpg123.exe executable, so I cannot test it.

powershell -NoProfile -Command "& mpg123.exe [StreamURL] | Tee-Object -FilePath .\streaming.txt"

Edit

Based on the information from @AlexP that mpg123.exe is writing to stderr, I would try:

powershell -NoProfile -Command "& mpg123.exe [StreamURL] 2>&1 | Tee-Object -FilePath .\streaming.txt"
lit
  • 14,456
  • 10
  • 65
  • 119
  • mpg123.exe can be found here: https://mpg123.de/download/win64/1.25.10/. Then just go for a stream url like: http://148.251.184.14:8192/stream. – SiL3NC3 Jun 11 '18 at 14:56
  • Thank you for your reply. But also no luck ... :( – SiL3NC3 Jun 11 '18 at 15:00
  • @SiL3NC3 - Would you mind reporting the result? Error message? Was a `streaming.txt` file created? – lit Jun 11 '18 at 15:24
  • Sure! No error message, stream was starting correctly, but streaming.txt was not being created ... sry for missing the details for you. – SiL3NC3 Jun 11 '18 at 15:55
0

I have decided to delete my original answer and post a new one, because although the old one was factually correct it didn't answer the question very well. Now that I understand what the OP is actually doing, I can answer this properly.

The issue is actually very simple. Most programs, especially command line programs, on most platforms contain logic to detect if stdout or stderr has been redirected to a file (> file) or a pipe (e.g. | tee). This logic is usually actually buried in the runtime library so programs get it for free, which is why they pretty much all do it, and I'm sure that's true of mpg123 which is a relatively simple beast. What I say below will apply to almost any program.

Now, what this logic does is to decide whether or not to buffer output to stdout / stderr (it may make a different decision for each one). If output is going directly to the console (or, in Unix, the terminal) then it is not buffered at all (or maybe just on a per-line basis). Everything is sent out pretty much as soon as the program generates it.

If, on the other hand, output is redirected then mpg123 detects this and writes the data out in chunks (often 4k chunks), and if the total amount of output generated while the program is running is smaller than the size of the buffer then you won't see anything in the output file or pipe until the program terminates, at which point the buffer is flushed and the file closed (so you see it then, as the OP noted).

Now, knowing all that, we can explain the behaviour that the OP observes when running mpg123. This is not in fact down to any intricate juggling that mpg123 might do with file handles and the change in behaviour when you add in -v is just a side-effect. What you see is a direct result of the different buffer strategy used when the output is redirected.

So, using the binary linked to by the OP, this command:

mpg123 http://148.251.184.14:8192/stream

Generates the following output on the console straightaway (because nothing is buffered):

High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
        version 1.25.10; written and copyright by Michael Hipp and others
        free software (LGPL) without any warranty but with best wishes

Directory: http://148.251.184.14:8192/
Playing MPEG stream 1 of 1: stream ...
ICY-NAME: Chroma Metal
ICY-URL: http://chromaradio.com

MPEG 1.0 L III cbr128 44100 j-s

ICY-META: StreamTitle='Avantasia - The Seven Angels';

It then goes on to play the stream though the sound card, which takes quite a while. The above information is written to stdout (and mpg123 always writes diagostic information to stdout).

This command, however, behaves differently, because the output is buffered (note the redirection of stdout):

mpg123 http://148.251.184.14:8192/stream 2>x.txt

As noted by the OP, this just creates a zero length file while the stream is playing, because the total amount of diagnostic output fits in mpg123s internal buffer so it just stays there until the program terminates, at which point the output duly turns up in the file for the reason given above.

And finally, this command, with the -v parameter added in:

mpg123 -v http://148.251.184.14:8192/stream 2>x.txt

does generate some output in x.txt while the program is running because the buffer fills up with the extra diagnostic information that the -v flag generates and at that point mpg123 has to write it to disk. The -v flag means verbose. That's where the extra output comes from.

Please note though that when you do this the data in the file is still always some way behind (because the next buffer-full is building up and won't be output until it's full), so while adding -v might get you what you want (or at least some of it), it hasn't changed the underlying problem. You can see this quite clearly if you run the above command in one console window and tail -F x.txt in another. When you do that, nothing shows up for the first 5 seconds or so. Then some (partial) output appears, and so it goes on.

So I hope that clears things up. Windows and Unix behave pretty much the same in this regard. I will edit the OP's question to make it a little less confusing. It's a bit untidy at the moment.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • And with Linux it is working like a charm without verbose option... Just by calling: mpg123 [streamURL] >> streaming.txt 2>&1 & – SiL3NC3 Jun 12 '18 at 15:59
  • OK, thanks. Interesting that it behaves differently there. The runtime library on Linux must behave differently as `mpg123` will be sending all its output through that. That's not what I would have expected but clearly it must be true. Thanks for the update. – Paul Sanders Jun 12 '18 at 16:53
  • You are welcome. :) Imho Linux is having a better handling of background tasks (Threading). While testing on my Raspbian, the " &" was needed for getting it to work, but "START /B mpg123.exe ..." for Windows (CMD) was not successful. – SiL3NC3 Jun 13 '18 at 17:27