Stop a function from writing to stdout

后端 未结 2 1343
清歌不尽
清歌不尽 2021-01-24 15:31

I have this line in my code:

writer = cv.CreateVideoWriter(\'video.avi\', cv.CV_FOURCC(\'X\',\'V\',\'I\',\'D\'), 30 ,(480,800), 1)

Which output

相关标签:
2条回答
  • 2021-01-24 16:12

    It's not OpenCV that outputs this info. It's FFMPEG (which is called by OpenCV).

    You can recompile FFMPEG to disable these debugs.

    0 讨论(0)
  • 2021-01-24 16:29
    import os
    import sys
    saved_stdout, saved_stderr = sys.stdout, sys.stderr
    sys.stdout = sys.stderr = open(os.devnull, "w")
    writer = cv.CreateVideoWriter('video.avi', cv.CV_FOURCC('X','V','I','D'), 30 ,(480,800), 1)
    sys.stdout, sys.stderr = saved_stdout, saved_stderr
    print 'Now this will print to stdout'
    

    Demo:

    >>> import os
    >>> import sys
    >>> saved_stdout, saved_stderr = sys.stdout, sys.stderr
    >>> sys.stdout = sys.stderr = open(os.devnull, "w")
    >>> print 'hello world'                     # no output
    >>> sys.stderr.write('Invisible Error!\n')  # no ouput
    >>> sys.stdout, sys.stderr = saved_stdout, saved_stderr
    >>> print 'goodbye world'                   
    goodbye world
    >>> sys.stderr.write('Error!\n')
    Error!
    
    0 讨论(0)
提交回复
热议问题