2014-11-01 504 views
5

我想使用Python处理OpenCV中的mms视频流。 流来自IP摄像机,我无法控制(流量监视器)。 流可以作为彩信或MMST方案 - 两个VLC和Windows Media Player如何使用OpenCV捕获视频流(Python)

mms://194.90.203.111/cam2 

戏剧。

mmst://194.90.203.111/cam2 

只适用于VLC。 我试图通过使用FFmpeg和VLC重新流式传输将方案更改为HTTP,但它不起作用。

据我所知,mms使用Windows Media Video来编码流。没有运气在URI的末尾添加'.mjpeg'。我还没有找到什么类型的流被OpenCV接受。

这里是我的代码 -

import cv2, platform 
#import numpy as np 

cam = "mms://194.90.203.111/cam2" 
#cam = 0 # Use local webcam. 

cap = cv2.VideoCapture(cam) 
if not cap: 
    print("!!! Failed VideoCapture: invalid parameter!") 

while(True): 
    # Capture frame-by-frame 
    ret, current_frame = cap.read() 
    if type(current_frame) == type(None): 
     print("!!! Couldn't read frame!") 
     break 

    # Display the resulting frame 
    cv2.imshow('frame',current_frame) 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
     break 

# release the capture 
cap.release() 
cv2.destroyAllWindows() 

的 我缺少什么? OpenCV可以捕获什么类型的视频流? 有没有方案更改或转码的优雅解决方案?

谢谢!

Python ver 2.7.8,OpenCV的ver 2.4.9,两个x86。 Win7 x64

+0

也许这将有助于:http://petrkout.com/electronics/low-latency-0-4-s-video-streaming-from-raspberry-pi-mjpeg-streamer-opencv/在客户部分 – Ryan 2014-11-01 18:21:11

+0

谢谢@Ryan!该链接有很多很好的信息。最后的Python部分对它进行了固定。 – NoamR 2014-11-07 21:04:01

回答

5

使用FFmpeg和FFserver解决。注FFserver仅适用于Linux。 该解决方案使用here的Python代码,如Ryan所示。

流程如下 - 利用期望的配置 (MJPEG在这种情况下)

  • 开始ffserver的后台进程。
  • FFmpeg输入是mmst流,输出流 到localhost。
  • 运行python脚本来打开localhost流,并且逐帧解码。

运行ffserver的

ffserver -d -f /etc/ffserver.conf 

上运行的FFmpeg

ffmpeg -i mmst://194.90.203.111/cam2 http://localhost:8090/cam2.ffm 

的Python代码的第二端子。在这种情况下,代码将打开一个视频流的窗口。

import cv2, platform 
import numpy as np 
import urllib 
import os 

cam2 = "http://localhost:8090/cam2.mjpeg" 

stream=urllib.urlopen(cam2) 
bytes='' 
while True: 
    # to read mjpeg frame - 
    bytes+=stream.read(1024) 
    a = bytes.find('\xff\xd8') 
    b = bytes.find('\xff\xd9') 
    if a!=-1 and b!=-1: 
     jpg = bytes[a:b+2] 
     bytes= bytes[b+2:] 
    frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR) 
    # we now have frame stored in frame. 

    cv2.imshow('cam2',frame) 

    # Press 'q' to quit 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
     break 

cv2.destroyAllWindows() 

ffserver.config -

Port 8090 
BindAddress 0.0.0.0 
MaxClients 10 
MaxBandWidth 50000 
CustomLog - 
#NoDaemon 

<Feed cam2.ffm> 
    File /tmp/cam2.ffm 
    FileMaxSize 1G 
    ACL allow 127.0.0.1 
    ACL allow localhost 
</Feed> 
<Stream cam2.mjpeg> 
    Feed cam2.ffm 
    Format mpjpeg 
    VideoFrameRate 25 
    VideoBitRate 10240 
    VideoBufferSize 20480 
    VideoSize 320x240 
    VideoQMin 3 
    VideoQMax 31 
    NoAudio 
    Strict -1 
</Stream> 
<Stream stat.html> 
    Format status 
    # Only allow local people to get the status 
    ACL allow localhost 
    ACL allow 192.168.0.0 192.168.255.255 
</Stream> 
<Redirect index.html> 
    URL http://www.ffmpeg.org/ 
</Redirect> 

注意,这ffserver.config需要更多的微调,但他们的工作相当出色,并产生非常接近的框架,只有一点点帧冻结源。

+0

我很高兴我找到了这个。感谢分享! – sascha 2017-01-20 23:49:21