1

我的代码中有这一行:

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

哪个输出来控制这个:

Output #0, avi, to 'video.avi':
Stream #0:0: Video: mpeg4, yuv420p, 480x800, q=2-31, 24576 kb/s, 90k tbn, 30 tbc

我想阻止它打印任何东西到标准输出,但保持我自己在代码中进一步输出的可能性。我怎样才能做到这一点?

我试过这个,但没有奏效。

class NullStream:
    def write(self, text):
        pass

sys.stdout = NullStream()
sys.stderr = NullStream()

print "hello" #This doesn't outputs

#This function still outputs to console:
writer = cv.CreateVideoWriter('video.avi', cv.CV_FOURCC('X','V','I','D'), 30 ,(480,800), 1)

sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
4

2 回答 2

3

输出此信息的不是 OpenCV。它是 FFMPEG(由 OpenCV 调用)。

您可以重新编译 FFMPEG 以禁用这些调试。

于 2012-04-25T18:52:25.077 回答
2
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'

演示:

>>> 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!
于 2012-04-25T18:36:48.370 回答