-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
213 lines (148 loc) · 5.97 KB
/
main.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
# -*- coding: utf-8 -*-
import glfw
import OpenGL.GL as gl
import imgui
from imgui.integrations.glfw import GlfwRenderer
import ctypes
from mpv import MPV, MpvRenderContext, OpenGlCbGetProcAddrFn
class VideoPlayer:
def terminate(self):
self.ctx.free()
self.mpv.terminate()
def __init__(self,filename):
self.filename = filename
self.open = True
self.fbo = gl.glGenFramebuffers(1)
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, self.fbo)
self.texture = gl.glGenTextures(1)
gl.glBindTexture(gl.GL_TEXTURE_2D, self.texture)
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
gl.glFramebufferTexture2D(gl.GL_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT0, gl.GL_TEXTURE_2D, self.texture, 0)
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, 100, 100, 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, None);
gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
self.playbackPos = (0,)
self.volume = (0,)
self.loop='inf'
self.mpv = MPV(log_handler=print, loglevel='debug')
def get_process_address(_, name):
print(name)
address = glfw.get_proc_address(name.decode('utf8'))
return ctypes.cast(address, ctypes.c_void_p).value
proc_addr_wrapper = OpenGlCbGetProcAddrFn(get_process_address)
self.ctx = MpvRenderContext(self.mpv, 'opengl', opengl_init_params={'get_proc_address': proc_addr_wrapper})
self.mpv.play(self.filename)
self.mpv.volume=0
def render(self):
videowindow, self.open = imgui.begin("Video window {}".format(self.filename), self.open, flags=imgui.WINDOW_NO_SCROLLBAR)
w,h = imgui.get_window_size()
if imgui.APPEARING:
imgui.set_window_size(400, 300)
w,h = imgui.core.get_content_region_available()
w=int(max(w,0))
h=int(max(h-85,0))
if not self.open:
imgui.end()
self.terminate()
return
if self.ctx.update() and w>0 and h>0:
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, self.fbo)
gl.glBindTexture(gl.GL_TEXTURE_2D, self.texture);
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, w, h, 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, None);
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
gl.glBindTexture(gl.GL_TEXTURE_2D, 0);
self.ctx.render(flip_y=False, opengl_fbo={'w': w, 'h': h, 'fbo': self.fbo})
try:
imgui.text("Filename: {} fbo: {} tex: {}".format(self.mpv.filename,self.fbo,self.texture))
except:
imgui.text("Filename: {} fbo: {} tex: {}".format(self.mpv.filename,self.fbo,self.texture))
try:
imgui.text("{0:.2f}s/{1:.2f}s ({2:.2f}s remaining)".format(self.mpv.time_pos,self.mpv.duration,self.mpv.playtime_remaining))
except:
imgui.text("Loading...")
imgui.image(self.texture, w,h )
imgui.push_item_width(-1)
changed, values = imgui.slider_float(
"##Playback Percentage", *self.playbackPos,
min_value=0.0, max_value=100.0,
format="Playback Percentage %.0f",
power=1.0
)
if changed and values:
try:
self.mpv.command('seek',values,'absolute-percent')
except:
pass
self.playbackPos = (values,)
elif self.mpv.percent_pos:
self.playbackPos = (self.mpv.percent_pos,)
changed, values = imgui.slider_float(
"##Volume", *self.volume,
min_value=0.0, max_value=100.0,
format="Volume %.0f",
power=1.0
)
if changed:
self.mpv.volume = values
self.volume = (values,)
elif self.mpv.volume:
self.volume = (self.mpv.volume,)
imgui.end()
def main():
imgui.create_context()
window = impl_glfw_init()
impl = GlfwRenderer(window)
_ = gl.glGenFramebuffers(1)
videoWindows = []
def dropFile(window,files):
for file in files:
videoWindows.append(VideoPlayer(file))
glfw.set_drop_callback(window, dropFile)
while not glfw.window_should_close(window):
glfw.poll_events()
impl.process_inputs()
imgui.new_frame()
videoWindows = [x for x in videoWindows if x.open]
if len(videoWindows) > 0:
for videoWindow in videoWindows:
videoWindow.render()
else:
imgui.core.set_next_window_position(10,10)
winw,winh = glfw.get_window_size(window)
imgui.core.set_next_window_size(winw-20,10)
imgui.begin("##Message", True, flags=imgui.WINDOW_NO_SCROLLBAR|imgui.WINDOW_NO_RESIZE|imgui.WINDOW_NO_TITLE_BAR|imgui.WINDOW_NO_MOVE )
imgui.text("Drop one or more video files onto this window to play")
imgui.end()
gl.glClearColor(0., 0., 0., 1.)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
imgui.render()
impl.render(imgui.get_draw_data())
glfw.swap_buffers(window)
for videoWindow in videoWindows:
videoWindow.terminate()
impl.shutdown()
glfw.terminate()
def impl_glfw_init():
width, height = 1280, 720
window_name = "minimal ImGui/GLFW3/pythonMPV example"
if not glfw.init():
print("Could not initialize OpenGL context")
exit(1)
# OS X supports only forward-compatible core profiles from 3.2
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(
int(width), int(height), window_name, None, None
)
glfw.make_context_current(window)
if not window:
glfw.terminate()
print("Could not initialize Window")
exit(1)
return window
if __name__ == "__main__":
main()