rtsp python server_python rtsp

这是一个Python代码示例,演示如何连接到具有RTSP的IP摄像头,从相机中提取RTP/NAL/H264包,并将它们写入可由标准视频播放器读取的文件。代码用于教育/演示目的,不考虑效率。
摘要由CSDN通过智能技术生成

# -*- 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+')

ms

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值