-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.py
76 lines (60 loc) · 2.14 KB
/
stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import httplib
import urlparse
class MJPEGClient:
"""An iterable producing JPEG-encoded video frames from an MJPEG stream URL."""
def __init__(self, url):
self._url = urlparse.urlparse(url)
h = httplib.HTTP(self._url.netloc)
h.putrequest('GET', self._url.path)
h.endheaders()
errcode, errmsg, headers = h.getreply()
if errcode == 200:
ndx = headers["content-type"].find('=') + 1
self._boundary = headers["content-type"][ndx:]
self._fh = h.getfile()
else:
raise RuntimeError("HTTP %d: %s" % (errcode, errmsg))
def __iter__(self):
"""Yields JPEG-encoded video frames."""
# TODO: handle chunked encoding delimited by marker instead
# of content-length.
b = "--" + self._boundary
print "boundary: %s" % b
state = 1
data = ""
while True:
length = None
while True:
line = self._fh.readline()
if state == 1 and line == "\r\n":
state = 2
elif state == 1:
if line.startswith("Content-Type: "):
type = line.split(" ")[1].strip()
print "%s" % type
elif state == 2:
ndx = line.find(b)
if ndx == -1:
data = data + line
else:
data = data + line[0:ndx]
state = 1
break
yield data
data = ""
if __name__ == "__main__":
from PIL import Image
import StringIO
import sys
if len(sys.argv) != 3:
print >> sys.stderr, "Usage: %s http://camera.example.com/mjpegstream outputfile" % sys.argv[0]
raise SystemExit
i = 1
for jpegdata in MJPEGClient(sys.argv[1]):
frame = Image.open(StringIO.StringIO(jpegdata))
frame.save("%s-%d.jpeg" % (sys.argv[2], i))
print "%d bytes, %dx%d pixels" % (
len(jpegdata), frame.size[0], frame.size[1])
i += 1
if i == 81:
break