Ask Your Question
0

python silent output

asked 2018-06-19 10:43:28 -0600

qle gravatar image

hello,
I'm a beginner with opencv and maybe the solution is obvious but
I devellope a little script in python and i need to do some screenshot on udp stream, all this part is ok but a lot of output pollute my console like

"[hevc @ 0x2ef0580] PPS id out of range: 0"
"[hevc @ 0x2f0cbc0] Could not find ref with POC 23"

Maybe there is some problems in the stream but the stream himself is working and i get my screenshots(and i'm not controling the streams) . so these information are useless and it's distubing for developpement

How to silent them or handle them properly in the python ?

I use VideoCapture to read multicast streams

Best regards

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-06-20 08:06:22 -0600

qle gravatar image

updated 2018-06-25 04:54:26 -0600

hello yes it seems that the only way is recompile ffmpeg with -disable-logging

There is a python solution to silent it: the idea is to redirect the sterr (where ffmpeg library push the log) "elsewhere" only when this function is executed:
I use the way explain by this very good blog article

 `import ctypes
import io
import os
import sys
import tempfile
from contextlib import contextmanager

import cv2

libc = ctypes.CDLL(None)
c_stderr = ctypes.c_void_p.in_dll(libc, 'stderr')


def screenshot(url):
    vidcap = cv2.VideoCapture(url)
    # do stuff in vidcap
    vidcap.release()


@contextmanager
def stderr_redirector(stream):
    original_stderr_fd = sys.stderr.fileno()

    def _redirect_stderr(to_fd):
         libc.fflush(c_stderr)
          sys.stderr.close()
          os.dup2(to_fd, original_stderr_fd)
          sys.stderr = io.TextIOWrapper(os.fdopen(original_stderr_fd, 'wb'))

    saved_stderr_fd = os.dup(original_stderr_fd)
    try:
          tfile = tempfile.TemporaryFile(mode='w+b')
          _redirect_stderr(tfile.fileno())
          yield
          _redirect_stderr(saved_stderr_fd)
          tfile.flush()
          tfile.seek(0, io.SEEK_SET)
          stream.write(tfile.read().decode())
    finally:
          tfile.close()
          os.close(saved_stderr_fd)


url = "udp://224.X.X.X:1234"
f = io.StringIO()
with stderr_redirector(f):
    screenshot(url)

# you can get the ffmpeg log with  f.getvalue() for analyze or just close it
f.close()`
edit flag offensive delete link more

Comments

Hi, I'm getting the following error when trying to use your code: File "D:/Workspaces/workspace-pycharm/test/test.py", line 41, in <module> libc = ctypes.CDLL(None) File "C:\Users\theto\Miniconda3\envs\p3.7imageio\lib\ctypes__init__.py", line 364, in __init__ self._handle = _dlopen(self._name, mode) TypeError: LoadLibrary() argument 1 must be str, not None

What would be the functionality of ctypes.CDLL(None) here? How can it be replaced so this code would work? Thanks in advance!

TomerP gravatar imageTomerP ( 2020-08-02 08:11:58 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-06-19 10:41:37 -0600

Seen: 5,057 times

Last updated: Jun 25 '18