Observe stderr

Is there a way to have an observable containing a number (let’s say float) that updates on updates to stderr, but where the stderr is a string that contains in it the number to be pushed into the observable?

So if I run run and it outputs to stderr: “and the number is 1.23”
and i have a function that can extract that number from these strings (using RegEx etc), how do I connect that observable to stderr?

Or is there some other more common way of tying a variable to the most recent curated content of stderr?

What is the context here?

If you can’t modify the code writing to stderr, it’s really unlikely that you can intercept or redirect stderr in-process. If you can modify the code writing to stderr, then find a better way to make it communicate. So if you really must do this, you’ll need another process that’s reading from your stderr, parsing the text, and sending the values back via some IPC such as a socket.

Sure you can, via redirect_stderr. How do you think IJulia redirects stderr (even from external C libraries) to the notebook? You redirect it and then watch the redirected stream in an asynchronous task.

But I agree that it’s something that probably should come up extremely rarely — communicating with code by redirecting and watching stderr is kind of a last resort.

PS. Under the hood, redirect_stderr works by calling dup2, with some additional trickery on Windows.

3 Likes

Thanks for the pointer.

Sorry for being so cryptic, here’s the story:
I want to get the current time stamp from an open session of ffmpeg’s ffplay. Thanks to efforts from @giordano and @staticfloat we now have FFplay_jll available, so we can do this:

using FFplay_jll
file = "video.mp4"
ffplay() do exe
    run(`$exe $file`)
end

and the following line would update with stats about the currently playing frame:

   3.66 A-V: -0.017 fd=   5 aq=   23KB vq=  554KB sq=    0B f=0/0   

where the first float there is the time stamp. I want to capture that number. That’s why I want to listen to stderr, and on every change update a value.

Why don’t you simply use VideoIO.jl?

The main reason is that there is an issue with getting the current time stamp when seeking (see issue Inaccuracies with seek · Issue #242 · JuliaIO/VideoIO.jl · GitHub). So while building a rudimentary video player with Makie and VideoIO is very possible (see earlier efforts here GitHub - JuliaIO/VideoPlayer.jl: Video player for Julia), it is really hard to get accurate time stamps (e.g. with VFR) after seeking and scrolling in the video.