Skip to content

Instantly share code, notes, and snippets.

@danielkucera
Created February 11, 2019 10:58
Show Gist options
  • Save danielkucera/0a2f36bc53959e4879cb567149aafb78 to your computer and use it in GitHub Desktop.
Save danielkucera/0a2f36bc53959e4879cb567149aafb78 to your computer and use it in GitHub Desktop.
./recvlkv373.py 226.2.2.2 2068 out.mjpeg
#!/usr/bin/env python
import socket
import binascii
import sys
def main():
MCAST_GRP = sys.argv[1]
MCAST_PORT = int(sys.argv[2])
f = open(sys.argv[3], 'w')
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except AttributeError:
pass
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
sock.bind((MCAST_GRP, MCAST_PORT))
host = socket.gethostbyname('0.0.0.0')
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(host))
sock.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(MCAST_GRP) + socket.inet_aton(host))
startoutput=0
buf = bytearray(10*1024*1024)
while 1:
try:
data, addr = sock.recvfrom(1316)
except socket.error, e:
print 'Expection'
frame_n=ord(data[0])*256+ord(data[1])
part=(ord(data[2])&0x7f)*256+ord(data[3])
end=ord(data[2])&0x80 > 0
frdat = data[4:]
offset = part*1020
buf[offset:offset+len(frdat)] = frdat
if part == 0:
#f = open('out/'+str(frame_n)+"_"+str(part).zfill(3)+'.jpg', 'w')
startoutput=1
if startoutput==1:
sys.stderr.write("%d,%d,%d:%s\r" % ( frame_n, part, len(data), data[:20].encode("hex")))
if end:
f.write("\r\n\r\n--myboundary\r\nContent-Type: image/jpeg\r\n\r\n")
f.write(buf[:offset+len(frdat)])
pass
#sys.stderr.write("---END---\n")
#f.write(data[4:])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment