elif recording_mode == 'continuous':
data['emulate_motion'] = True
- if proto == 'v4l2':
- max_val = data['width'] * data['height'] * data['framerate']
-
- else: # assume a netcam image size of 640x480, since we have no means to know it at this point
- max_val = 640 * 480 * data['framerate']
-
- max_val = min(max_val, 9999999)
+ data['ffmpeg_video_codec'] = ui['movie_format']
+ q = int(ui['movie_quality'])
+ if data['ffmpeg_video_codec'] in _EXPONENTIAL_QUALITY_CODECS:
+ vbr = max(1, _MAX_FFMPEG_VARIABLE_BITRATE * (1 - math.log(max(1, q * _EXPONENTIAL_QUALITY_FACTOR), _EXPONENTIAL_QUALITY_FACTOR * 100)))
+
+ else:
+ vbr = 1 + (_MAX_FFMPEG_VARIABLE_BITRATE - 1) / 100.0 * (100 - q)
- data['ffmpeg_bps'] = int(int(ui['movie_quality']) * max_val / 100)
+ data['ffmpeg_variable_bitrate'] = int(vbr)
+ # motion detection
+ if ui['mask']:
+ if ui['mask_type'] == 'smart':
+ data['smart_mask_speed'] = 10 - int(ui['smart_mask_slugginess'])
+
+ elif ui['mask_type'] == 'editable':
+ data['mask_file'] = utils.build_editable_mask_file(ui['editable_mask'])
+
+ else:
+ data['mask_file'] = ui['mask_file']
+
# working schedule
if ui['working_schedule']:
data['@working_schedule'] = (
import os
import re
import socket
+import struct
+ import sys
import time
import urllib
+ import urllib2
import urlparse
+from PIL import Image, ImageDraw
+
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.iostream import IOStream
from tornado.ioloop import IOLoop
return 'Digest %s' % (base)
+ def urlopen(*args, **kwargs):
+ if sys.version_info >= (2, 7, 9) and not settings.VALIDATE_CERTS:
+ # ssl certs are not verified by default
+ # in versions prior to 2.7.9
+
+ import ssl
+
+ ctx = ssl.create_default_context()
+ ctx.check_hostname = False
+ ctx.verify_mode = ssl.CERT_NONE
+
+ kwargs.setdefault('context', ctx)
+
+ return urllib2.urlopen(*args, **kwargs)
++
++
+def build_editable_mask_file(editable_mask):
+ width = editable_mask[0]
+ height = editable_mask[1]
+ nx = editable_mask[2]
+ ny = editable_mask[3]
+ lines = editable_mask[4:]
+
+ data = struct.pack('<HHBB', width, height, nx, ny)
+ for line in lines:
+ data += struct.pack('<I', line)
+
+ name = base64.b64encode(data, '-_')
+
+ # draw the actual mask image content
+ im = Image.new('L', (width, height), 255) # all white
+ dr = ImageDraw.Draw(im)
+
+ rw = width / nx
+ rh = height / ny
+
+ for y in xrange(ny):
+ line = lines[y]
+ for x in xrange(nx):
+ if line & (31 - x):
+ print line & (31 - x)
+ dr.rectangle((x * rw, y * rh, (x + 1) * rw, (y + 1) * rh), fill=0)
+
+ file_name = os.path.join(settings.CONF_PATH, name) + '.pgm'
+ im.save(file_name, 'ppm')
+
+ return name
+
+
+def parse_editable_mask_file(file_name):
+ name = os.path.splitext(os.path.basename(file_name))[0]
+ try:
+ data = base64.b64decode(name, '-_')
+ width, height, nx, ny = struct.unpack('<HHBB', data[:6])
+ fmt = '<' + 'I' * ny
+ lines = struct.unpack(fmt, data[6:])
+
+ return [width, height, nx, ny] + list(lines)
+
+ except Exception:
+ return None