-
Notifications
You must be signed in to change notification settings - Fork 5
/
lapse.py
155 lines (114 loc) · 4.39 KB
/
lapse.py
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
#!/usr/bin/env python
"""
lapse.py
Create a time lapse movie
Copyright 2007 Justin Azoff <justin@bouncybouncy.net>
License:
GPL
"""
import sys
import pygst
pygst.require('0.10')
import gst
import gobject
import time
class LapseElement(gst.Element):
_srcpadtemplate = gst.PadTemplate ("srcpadtemplate",
gst.PAD_SRC,
gst.PAD_ALWAYS,
gst.caps_new_any())
_sinkpadtemplate = gst.PadTemplate ("sinkpadtemplate",
gst.PAD_SINK,
gst.PAD_ALWAYS,
gst.caps_new_any())
def info(self, m):
print m
def __init__(self):
gst.Element.__init__(self)
gst.info('creating pads')
self.srcpad = gst.Pad(self._srcpadtemplate, "src")
self.sinkpad = gst.Pad(self._sinkpadtemplate, "sink")
gst.info('adding pads to self')
self.add_pad(self.srcpad)
self.add_pad(self.sinkpad)
gst.info('setting chain/event functions')
#self.srcpad.set_chain_function(self.src_chainfunc)
#self.srcpad.set_event_function(self.src_eventfunc)
self.sinkpad.set_chain_function(self.sink_chainfunc)
self.sinkpad.set_event_function(self.sink_eventfunc)
self.time_cb = None
self.interval = 60
self._calc_next()
def _calc_next(self):
"""Given the configured interval, calculate when the next
frame should be added to the output"""
d = self.interval
self.next = d + int(time.time() / d)*d
def set_property(self, name, value):
if name == "interval":
self.interval = value
self._calc_next()
elif name =="time_cb":
self.time_cb = value
def sink_chainfunc(self, pad, buffer):
#self.info("%s timestamp(buffer):%d" % (pad, buffer.timestamp))
self.time_cb and self.time_cb(time.ctime(self.next))
if time.time() > self.next:
self._calc_next()
self.srcpad.push(buffer)
self.info("pushed buffer at " + time.ctime())
return gst.FLOW_OK
def sink_eventfunc(self, pad, event):
self.info("%s event:%r" % (pad, event.type))
self.srcpad.push_event(event)
return True
gobject.type_register(LapseElement)
class lapse:
def __init__(self):
pipeline = gst.Pipeline("time lapse")
source = gst.element_factory_make("v4lsrc", "source")
text = gst.element_factory_make("cairotextoverlay")
tee = gst.element_factory_make("tee")
self.caps = gst.Caps("video/x-raw-yuv, width=640,height=480")
filter = gst.element_factory_make("capsfilter", "filter")
filter.set_property("caps", self.caps)
text.set_property("halign", "left")
text.set_property("valign", "bottom")
pipeline.add(source, filter, text, tee)
#link all elements
gst.element_link_many(source, filter, text, tee)
self.pipeline = pipeline
self.text = text
self.tee = tee
self.outputs=[]
def _tcb(self, txt):
self.text.set_property("text", txt)
def add_output(self, filename, interval):
self.outputs.append(dict(filename=filename,interval=interval))
enc = gst.element_factory_make("theoraenc")
mux = gst.element_factory_make("oggmux")
sink = gst.element_factory_make("filesink")
lapse = LapseElement()
lapse.set_property("interval", interval)
lapse.set_property('time_cb', self._tcb)
sink.set_property("location", filename)
self.pipeline.add(lapse, enc, mux, sink)
gst.element_link_many(self.tee, lapse, enc, mux, sink)
def run(self):
if not self.outputs:
raise RuntimeError("No outputs")
self.bus = self.pipeline.get_bus()
self.pipeline.set_state(gst.STATE_PLAYING)
while 1:
msg = self.bus.poll(gst.MESSAGE_EOS | gst.MESSAGE_ERROR, gst.SECOND)
if msg:
break
self.pipeline.set_state(gst.STATE_NULL)
def foo(self, *args):
print args
sleep(self.interval)
if __name__ == "__main__":
l = lapse()
l.add_output(filename='video.ogm', interval=5)
l.add_output(filename='video2.ogm', interval=60)
l.run()