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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
# !/usr/bin/python
# -*- coding: utf-8 -*-
# Glitter Toolkit
__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
__licenses__ = ["LICENSE.LGPL"]
__description__ = "Video widget"
import os
import sys
import math
import gst
import gobject
import clutter
import cluttergst
from container import Container
class Video(Container):
"""
A video widget offers easy and simple video playback backed by GStreamer.
Features include seamless playback, support for all GStreamer compatible
video streams, basic playback control and support for native, widescreen
(16:9), TV (4:3) and "smart" aspect ratios.
"""
def __init__(self, source=None, aspect_ratio="native"):
""" Initialize video widget """
super(Video, self).__init__()
self._source = source
self._aspect_ratio = aspect_ratio
self._texture = None
self._update_style(self.style)
def _update_style(self, props=None):
""" Updates style """
super(Video, self)._update_style(props)
pass
def _update_layout(self):
""" Updates layout """
super(Video, self)._update_layout()
if not self._texture:
self._set_source()
widthu = self.get_widthu()
heightu = self.get_heightu()
# Account for 0 width or height
if widthu == 0 or heightu == 0:
return
width_delta = math.fabs(widthu - widthu * 0.5625)
height_delta = math.fabs(heightu - heightu * 1.7777)
# native
if self.aspect_ratio == "native":
self._texture.set_positionu(0.0, 0.0)
self._texture.set_widthu(widthu)
self._texture.set_heightu(heightu)
# 16:9 widescreen (720p & 1080p)
elif self.aspect_ratio == "widescreen":
if width_delta < height_delta:
video_height = int(widthu * 0.5625)
self._texture.set_widthu(widthu)
self._texture.set_heightu(video_height)
self._texture.set_xu(0)
self._texture.set_yu(int((heightu - video_height) / 2.0))
else:
video_width = int(heightu * 1.7777)
self._texture.set_widthu(video_width)
self._texture.set_heightu(heightu)
self._texture.set_xu(int((widthu - video_width) / 2.0))
self._texture.set_yu(0)
# 4:3 tv-like
elif self.aspect_ratio == "tv":
if width_delta < height_delta:
video_height = int(widthu * 0.75)
self._texture.set_widthu(widthu)
self._texture.set_heightu(video_height)
self._texture.set_xu(0)
self._texture.set_yu(int((heightu - video_height) / 2.0))
else:
video_width = int(heightu * 1.3333)
self._texture.set_widthu(video_width)
self._texture.set_heightu(heightu)
self._texture.set_xu(int((widthu - video_width) / 2.0))
self._texture.set_yu(0)
# smart: maximum viewing area without cutting too much
elif self.aspect_ratio == "smart":
if width_delta < height_delta:
video_height = int(widthu * 0.5625)
self._texture.set_widthu(widthu)
self._texture.set_heightu(video_height)
self._texture.set_xu(0)
self._texture.set_yu(int((heightu - video_height) / 2.0))
else:
video_width = int(heightu * 1.7777)
self._texture.set_widthu(video_width)
self._texture.set_heightu(heightu)
self._texture.set_xu(int((widthu - video_width) / 2.0))
self._texture.set_yu(0)
def get_source(self):
""" Retrieve video source (uri) """
return self._source
def set_source(self, value):
""" Sets video source
value -- (str) source file uri
"""
self._source = value
source = property(get_source, set_source)
def get_aspect_ratio(self):
""" Retireve aspect ratio """
return self._aspect_ratio
def set_aspect_ratio(self, value):
""" Sets aspect ratio
value -- (str) 'native', 'widescreen', 'tv' or 'smart'
"""
self._aspect_ratio = value
self._update_layout()
aspect_ratio = property(get_aspect_ratio, set_aspect_ratio)
def get_position(self):
""" Retrieve video position as a factor of 1 """
try:
position = self.playbin.query_position(gst.FORMAT_TIME, None)[0]
duration = self.playbin.query_duration(gst.FORMAT_TIME, None)[0]
except gst.QueryError, ex:
return 0
position_secs = position / 1000000000.0
duration_secs = duration / 1000000000.0
return position_secs / duration_secs
def set_position(self, position):
""" Set video position as a factor of 1 """
try:
duration = self.playbin.query_duration(gst.FORMAT_TIME, None)[0]
except gst.QueryError, ex:
return 0
time_ns = duration * position
self.playbin.seek_simple(gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, time_ns)
def get_position_as_timestamp(self, mode="ellapsed"):
""" Retrieve playback position as a timestamp (ex: -3:07)
mode -- (str) "ellapsed" or "left"
"""
try:
duration = self.playbin.query_duration(gst.FORMAT_TIME, None)[0]
position = self.playbin.query_position(gst.FORMAT_TIME, None)[0]
if mode == "ellapsed":
return self._time_ns_to_stamp(position)
else:
return "-" + self._time_ns_to_stamp(duration - position)
except gst.QueryError, ex:
return "0:00"
def play(self):
""" Play video. Resumes or starts video playback """
if not self.is_playing():
self._texture.set_playing(True)
def pause(self):
""" Pause video playback """
if self.is_playing():
self._texture.set_playing(False)
def stop(self):
""" Stop video playback """
if self._texture:
self.remove(self._texture)
self._texture.set_playing(False)
self._texture.set_property("position", 0)
def is_playing(self):
""" Retrieve wether video is playing """
if self._texture:
if self._texture.get_playing():
return True
return False
def _on_gst_message(self, bus, message):
""" Messages from our gstreamer bus """
if message.type == gst.MESSAGE_EOS:
self.stop()
elif message.type == gst.MESSAGE_ERROR:
print "Gstreamer exception -- bus: " + str(bus) + ", message: " + \
str(message)
def _set_source(self):
""" Set texture based on given source """
width = self.get_width()
height = self.get_height()
# Account for 0 width or height
if width == 0 or height == 0:
return
# Account for no size change
if self._texture and \
width == self._texture.get_width() and \
height == self._texture.get_height():
return
if os.path.exists(self.source):
self._texture = cluttergst.VideoTexture()
self.playbin = self._texture.get_playbin()
self.bus = self.playbin.get_bus()
self.bus.add_signal_watch()
self.bus.connect('message', self._on_gst_message)
self._texture.show()
self._texture.set_filename(self.source)
self.add(self._texture)
self.play()
else:
raise AttributeError, "source doesn't exist: %s" % self.source
def _time_ns_to_stamp(self, time_ns):
""" Converts time given in nanoseconds to a human readable time
stamp (ex: 4:23)
time_ns -- (int) time in nanoseconds
"""
time_s = time_ns / 1000000000
time_stamp = ""
if time_s >= 3600:
hours = time_s / 3600
time_s -= hours * 3600
time_stamp = str(hours) + ":"
if time_s >= 600:
mins = time_s / 60
time_s -= mins * 60
time_stamp += str(mins) + ":"
elif time_s >= 60:
mins = time_s / 60
time_s -= mins * 60
time_stamp += str(mins) + ":"
else:
time_stamp = time_stamp + "0:"
if time_s > 9:
time_stamp += str(time_s)
else:
time_stamp += "0" + str(time_s)
return time_stamp
|