dvrip icon indicating copy to clipboard operation
dvrip copied to clipboard

README template or just USAGE example

Open d34db33f-1007 opened this issue 4 years ago • 2 comments

hello! i've wrote example code of core features from this project, so you can just use it as a wiki. the snapshoting function now relies on PyAV library which costs additional 45MB when code compiled, but i'm in progress of writing my own h264 decoder in python3, which i would be glad to share either.

import socket
import traceback

from io import BytesIO
from PIL import Image
from av.packet import Packet
from av.codec.context import CodecContext

from dvrip.monitor import Stream
from dvrip.io import DVRIPClient


Status = {
		None: 0,
		'Wrong password': -1,
		'Banned': 2
	}

class XMEye:
	def __init__(self, ip=None, port=None):
		self.ip = ip
		self.port = port
		self.login = None
		self.password = None

	def auth(self, login, password):
		global Status

		self.Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.Socket.settimeout(4)
		self.conn = DVRIPClient(self.Socket)
		log_in = self.conn.connect((self.ip, self.port), login, password)
		return Status[log_in]

	def get_snapshot(self, ch):
		self.Socket2 = socket.create_connection((self.ip, self.port), 11)
		h264 = self.conn.monitor(self.Socket2, channel=ch, stream=Stream.HD)

		data = b''
		while True:
			if chunk := h264.read(1024):
				data += chunk
			if len(chunk) < 1024:
				break

		self.codec_264 = CodecContext.create("h264", "r")
		frame = self.codec_264.decode(Packet(data)):
#		frame[0].to_image().save(f'frame_{frame.index}.jpg')
		jpeg = BytesIO()
		frame[0].to_image().save(jpeg, format='JPEG')
		if ch == self.channels:
			self.conn.logout()
			self.Socket2.close()
		return jpeg.getvalue()

	def sys_info(self):
		info = self.conn.systeminfo()
		self.channels_count = int(info.videoin)
		self.model = f'{info.chassis}_{info.board}'
		try:
			ptz = self.conn.button(channel=0, button=PTZButton.MENU)
		except DVRIPRequestError:
			ptz = None
		if int(info.audioin) > 0 and ptz is None:
			self.model = f'{self.model}-Sound'
		elif ptz is not None:
			self.model = f'{self.model}-PTZ'
		return

#	def debug(self):
#		self.conn.keepalive()

d34db33f-1007 avatar Jul 06 '20 02:07 d34db33f-1007

UPD: https://github.com/MasterNobody/x264 lightweight h264 decoder implementation which builds just in 2Mb

d34db33f-1007 avatar Jul 09 '20 13:07 d34db33f-1007

UPD2: compiling custom version of ffmpeg and PyAV makes compiled binary about 8Mb. Makefile flags for ffmpeg:


./configure --enable-pic --disable-debug --enable-shared --disable-decoders --enable-decoder=h264 \ 
--disable-programs --disable-logging --disable-protocols --disable-avfoundation --disable-appkit \ 
--disable-coreimage --disable-iconv --disable-securetransport --disable-schannel \ 
--disable-safe-bitstream-reader --disable-large-tests --disable-doc --disable-postproc \ 
--disable-network --disable-encoders --disable-muxers --disable-parsers --enable-parser=h264 \ 
--enable-small --disable-runtime-cpudetect --disable-hwaccels --disable-demuxers --disable-devices

	# size in cost of perfomance:
		# --enable-small --disable-runtime-cpudetect --disable-hwaccels --disable-safe-bitstream-reader

d34db33f-1007 avatar Jul 22 '20 20:07 d34db33f-1007