美文网首页
使用python vlc 读取视频图像帧

使用python vlc 读取视频图像帧

作者: 蓝云风翼 | 来源:发表于2019-01-28 14:11 被阅读0次

    直接上代码:
    import vlc
    import ctypes
    import time
    import sys
    import cv2
    import numpy
    from PIL import Image

    vlcInstance = vlc.Instance()

    m = vlcInstance.media_new("rtsp://admin:admin@172.16.1.60:554/h264/ch1/main/av_stream")

    mp = vlc.libvlc_media_player_new_from_media(m)

    VIDEOWIDTH = 1920
    VIDEOHEIGHT = 1080

    size = VIDEOWIDTH * VIDEOHEIGHT * 4
    buf = (ctypes.c_ubyte * size)()
    buf_p = ctypes.cast(buf, ctypes.c_void_p)

    VideoLockCb = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p))

    @VideoLockCb
    def _lockcb(opaque, planes):
    #print("lock", file=sys.stderr)
    planes[0] = buf_p

    @vlc.CallbackDecorators.VideoDisplayCb
    def _display(opaque, picture):
    img = Image.frombuffer("RGBA", (VIDEOWIDTH, VIDEOHEIGHT), buf, "raw", "BGRA", 0, 1)
    opencvImage = cv2.cvtColor(numpy.array(img), cv2.COLOR_RGB2BGR)
    cv2.imshow('image',opencvImage)
    cv2.waitKey(10)

    vlc.libvlc_video_set_callbacks(mp, _lockcb, None, _display, None)
    mp.video_set_format("BGRA", VIDEOWIDTH, VIDEOHEIGHT, VIDEOWIDTH*4)
    while True:
    mp.play()
    time.sleep(1)

    相关文章

      网友评论

          本文标题:使用python vlc 读取视频图像帧

          本文链接:https://www.haomeiwen.com/subject/cysajqtx.html