Prevent OpenCV function CreateVideoWriter from printing to console in Python
Question: I'm using the Python bindings for OpenCV and have run into a little annoyance
using CreateVideoWriter where when I call the function, it prints something
similar to the below to the console and I can't seem to surpress it or ideally
redirect it into a variable.

    
    
    Output #0, avi, to 'temp/Temp.0433.avi':
        Stream #0.0: Video: mjpeg, yuvj420p, 320x240, q=2-31, 9830 kb/s, 90k tbn, 25
     tbc
    

The command I'm using for testing is this:

    
    
    self.file = cvCreateVideoWriter(nf,CV_FOURCC('M','J','P','G'),self.fps,cvSize(320,240),1)
    

Although in the long run this app will have a control GUI its currently
console based, the function is called every minute so this means its difficult
to present even a simple menu or more useful status information without this
call filling up the console.

Just wondering if anyone has experienced the same and/or has any ideas how I
might be able to prevent this happening or can offer pointers as to what I'm
doing wrong?


Answer: I think the easiest way for you to do this is temporarily to redirect
`sys.stdout` while calling the messy function -- anything else will force you
to change the Python bindings.

Fortunately, this is easy: just use a `contextmanager`:

    
    
    >>> import contextlib
    >>> @contextlib.contextmanager
    ... def stdout_as(stream):
    ...     import sys
    ...     sys.stdout = stream
    ...     yield
    ...     sys.stdout = sys.__stdout__
    ...
    >>> print("hi")
    hi
    >>> import io
    >>> stream = io.StringIO()
    >>> with stdout_as(stream):
    ...     print("hi")
    ...
    >>> stream.seek(0)
    0
    >>> stream.read()
    'hi\n'
    >>> print("hi")
    hi
    

