设为首页 收藏本站
查看: 668|回复: 0

[经验分享] python rtsp-Bad

[复制链接]

尚未签到

发表于 2018-8-6 06:32:52 | 显示全部楼层 |阅读模式
# -*- coding: utf-8 -*-  
"""
  
A demo python code that ..
  
1) Connects to an IP cam with RTSP
  
2) Draws RTP/NAL/H264 packets from the camera
  
3) Writes them to a file that can be read with any stock video player (say, mplayer, vlc & other ffmpeg based video-players)
  
Done for educative/demonstrative purposes, not for efficiency..!
  
written 2015 by Sampsa Riikonen.
  
"""
  
import socket
  
import re
  
import bitstring # if you don't have this from your linux distro, install with "pip install bitstring"
  
# ************************ FOR QUICK-TESTING EDIT THIS AREA *********************************************************
  
ip="192.168.1.74" # IP address of your cam
  
port=1616
  
adr="rtsp://admin:12345@192.168.1.74" # username, passwd, etc.
  
clientports=[60784,60785] # the client ports we are going to use for receiving video
  
fname="stream.h264" # filename for dumping the stream
  
rn=5000 # receive this many packets
  
# After running this program, you can try your file defined in fname with "vlc fname" or "mplayer fname" from the command line
  
# you might also want to install h264bitstream to analyze your h264 file
  
# *******************************************************************************************************************
  
dest="DESCRIBE "+adr+" RTSP/1.0\r\nCSeq: 2\r\nUser-Agent: python\r\nAccept: application/sdp\r\n\r\n"
  
setu="SETUP "+adr+"/trackID=1 RTSP/1.0\r\nCSeq: 3\r\nUser-Agent: python\r\nTransport: RTP/AVP;unicast;client_port="+str(clientports[0])+"-"+str(clientports[1])+"\r\n\r\n"
  
play="PLAY "+adr+" RTSP/1.0\r\nCSeq: 5\r\nUser-Agent: python\r\nSession: SESID\r\nRange: npt=0.000-\r\n\r\n"
  
# File organized as follows:
  
# 1) Strings manipulation routines
  
# 2) RTP stream handling routine
  
# 3) Main program
  
# *** (1) First, some string searching/manipulation for handling the rtsp strings ***
  
def getPorts(searchst,st):
  
      """ Searching port numbers from rtsp strings using regular expressions
  
      """
  
      pat=re.compile(searchst+"=\d*-\d*")
  
      pat2=re.compile('\d+')
  
      mstring=pat.findall(st)[0] # matched string .. "client_port=1000-1001"
  
      nums=pat2.findall(mstring)
  
      numas=[]
  
      for num in nums:
  
        numas.append(int(num))
  
      return numas
  
def getLength(st):
  
      """ Searching "content-length" from rtsp strings using regular expressions
  
      """
  
      pat=re.compile("Content-Length: \d*")
  
      pat2=re.compile('\d+')
  
      mstring=pat.findall(st)[0] # matched string.. "Content-Length: 614"
  
      num=int(pat2.findall(mstring)[0])
  
      return num
  
def printrec(recst):
  
      """ Pretty-printing rtsp strings
  
      """
  
      recs=recst.split('\r\n')
  
      for rec in recs:
  
        print (rec)
  
def sessionid(recst):
  
      """ Search session id from rtsp strings
  
      """
  
      recs=recst.split('\r\n')
  
      for rec in recs:
  
        ss=rec.split()
  
        # #print ">",ss
  
        if (ss[0].strip()=="Session:"):
  
          return int(ss[1].split(";")[0].strip())
  
def setsesid(recst,idn):
  
      """ Sets session id in an rtsp string
  
      """
  
      return recst.replace("SESID",str(idn))
  

  
# ********* (2) The routine for handling the RTP stream ***********
  
