forked from autotest/virt-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_maker.py
224 lines (171 loc) · 6.65 KB
/
video_maker.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
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
"""
Video Maker transforms screenshots taken during a test into a HTML 5
compatible video, so that one can watch the screen activity of the
whole test from inside your own browser.
This relies on generally available multimedia libraries, frameworks
and tools.
"""
import os, time, glob, logging
__all__ = ['GstPythonVideoMaker', 'video_maker']
#
# Check what kind of video libraries tools we have available
#
# Gstreamer python bindings are our first choice
try:
import gst
GST_PYTHON_INSTALLED = True
except ImportError:
GST_PYTHON_INSTALLED = False
#
# PIL is also required to normalize images
#
try:
import PIL.Image
PIL_INSTALLED = True
except ImportError:
PIL_INSTALLED = False
#
# We only do video
#
CONTAINER_PREFERENCE = ['ogg', 'webm']
ENCODER_PREFERENCE = ['theora', 'vp8']
class GstPythonVideoMaker(object):
'''
Makes a movie out of screendump images using gstreamer-python
'''
CONTAINER_MAPPING = {'ogg' : 'oggmux',
'webm' : 'webmmux'}
ENCODER_MAPPING = {'theora' : 'theoraenc',
'vp8' : 'vp8enc'}
CONTAINER_ENCODER_MAPPING = {'ogg' : 'theora',
'webm' : 'vp8'}
def __init__(self, verbose=False):
if not GST_PYTHON_INSTALLED:
raise ValueError('gstreamer-python library was not found')
if not PIL_INSTALLED:
raise ValueError('python-imaging library was not found')
self.verbose = verbose
def get_most_common_image_size(self, input_dir):
'''
Find the most common image size
'''
image_sizes = {}
image_files = glob.glob(os.path.join(input_dir, '*.jpg'))
for f in image_files:
i = PIL.Image.open(f)
if not image_sizes.has_key(i.size):
image_sizes[i.size] = 1
else:
image_sizes[i.size] += 1
most_common_size_counter = 0
most_common_size = None
for image_size, image_counter in image_sizes.items():
if image_counter > most_common_size_counter:
most_common_size_counter = image_counter
most_common_size = image_size
return most_common_size
def normalize_images(self, input_dir):
'''
GStreamer requires all images to be the same size, so we do it here
'''
image_size = self.get_most_common_image_size(input_dir)
if image_size is None:
image_size = (800, 600)
if self.verbose:
logging.debug('Normalizing image files to size: %s', image_size)
image_files = glob.glob(os.path.join(input_dir, '*.jpg'))
for f in image_files:
i = PIL.Image.open(f)
if i.size != image_size:
i.resize(image_size).save(f)
def has_element(self, kind):
'''
Returns True if a gstreamer element is available
'''
return gst.element_factory_find(kind) is not None
def get_container_name(self):
'''
Gets the video container available that is the best based on preference
'''
for c in CONTAINER_PREFERENCE:
element_kind = self.CONTAINER_MAPPING.get(c, c)
if self.has_element(element_kind):
return element_kind
raise ValueError('No suitable container format was found')
def get_encoder_name(self):
'''
Gets the video encoder available that is the best based on preference
'''
for c in ENCODER_PREFERENCE:
element_kind = self.ENCODER_MAPPING.get(c, c)
if self.has_element(element_kind):
return element_kind
raise ValueError('No suitable encoder format was found')
def get_element(self, name):
'''
Makes and returns and element from the gst factory interface
'''
if self.verbose:
logging.debug('GStreamer element requested: %s', name)
return gst.element_factory_make(name, name)
def start(self, input_dir, output_file):
'''
Process the input files and output the video file
'''
self.normalize_images(input_dir)
no_files = len(glob.glob(os.path.join(input_dir, '*.jpg')))
if self.verbose:
logging.debug('Number of files to encode as video: %s', no_files)
pipeline = gst.Pipeline("pipeline")
source = self.get_element("multifilesrc")
source_location = os.path.join(input_dir, "%04d.jpg")
if self.verbose:
logging.debug("Source location: %s", source_location)
source.set_property('location', source_location)
source.set_property('index', 1)
source_caps = gst.Caps()
source_caps.append('image/jpeg,framerate=(fraction)4/1')
source.set_property('caps', source_caps)
decoder = self.get_element("jpegdec")
# Attempt to auto detect the chosen encoder/mux based on output_file
encoder = None
container = None
for container_name in self.CONTAINER_ENCODER_MAPPING:
if output_file.endswith('.%s' % container_name):
enc_name = self.CONTAINER_ENCODER_MAPPING[container_name]
enc_name_gst = self.ENCODER_MAPPING[enc_name]
encoder = self.get_element(enc_name_gst)
cont_name_gst = self.CONTAINER_MAPPING[container_name]
container = self.get_element(cont_name_gst)
# If auto detection fails, choose from the list of preferred codec/mux
if encoder is None:
encoder = self.get_element(self.get_encoder_name())
if container is None:
container = self.get_element(self.get_container_name())
output = self.get_element("filesink")
output.set_property('location', output_file)
pipeline.add_many(source, decoder, encoder, container, output)
gst.element_link_many(source, decoder, encoder, container, output)
pipeline.set_state(gst.STATE_PLAYING)
while True:
if source.get_property('index') <= no_files:
if self.verbose:
logging.debug("Currently processing image number: %s",
source.get_property('index'))
time.sleep(1)
else:
break
time.sleep(3)
pipeline.set_state(gst.STATE_NULL)
def video_maker(input_dir, output_file):
'''
Instantiates and runs a video maker
'''
v = GstPythonVideoMaker()
v.start(input_dir, output_file)
if __name__ == '__main__':
import sys
if len(sys.argv) < 3:
print 'Usage: %s <input_dir> <output_file>' % sys.argv[0]
else:
video_maker(sys.argv[1], sys.argv[2])