Solved using FFmpeg and FFserver. Note FFserver only works on Linux. The solution uses python code from here as suggested by Ryan.
Flow is as follows -
- Start FFserver background process using the desired configuration (mjpeg in this case).
- FFmpeg input is the mmst stream, output stream to localhost.
- Run python script to open the localhost stream and decode frame by frame.
Run FFserver
ffserver -d -f /etc/ffserver.conf
On a second terminal run FFmpeg
ffmpeg -i mmst://194.90.203.111/cam2 http://localhost:8090/cam2.ffm
The Python code. In this case, the code will open a window with the video stream.
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>
Note that this ffserver.config needs more fine tuning, but they work rather well and produce a frame very close to source with only a little frame freeze.
Answer from NoamR on Stack OverflowYou can simply save the grabbed frames into images:
camera = cv2.VideoCapture(0)
i = 0
while True:
f,img = camera.read()
cv2.imshow("webcam",img)
if (cv2.waitKey(5) != -1):
break
cv2.imwrite('{0:05d}.jpg'.format(i),img)
i += 1
or to a video like this:
camera = cv2.VideoCapture(0)
video = cv2.VideoWriter('video.avi', -1, 25, (640, 480));
while True:
f,img = camera.read()
video.write(img)
cv2.imshow("webcam",img)
if (cv2.waitKey(5) != -1):
break
video.release()
When creating VideoWriter object, you need to provide several parameters that you can extract from the input stream. A tutorial can be found here.
In ubuntu create video from given pictures using following code
os.system('ffmpeg -f image2 -r 8 -i %05d.bmp -vcodec mpeg4 -y movie3.mp4')
where name of picture is 00000.bmp,00001.bmp,00002.bmp etc..