def digestpacket(st):
  
      """ This routine takes a UDP packet, i.e. a string of bytes and ..
  
      (a) strips off the RTP header
  
      (b) adds NAL "stamps" to the packets, so that they are recognized as NAL's
  
      (c) Concantenates frames
  
      (d) Returns a packet that can be written to disk as such and that is recognized by stock media players as h264 stream
  
      """
  
      startbytes="\x00\x00\x00\x01" # this is the sequence of four bytes that identifies a NAL packet.. must be in front of every NAL packet.
  

  
      bt=bitstring.BitArray(bytes=st) # turn the whole string-of-bytes packet into a string of bits.  Very unefficient, but hey, this is only for demoing.
  
      lc=12 # bytecounter
  
      bc=12*8 # bitcounter
  

  
      version=bt[0:2].uint # version
  
      p=bt[3] # P
  
      x=bt[4] # X
  
      cc=bt[4:8].uint # CC
  
      m=bt[9] # M
  
      pt=bt[9:16].uint # PT
  
      sn=bt[16:32].uint # sequence number
  
      timestamp=bt[32:64].uint # timestamp
  
      ***c=bt[64:96].uint # ***c identifier
  
      # The header format can be found from:
  
      # https://en.wikipedia.org/wiki/Real-time_Transport_Protocol
  

  
      lc=12 # so, we have red twelve bytes
  
      bc=12*8 # .. and that many bits
  

  
      #print "version, p, x, cc, m, pt",version,p,x,cc,m,pt
  
      #print "sequence number, timestamp",sn,timestamp
  
      #print "sync. source identifier",***c
  

  
      # st=f.read(4*cc) # csrc identifiers, 32 bits (4 bytes) each
  
      cids=[]
  
      for i in range(cc):
  
        cids.append(bt[bc:bc+32].uint)
  
        bc+=32; lc+=4;
  
      #print "csrc identifiers:",cids
  

  
      if (x):
  
        # this section haven't been tested.. might fail
  
        hid=bt[bc:bc+16].uint
  
        bc+=16; lc+=2;
  

  
        hlen=bt[bc:bc+16].uint
  
        bc+=16; lc+=2;
  

  
        #print "ext. header id, header len",hid,hlen
  

  
        hst=bt[bc:bc+32*hlen]
  
        bc+=32*hlen; lc+=4*hlen;
  

  

  
      # OK, now we enter the NAL packet, as described here:
  
      #
  
      # https://tools.ietf.org/html/rfc6184#section-1.3
  
      #
  
      # Some quotes from that document:
  
      #
  
      """
  
      5.3. NAL Unit Header Usage
  

  

  
      The structure and semantics of the NAL unit header were introduced in
  
      Section 1.3.  For convenience, the format of the NAL unit header is
  
      reprinted below:
  

  
          +---------------+
  
          |0|1|2|3|4|5|6|7|
  
          +-+-+-+-+-+-+-+-+
  
          |F|NRI|  Type   |
  
          +---------------+
  

  
      This section specifies the semantics of F and NRI according to this
  
      specification.
  

  
      """
  
      """
  
      Table 3.  Summary of allowed NAL unit types for each packetization
  
                    mode (yes = allowed, no = disallowed, ig = ignore)
  

  
          Payload Packet    Single NAL    Non-Interleaved    Interleaved
  
          Type    Type      Unit Mode           Mode             Mode
  
          -------------------------------------------------------------
  
          0      reserved      ig               ig               ig
  
          1-23   NAL unit     yes              yes               no
  
          24     STAP-A        no              yes               no
  
          25     STAP-B        no               no              yes
  
          26     MTAP16        no               no              yes
  
          27     MTAP24        no               no              yes
  
          28     FU-A          no              yes              yes
  
          29     FU-B          no               no              yes
  
          30-31  reserved      ig               ig               ig
  
      """
  
      # This was also very usefull:
  
      # http://stackoverflow.com/questions/7665217/how-to-process-raw-udp-packets-so-that-they-can-be-decoded-by-a-decoder-filter-i
  
      # A quote from that:
  
      """
  
      First byte:  [ 3 NAL UNIT BITS | 5 FRAGMENT TYPE BITS]
  
      Second byte: [ START BIT | RESERVED BIT | END BIT | 5 NAL UNIT BITS]
  
      Other bytes: [... VIDEO FRAGMENT DATA...]
  
      """
  

  
      fb=bt[bc] # i.e. "F"
  
      nri=bt[bc+1:bc+3].uint # "NRI"
  
      nlu0=bt[bc:bc+3] # "3 NAL UNIT BITS" (i.e. [F | NRI])
  
      typ=bt[bc+3:bc+8].uint # "Type"
  
      #print "F, NRI, Type :", fb, nri, typ
  
      #print "first three bits together :",bt[bc:bc+3]
  

  
      if (typ==7 or typ==8):
  
        # this means we have either an SPS or a PPS packet
  
        # they have the meta-info about resolution, etc.
  
        # more reading for example here:
  
        # http://www.cardinalpeak.com/blog/the-h-264-sequence-parameter-set/
  
        if (typ==7):
  
          print (">>>>> SPS packet")
  
        else:
  
          print (">>>>> PPS packet")
  
        return startbytes+st[lc:]
  
        # .. notice here that we include the NAL starting sequence "startbytes" and the "First byte"
  

  
      bc+=8; lc+=1; # let's go to "Second byte"
  
      # ********* WE ARE AT THE "Second byte" ************
  
      # The "Type" here is most likely 28, i.e. "FU-A"
  
      start=bt[bc] # start bit
  
      end=bt[bc+2] # end bit
  
      nlu1=bt[bc+3:bc+8] # 5 nal unit bits
  

  
      if (start): # OK, this is a first fragment in a movie frame
  
        #print ">>> first fragment found"
  
        nlu=nlu0+nlu1 # Create "[3 NAL UNIT BITS | 5 NAL UNIT BITS]"
  
        head=startbytes+nlu.bytes # .. add the NAL starting sequence
  
        lc+=1 # We skip the "Second byte"
  
      if (start==False and end==False): # intermediate fragment in a sequence, just dump "VIDEO FRAGMENT DATA"
  
        head=""
  
        lc+=1 # We skip the "Second byte"
  
      elif (end==True): # last fragment in a sequence, just dump "VIDEO FRAGMENT DATA"
  
        head=""
  
        #print "<<<< last fragment found"
  
        lc+=1 # We skip the "Second byte"
  

  
      if (typ==28): # This code only handles "Type" = 28, i.e. "FU-A"
  
        return head+st[lc:]
  
      else:
  
        raise(Exception,"unknown frame type for this piece of s***")
  
