Search code examples
python-2.7stdoutgtk3libvlclibav

Redirect c library stdout messages in Python


I have a GUI application in Python 2.7 using Gtk+ 3.10.8 running on Linux Mint 17.1.

The application uses the python bindings for the libvlc library to embed a video player. The libvlc library uses libav for its video handling.

My problem is that libav is extremely verbose and generates a constant stream of warnings, and libvlc does not provide any mechanism to alter the verbosity or suppress it.

Obviously, one can turn off the noise by running the python script thus (for example):

$ ./myprogram.py &> /dev/null

I would like to be able to achieve the same effect programmatically from within the program. I'd be happy simply to suppress all access from external libraries to stdout, but ultimately it would be better to capture that output and implement some logging.

All the examples I can find on this subject assume more direct control of the external process, i.e. such as declaring it as a subprocess. Is there another way to do it when I don't have direct access to the actual library in question (libav) from my Python code?

Edit: Thanks to everyone's help, the correct answer can be found at: Python version of freopen(), answer #3.

You just have to make sure that you are not running your code from an IDE that interferes with stdout in any way. Interactive IDEs tend to do that, and will throw an error like:

AttributeError: FileWrapper instance has no attribute 'fileno'

Solution

  • The easiest solution is probably to close stdout and open /dev/null as the new stdout. This should hopefully close the underlying filedescriptor used for stdout which means next file open will reuse that descriptor.