/
plopificator
executable file
·270 lines (215 loc) · 8.46 KB
/
plopificator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/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)