You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

270 lines
8.5 KiB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2009 Marc Poulhiès
#
# This file is part of Plopifier.
#
# Plopifier is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plopifier is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plopifier. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import optparse
import time
import tempfile
from datetime import datetime,timedelta
from subprocess import Popen
from threading import Thread,Condition,Semaphore
from plopifier import request
import vimeo
from vimeo import VimeoException, CurlyRestException
#mencoder mf://@filelist.ok
# -mf w=640:h=480:fps=25:type=jpg
# -ovc lavc
# -lavcopts vcodec=mpeg4:mbd=2:trell
# -oac copy
# -o saint-eynard-2008-05-04.avi
def down_sample(list, num):
r = float(len(list))/float(num)
return [list[int(i*r)] for i in xrange(num)]
def build_video (file_list, duration, output_filename, fps=25):
nframe = duration * fps
print "max frame:", nframe, "got :", len(file_list)
if len(file_list) > nframe:
file_list = down_sample(file_list, nframe)
elif len(file_list) < nframe:
print "Not enough images!"
return False
fd,flist = tempfile.mkstemp(prefix="plopificator")
fh = os.fdopen(fd, "w")
fh.write("\n".join(file_list))
fh.write("\n")
fh.close()
cmd = "mencoder mf://@%s " %flist
cmdargs = "-mf w=640:h=480:fps=%d:type=jpg " %fps
cmdargs += "-ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy "
cmdargs += "-really-quiet "
cmd += cmdargs
cmd += "-o " + output_filename
p = Popen(cmd, shell=True)
print "finished with %s" %cmd
sts = os.waitpid(p.pid, 0)
os.unlink(flist)
return True
class Stealer(Thread):
def __init__(self, cond, v, day, tags, file_list, duration, output_filename, fps=25):
Thread.__init__(self)
self.sem_lock = Semaphore()
self.sem_lock.acquire()
self.cond = cond
self.tags = tags
self.day = day
self.v = v
self.file_list = file_list
self.duration = duration
self.output_filename = output_filename
self.fps = fps
def run(self):
build_video(self.file_list, self.duration, self.output_filename, self.fps)
print "BUILDING ", self.output_filename
print "GETTING TICKET"
ticket = "ABCD"
ticket = self.v.videos_getUploadTicket()
print "[%s] Finished building video and ticket [%s] acquired, waiting..." %(self.output_filename, ticket)
self.sem_lock.release()
## self.cond.acquire()
## self.cond.wait()
## self.cond.release()
print "[%s] notified, trying to upload!" % self.output_filename
try :
print "UPLOADING", self.output_filename
self.v.do_upload(self.output_filename,
str(self.day.strftime("%d%m%Y")),
ticket, self.tags)
except VimeoException, e :
print "There was an error with Vimeo:"
print e
def main(argv):
parser = optparse.OptionParser(
usage='Usage: %prog [options]',
description="A cloud movi3 generat0r ")
parser.add_option('-n', '--number',
help="Specify the number of videos to create & upload",
default=10, type="int")
parser.add_option('--prefetch-tickets',
action="store_true", default=False)
# parser.add_option('-l', '--thread-killer',
# help='enable the multi thread ticket stealer',
# action="store_true", default=False)
parser.add_option('-k', '--vimeo-apikey', metavar='api-key',
help='set the "api_key" for vimeo')
parser.add_option('-s', '--vimeo-secret', metavar='api-secret',
help='set the "secret" for vimeo')
parser.add_option('-t', '--vimeo-authtoken', metavar='authtok',
help='set the "auth_token" for vimeo')
parser.add_option('--vimeo-testticket',
action="store_true", default=False,
help='simply tests if an upload ticket can be acquired')
parser.add_option('-d', '--sqldb-file', metavar='FILE',
help='set the sqlite file',
default="/mnt/ft-1/webcam/test.sqlite")
parser.add_option('-r', '--sqldb-root', metavar='PATH',
help='set the root for relative paths in db',
default="/mnt/ft-1/webcam")
parser.add_option('--delta', metavar='TIME',
help='set the time between two sequential beginings')
parser.add_option('-b', '--begin', metavar='DATE',
help='set the start date')
parser.add_option('-e', '--end', metavar='DATE',
help='set the end date')
parser.add_option('-u', '--only-upload', metavar='FILE',
help='only upload FILE to vimeo')
(options, args) = parser.parse_args(argv[1:])
v = vimeo.Vimeo(options.vimeo_apikey,
options.vimeo_secret,
options.vimeo_authtoken)
v.set_userid()
tags = ["SaintEynard", "nuages", "clouds", "timelapse"]
if options.vimeo_testticket:
try:
v.videos_getUploadTicket()
print "Ticket acquired, user can upload video"
return 0
except CurlyRestException,e:
print "Looks like we can't get an upload ticket. The reason is:"
print "%s - %s" %(e.code, e.msg)
return -1
if options.only_upload != None:
v.upload(options.only_upload, "Forced upload", tags)
return
print "use: ", options.sqldb_file, options.sqldb_root
sq = request.Request(options.sqldb_file, options.sqldb_root)
delta = timedelta(1)
start = datetime(2008,8,7)
morning = timedelta(0,0,0,0,00,6)
full_day = timedelta(0,0,0,0,30,16)
today = start
# thread_list = []
# thread_cond = Condition()
tickets = []
if options.prefetch_tickets:
print "Prefetching tickets...",
tickets = [v.videos_getUploadTicket() for x in xrange(options.number)]
print "OK [%d]" %(options.number)
failed_vid = []
for i in xrange(options.number):
bbool = False
while not bbool:
files = sq.get_files(today+morning, today + morning + full_day)
bbool = build_video(files, 110, "/tmp/saint-eynard.avi")
if not bbool:
fstr = "%s->%s [%d]" %(str(today+morning),
str(today + morning + full_day),
len(files))
failed_vid.append(fstr)
print "failed for : ", fstr
today += delta
if options.prefetch_tickets:
print "I have %d tickets. Video index is %d" % (len(tickets), i)
ticket = tickets[0]
tickets = tickets[1:]
else:
ticket = None
try :
v.do_upload("/tmp/saint-eynard.avi", str(today.strftime("%d%m%Y")),
ticket=ticket, tags=tags)
except VimeoException, e :
print "There was an error with Vimeo:"
print e
break
v.process_bug_queue()
today += delta
while len(v.vimeo_bug_queue) > 0:
v.process_bug_queue()
time.sleep(1)
# if options.thread_killer:
# print "Will handler the killers!"
# for t in thread_list:
# print "Get lock...",
# t.sem_lock.acquire()
# print "OK!"
## print "Notifying all...",
## for t in thread_list:
## thread_cond.acquire()
## thread_cond.notifyAll()
## thread_cond.release()
## print "!",
## print " OK!"
# print "ok!"
# for t in thread_list:
# print "TRY JOIN...",
# t.join()
# print "JOINED!"
# else:
# print "not fun, no thread!"
if __name__ == '__main__':
main(sys.argv)