# *********** (3) THE MAIN PROGRAM STARTS HERE ****************
  
# Create an TCP socket for RTSP communication
  
# further reading:
  
# https://docs.python.org/2.7/howto/sockets.html
  
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
s.connect((ip,port)) # RTSP should peek out from port 1616
  
s.send(dest)
  
recst=s.recv(4096)
  
printrec(recst)
  
s.send(setu)
  
recst=s.recv(4096)
  
printrec(recst)
  
idn=sessionid(recst)
  
serverports=getPorts("server_port",recst)
  
clientports=getPorts("client_port",recst)
  
print ("ip,serverports",ip,serverports)
  
s1=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  
s1.bind(("", clientports[0])) # we open a port that is visible to the whole internet (the empty string "" takes care of that)
  
s1.settimeout(5) # if the socket is dead for 5 s., its thrown into trash
  
# further reading:
  
# https://wiki.python.org/moin/UdpCommunication
  
# Now our port is open for receiving shitloads of videodata.  Give the camera the PLAY command..
  
print ("*** SENDING PLAY ***")
  
play=setsesid(play,idn)
  
s.send(play)
  
recst=s.recv(4096)
  
printrec(recst)
  
print ("** STRIPPING RTP INFO AND DUMPING INTO FILE **")
  
f=open(fname,'w')
  
for i in range(rn):
  
  recst=s1.recv(4096)
  
  print ("read",len(recst),"bytes")
  
  st=digestpacket(recst)
  
  print ("dumping",len(st),"bytes")
  
  f.write(st)
  
f.close()
  
# Before closing the sockets, we should give the "TEARDOWN" command via RTSP, but I am feeling lazy today (after googling, wireshark-analyzing, among other-things).
  
s.close()
  
s1.close()
  

  

  

  

  

  

  

  
Python-RTSP
  
Implementation of the RTSP protocol on top of the Twisted Python library. — Read more
  
http://odie5533.com/
  
https://github.com/odie5533/Python-RTSP

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-547231-1-1.html 上篇帖子: python操作redis-Python & Golang 学习 下篇帖子: python多线程队列安全
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表