Automated AVI encoding with Python
Posted: Mon Dec 10, 2007 11:29 am
Hi, in few hours i wrote this simple OOscript for automate the video making of ZM's event images and purge from DB and filesystem too. Sorry if prints are in italian! Soon i'll make it with locales. The usage is very simple, like this examples:
./ZMmakemovie --camera 14 --start-time '2007-12-10 9:00:00' --end-time '200-12-12 9:00:00' --remove
This will make a 'two days avi' and after that it will purge the processed images. Its very simple and u'll find a fluffy mencoder command, someone certainly can suggest me something best. If u don't want purge them omit the --remove option.
my mail is
peppelinux@yahoo.it
i'll make a cgi too, i hope to interpolate it fine with the ZM authentication.
My english is fluffy too, brindisi
./ZMmakemovie --camera 14 --start-time '2007-12-10 9:00:00' --end-time '200-12-12 9:00:00' --remove
This will make a 'two days avi' and after that it will purge the processed images. Its very simple and u'll find a fluffy mencoder command, someone certainly can suggest me something best. If u don't want purge them omit the --remove option.
my mail is
peppelinux@yahoo.it
i'll make a cgi too, i hope to interpolate it fine with the ZM authentication.
My english is fluffy too, brindisi
Code: Select all
#!/usr/bin/env python
import sys
import os
import MySQLdb as mysql
import string
import time
IMG_DIR = '/var/lib/zm/events/'
MENCODER_CMD = ''
STORE_DIR = '/home/executive/VideoRec/' ### SET YOUR OWN DIR
DB_HOST = 'localhost'
DB_NAME = 'zm'
DB_USER = 'zm'
DB_PASSWD = 'zm_pass'
class Registrazione:
def __init__(self, numero, starttime, endtime, purge=0):
os.chdir(IMG_DIR)
self.camera = numero
self.durata = [starttime, endtime]
self.ideventi = []
self.purge = purge
try:
conn = mysql.connect (host=DB_HOST, user=DB_USER, passwd=DB_PASSWD, db=DB_NAME)
except mysql.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
cursor = conn.cursor ()
cursor.execute("""
SELECT Id, MonitorId, Name, StartTime, EndTime
FROM Events WHERE MonitorId =%s AND StartTime >=%s
AND EndTime <=%s ORDER BY MonitorId
""", (self.camera, self.durata[0], self.durata[1]))
dbevents = cursor.fetchall()
conn.close()
if not dbevents:
print 'la ricerca non ha prodotto alcun risultato, probabilmente hai sbagliato data o id'
sys.exit()
else:
self.ideventi = []
cnt = 0
for tuples in dbevents:
self.ideventi.append(str(dbevents[cnt][0]))
cnt = cnt + 1
targetid = IMG_DIR + self.camera + '/'
print 'Creo la lista per la camera', self.camera
paths = []
fi = '/tmp/Camera'+self.camera+' '+self.durata[0]+self.durata[1]+'.txt'
nomefile = fi.replace(' ', '_')
self.nomefile = nomefile
f = open(nomefile, 'w')
for i in self.ideventi:
self.path = targetid + i + '/'
for name in os.listdir(self.path):
fullpath = os.path.join(self.path,name)
paths.append(fullpath)
f.writelines(fullpath + '\n')
self.immaggini = paths
f.close()
def makecodings(self):
outputf = STORE_DIR + self.nomefile[5:49]
start = time.strftime('%H:%M:%S')
comando = "mencoder mf://@%s -mf w=640:h=480:fps=2:type=jpeg -vf yadif=0:1 -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:aspect=4/3 -quiet -o %s.avi" % (self.nomefile, outputf)
os.system(comando)
end = time.strftime('%H:%M:%S')
print 'eseguito alle %s terminato alle %s' % (start, end)
if self.purge:
try:
conn = mysql.connect (host='localhost', user='executive', passwd='zm_pass', db='zm')
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
cursor = conn.cursor ()
print 'rimuovo adesso le immaggini dalla base dati'
cursor.execute("""
DELETE FROM Events WHERE MonitorId =%s AND StartTime >=%s
AND EndTime <=%s""", (self.camera, self.durata[0], self.durata[1]))
conn.close()
print 'rimuovo ora le immaggini dal filesystem'
f = open(self.nomefile, 'r')
removals = f.readlines()
cnt = len(self.path)
for i in removals:
os.system('rm -rf %s ' % i[0:cnt])
f.close()
os.remove(self.nomefile)
import optparse
usage = 'usage: %prog [options] arg'
parser = optparse.OptionParser(usage)
parser.add_option("-c", "--camera", dest="camera", help="choose the camera id that you want record. This must be a rational number")
parser.add_option("-s", "--start-time", dest="start", nargs=1, help="That's the starttime, when the record starts. The format must be 'YYYY-MM-DD HH:MM:SS' like 2007-12-3 9:00:00")
parser.add_option("-e", "--end-time", dest="end", help="That's the endtime, when the record ends. The format must be the same of starttime")
parser.add_option("--remove", action="store_true", dest="remove", help="This will remove the images after theirs encoding in avi")
(options, args) = parser.parse_args() #(sys.argv[1:])
if options.camera and options.start and options.end:
if not options.remove:
ID = Registrazione(options.camera, options.start, options.end)
ID.makecodings()
else:
ID = Registrazione(options.camera, options.start, options.end, options.remove)
ID.makecodings()
else:
print 'non si possono omettere le opzioni:\n -c, --camera\n -s, --start-time\n -e, --end-time'
print 'puoi rimuovere le immaggini dopo aver creato il filmato con il comando --remove'