Script to Compile Videos from JPGs Uploaded from Low-End IP Camera DLINK DCS-932L

This script uses ImageMagick and AVConc (ffmpeg) to convert images uploaded by a low-end DLINK internet camera to an FTP server. To set up the system on a linux box, create a new user, camera, and then install vsftpd imagemagick and ffmpeg.

apt-get install vsftpd imagemagick ffmpeg

I won't go into setting up vsftpd. It's not that hard.

The D-LINK DCS-932L can be set up to upload images via ftp. I set it to upload images when the motion detection feature sees something. Set the uploader to name the files with the date and time stamp. I set the prefix to "cam1" and the path to /home/camera/camera. You can alter these in the attached script to match your settings.

Copy the attached script mkvd.py, somewhere, and then run it in your cron every night. The script will scan all the directories in the path you specify, and any that look like a D-Link directory will be scanned for a video file. If there's no video file, it'll create one.

Excuse the crudeness of this code. I'm a python noob.

Also attached is a sample video created at night under relatively low-light conditions. I don't think this camera really works for this purpose.

#! /usr/bin/python3
# vim: set ts=2 sw=2 ai:

import os,re
import datetime
from datetime import datetime


def makevideo( path, prefix, year, month, day, hh ):
	vidname = year+'-'+month+'-'+day+'-'+hh+'.avi'

	# if the video file exists, don't make it
	if os.path.exists( path + vidname ):
		return
	
	try:
		os.mkdir( path + 'work' )
	except OSError:
		pass

	dirc = os.listdir( path )
	dirc.sort()

	i = 0
	for d in dirc:
		if ( d.startswith( prefix ) ):
			dt = d[len(prefix):-4]
			year = dt[0:4]
			month = dt[4:6]
			day = dt[6:8]
			hh = dt[8:10]
			mm = dt[10:12]
			ss = dt[12:14]
			frame = dt[14:16]
			stamp = year + '/' + month + '/' + day + ' ' + hh + ':' + mm + ':' + ss + '.' + frame
			print(d + ' -> work/' + ( "%04d" % (i) )  + '.jpg ' + stamp )
			os.system("convert "+path+d+" label:'"+stamp+"' -gravity Center -append "+path+"work/"+("%04d" % (i))+".jpg")
			i = i + 1

	os.chdir( path )
	os.system("avconv -f image2 -framerate 25 -r 3 -i work/%04d.jpg "+vidname)
	os.system("rm -rf work")


prefix = 'cam1'
path = '/home/camera/camera/'
dirc = os.listdir( path )
dirc.sort()
for d in dirc:
	if ( re.match( '^\d{8}$', d ) ):
		year = d[0:4]
		month = d[4:6]
		day = d[6:8]
		dird = os.listdir( path + d )
		dird.sort()
		for e in dird:
			if ( re.match( '^\d{4}$', e ) ):
				hh = e[0:2]
				# don't call the makevideo function if the current date matches the video's date and hour
				# we want to avoid making a video prematurely, before all the frames for the hour are saved
				tt = datetime.now().timetuple()
				if (year == ( '%04d' % tt.tm_year) and \
						month == ( '%02d' % tt.tm_mon ) and \
						day == ( '%02d' % tt.tm_mday ) and \
						hh == ( '%02d' % tt.tm_hour ) ):
							pass
				else:
					makevideo( path + d + '/' + e + '/', prefix, year, month, day, hh )
AttachmentSize
mkvd.py_.txt1.76 KB
2013-09-03-04.avi7.51 MB