-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmplay.py
executable file
·279 lines (225 loc) · 8.25 KB
/
mplay.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
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
#!/usr/bin/env python3
# so ugly
import subprocess
import json
import sys
class Rect:
def __init__(self, pos=(0, 0), size=(0,0), parent=None):
self.setSize(size)
self.setPos(pos)
self.setParent(parent)
def setParent(self, rect):
self.parent = rect
def setPos(self, pos):
self.pos = tuple(pos)
self.x, self.y = self.pos
self.x1 = (self.x + self.w, self.y + self.h)
# print('setpos',self.x+self.w, self.y+self.h)
def setSize(self, size):
self.size = tuple(size)
self.w, self.h = self.size
self.ar = self.w / self.h if self.h else None
def quote_enclose(path):
return "'{}'".format(path.strip('"'))
def xpdyinfo(prop):
return tuple( map( int,
list( filter( None,
subprocess.check_output('xdpyinfo | grep {}'.format(prop), shell=True)
.decode().split(' '))
)[1].split('x')
))
def get_screen_rect(res = None):
if not res:
res = xpdyinfo('dimensions')
ppi = xpdyinfo('resolution')
print(size)
# size = (1080, 1920)
res = (1440, 900)
return Rect(size = res)
def get_video_rect(path):
cmd = [ 'ffprobe',
'-v', 'quiet',
'-print_format', 'json',
'-show_streams',
path ]
info = json.loads(subprocess.check_output(cmd).decode())
video_w = info['streams'][0]['width']
video_h = info['streams'][0]['height']
size = (video_w, video_h)
# size = (1920, 1080)
# size = (1080, 1080)
# size = (640,480)
return Rect(size=size)
def get_wall_rect(display, video):
if display.ar > video.ar:
# vertical letterboxing; size v.h to display.h
video_w = int( display.h / video.h * video.w )
video_h = display.h
elif display.ar < video.ar:
# horizontal letterboxing; size v.w to display.w
video_h = int( display.w / video.w * video.h )
video_w = display.w
else:
# they're the same
video_w = display.w
video_h = display.h
return Rect(size=(video_w,video_h))
def center_wall(wall, display):
w_disp = int( (display.w - wall.w) / 2 )
h_disp = int( (display.h - wall.h) / 2 )
x0 = (w_disp, h_disp)
wall.setPos(x0)
return wall
def calc_display(screen, mon_rows, mon_cols, bezel = (0,0)):
return Rect(size=
(( bezel[0] + screen.w ) * mon_cols - bezel[0],
( bezel[1] + screen.h ) * mon_rows - bezel[1]))
def calc_win_vars(s0, s1, w0, w1):
if s1 < w0 or w1 < s0: # wall not in screen
win_size = None
win_pos = None
# return (None, None)
else:
if s0 < w0: # wall starts in screen
win_pos = w0 - s0
else: # w0 <= s0: wall starts before screen
win_pos = 0
if s1 < w1: # wall ends after screen
win_size = s1 - max(w0,s0)
else: # w1 <= s1: wall ends in screen
win_size = w1 - max(w0,s0)
return (win_size, win_pos)
def calc_crop_vars(screen_pos_g, win_pos_l, win_size, wall_pos_g, scale):
crop_pos = (screen_pos_g + win_pos_l - wall_pos_g) / scale
crop_size = win_size / scale
return (crop_size, crop_pos)
def calc_transform(video, screen, wall, mon_index, bezel):
scale = wall.w / video.w
win_size = [0,0]
win_pos = [0,0]
crop_size = [0,0]
crop_pos = [0,0]
for i in range(2):
w0, w1 = (wall.pos[i], wall.x1[i])
s0 = (screen.size[i] + bezel[i]) * mon_index[i]
s1 = s0 + screen.size[i]
win_size[i], win_pos[i] = calc_win_vars(s0, s1, w0, w1)
if win_size[i] == None:
return None
crop_size[i], crop_pos[i] = calc_crop_vars(s0, win_pos[i], win_size[i], wall.pos[i], scale)
# print('size', crop_size)
# print('pos', crop_pos)
window = Rect(
pos = map(round, win_pos),
size = map(round, win_size))
crop = Rect(
pos = map(round, crop_pos),
size = map(round, crop_size))
return {
'index': mon_index[::-1],
'crop': crop,
'window': window
}
def gen_mplayer_cmd(bcast, crop, window, path):
'''
bcast : broadcast address, maybe 10.1.15.255
crop : Rect, in original video resolution + coordinates (formatted w:h:x:y)
crop.pos : (x, y)
crop.size : (w, h)
window : Rect of mplayer window
window.pos : x:y, pixel location of mplayer in screen coordinates
window.size : (w, h) size of mplayer window, automatically scaled
path : obvious
'''
crop_str = ':'.join(map(str, crop.size + crop.pos))
cmd = "DISPLAY=:0 mplayer -udp-slave -udp-ip {} -vf crop={} -geometry {}:{} -x {} -y {} {}".format(bcast, crop_str, window.x, window.y, window.w, window.h, path)
return cmd
def gen_videowall_cmds(path, size, res, bcast, draw = False):
screen_rows, screen_cols = size
bezel = (200, 200)
screen = get_screen_rect(res)
video = get_video_rect(path)
display = calc_display(screen, screen_rows, screen_cols, bezel)
wall = get_wall_rect(display, video)
wall = center_wall(wall, display)
scale_x = wall.w / video.w
scale_y = wall.h / video.h
print('screen size = {} x {}'.format(screen.w, screen.h))
print('video size = {} x {}'.format(video.w, video.h))
print('display size = {} x {}'.format(display.w, display.h))
print('wall size = {} x {}'.format(wall.w, wall.h))
print('wall scale = {:.2f} x {:.2f}'.format(scale_x, scale_y))
print()
results = (
calc_transform(video, screen , wall, (j,i), bezel)
for j in range(screen_cols)
for i in range(screen_rows)
)
results = filter(None, results)
final = ({
'pos': res['index'],
'cmd': gen_mplayer_cmd(bcast, res['crop'], res['window'], quote_enclose(path))
} for res in results)
if draw:
draw_result(display, wall, screen, bezel, size, results)
return final
def draw_result(display, wall, screen, bezel, size, results):
screen_rows, screen_cols = size
import tkinter
tk_scale = 0.2
tk_ofs = 20/tk_scale
colors = ('green','blue','orange','yellow')
tk = tkinter.Tk()
canvas = tkinter.Canvas(tk, width=700,height=500)
canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)
# display
canvas.create_rectangle(
tk_ofs,
tk_ofs,
tk_ofs + display.w + bezel[0] * 2,
tk_ofs + display.h + bezel[1] * 2,
width=2)
canvas.create_text(
tk_ofs,
tk_ofs,
text="display", anchor="sw")
# wall
canvas.create_rectangle(
tk_ofs + wall.pos[0] + bezel[0],
tk_ofs + wall.pos[1] + bezel[1],
tk_ofs + wall.x1[0] + bezel[0],
tk_ofs + wall.x1[1] + bezel[1],
outline='red',width=2)
canvas.create_text(
tk_ofs + wall.pos[0] + bezel[0],
tk_ofs + wall.pos[1] + bezel[1],
text="wall", anchor="sw")
for i in range(screen_rows):
for j in range(screen_cols):
tk_screen_ofs = (
tk_ofs + j * (screen.w + bezel[0]) + bezel[0],
tk_ofs + i * (screen.h + bezel[1]) + bezel[1])
canvas.create_rectangle(
tk_screen_ofs[0],
tk_screen_ofs[1],
tk_screen_ofs[0] + screen.w,
tk_screen_ofs[1] + screen.h)
canvas.create_text(
tk_screen_ofs[0],
tk_screen_ofs[1] + screen.h,
text="[{},{}]".format(i,j),
anchor='sw')
for res in results:
i, j = res['index']
tk_screen_ofs = (
tk_ofs + j * (screen.w + bezel[0]) + bezel[0],
tk_ofs + i * (screen.h + bezel[1]) + bezel[1])
color = colors[(j + i * screen_rows) % len(colors)]
canvas.create_rectangle(
tk_screen_ofs[0] + res['window'].x,
tk_screen_ofs[1] + res['window'].y,
tk_screen_ofs[0] + res['window'].x + res['window'].w,
tk_screen_ofs[1] + res['window'].y + res['window'].h,
outline=color, width=3)
canvas.scale("all", 0, 0, tk_scale, tk_scale)
tk.mainloop()