Thanks to visit codestin.com
Credit goes to github.com

Skip to content

README template or just USAGE example #8

@d34db33f-1007

Description

@d34db33f-1007

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions