Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

README template or just USAGE example #8

Open
d34db33f-1007 opened this issue Jul 6, 2020 · 2 comments
Open

README template or just USAGE example #8

d34db33f-1007 opened this issue Jul 6, 2020 · 2 comments

Comments

@d34db33f-1007
Copy link

d34db33f-1007 commented Jul 6, 2020

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
Copy link
Author

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

@d34db33f-1007
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant