annotate libervia/pages/_browser/alt_media_player.py @ 1337:f0648005cd11

browser: alternative media player: the new `alt_media_player` module implements an `ogv.js` based media player which is able to play WebM (VP8/VP9) or Ogg (Theora, Vorbis) formats (+ other codecs not integrated yet) on browser that don't do it natively. The module imports `ogv.js` only when it's necessary (incompatible browser + videos needing it). This for now used in blog, and will be used in other places when it's suitable.
author Goffi <goffi@goffi.org>
date Sat, 15 Aug 2020 16:45:23 +0200
parents
children 472267dcd4d8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 """This module implement an ogv.js based alternative media player
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
4
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 This is useful to play libre video/audio formats on browser that don't do it natively.
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 """
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
7
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from browser import document, timer
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
9
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 class VideoPlayer:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 TIMER_MODES = ("timer", "remaining")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 imports_done = False
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 def __init__(self, ori_video_elt, sources):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 self.video_player_elt = video_player_elt = video_player_tpl.get_elt()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 self.player = player = self.ogv.OGVPlayer.new() # {"debug": True})
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 ori_video_elt.parentNode.replaceChild(video_player_elt, ori_video_elt)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 overlay_play_elt = self.video_player_elt.select_one(".video_overlay_play")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 overlay_play_elt.bind("click", self.on_overlay_play_elt_click)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 self.progress_elt = video_player_elt.select_one("progress")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 self.progress_elt.bind("click", self.on_progress_click)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 self.timer_elt = video_player_elt.select_one(".timer")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 self.timer_mode = "timer"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 self.controls_elt = video_player_elt.select_one(".video_controls")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 player_wrapper_elt = video_player_elt.select_one(".video_elt")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 player.src = sources[0]
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 player_wrapper_elt <= player
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 self.hide_controls_timer = None
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 # a click on the video itself is like click on play icon
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 player_wrapper_elt.bind("click", self.on_play_click)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 # buttons
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 for handler in ("play", "change_timer_mode", "change_volume", "fullscreen"):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 for elt in video_player_elt.select(f".click_to_{handler}"):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 elt.bind("click", getattr(self, f"on_{handler}_click"))
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 # events
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 # FIXME: progress is not implemented in OGV.js, update when available
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 for event in ("play", "pause", "timeupdate", "ended", "volumechange"):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 player.bind(event, getattr(self, f"on_{event}"))
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def on_overlay_play_elt_click(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 evt.stopPropagation()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 evt.target.remove()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self.player.play()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def on_play_click(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 if self.player.paused:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 print("playing")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.player.play()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.player.pause()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 print("paused")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def on_change_timer_mode_click(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.timer_mode = self.TIMER_MODES[
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 (self.TIMER_MODES.index(self.timer_mode) + 1) % len(self.TIMER_MODES)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 ]
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def on_change_volume_click(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.player.muted = not self.player.muted
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def on_fullscreen_click(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 try:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 fullscreen_elt = document.fullscreenElement
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 request_fullscreen = self.video_player_elt.requestFullscreen
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 except AttributeError:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 print("fullscreen is not available on this browser")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 if fullscreen_elt == None:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 print("requesting fullscreen")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 request_fullscreen()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 print(f"leaving fullscreen: {fullscreen_elt}")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 try:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 document.exitFullscreen()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 except AttributeError:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 print("exitFullscreen not available on this browser")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 def on_progress_click(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 position = evt.offsetX / evt.target.width
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 new_time = self.player.duration * position
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 self.player.currentTime = new_time
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def on_play(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self.video_player_elt.classList.add("playing")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.show_controls()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.video_player_elt.bind("mousemove", self.on_mouse_move)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 def on_pause(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 self.video_player_elt.classList.remove("playing")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 self.show_controls()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 self.video_player_elt.unbind("mousemove")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def on_timeupdate(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 self.update_progress()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 def on_ended(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 self.update_progress()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 def on_volumechange(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 if self.player.muted:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 self.video_player_elt.classList.add("muted")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self.video_player_elt.classList.remove("muted")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 def on_mouse_move(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 self.show_controls()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 def update_progress(self):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 duration = self.player.duration
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 current_time = duration if self.player.ended else self.player.currentTime
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 self.progress_elt.max = duration
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self.progress_elt.value = current_time
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 self.progress_elt.text = f"{current_time/duration*100:.02f}"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 current_time, duration = int(current_time), int(duration)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 if self.timer_mode == "timer":
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 cur_min, cur_sec = divmod(current_time, 60)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 tot_min, tot_sec = divmod(duration, 60)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 self.timer_elt.text = f"{cur_min}:{cur_sec:02d}/{tot_min}:{tot_sec:02d}"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 elif self.timer_mode == "remaining":
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 rem_min, rem_sec = divmod(duration - current_time, 60)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 self.timer_elt.text = f"{rem_min}:{rem_sec:02d}"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 print(f"ERROR: unknown timer mode: {self.timer_mode}")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 def hide_controls(self):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 self.controls_elt.classList.add("hidden")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 self.video_player_elt.style.cursor = "none"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 if self.hide_controls_timer is not None:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 timer.clear_timeout(self.hide_controls_timer)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self.hide_controls_timer = None
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def show_controls(self):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 self.controls_elt.classList.remove("hidden")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 self.video_player_elt.style.cursor = ""
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if self.hide_controls_timer is not None:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 timer.clear_timeout(self.hide_controls_timer)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 if self.player.paused:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 self.hide_controls_timer = None
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 self.hide_controls_timer = timer.set_timeout(self.hide_controls, 3000)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 @classmethod
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 def do_imports(cls):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 # we do imports (notably for ogv.js) if they are necessary
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if cls.imports_done:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 return
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 from js_modules import ogv
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 cls.ogv = ogv
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 if not ogv.OGVCompat.supported('OGVPlayer'):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 print("Can't use OGVPlayer with this browser")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 raise NotImplementedError
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 import template
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 global video_player_tpl
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 video_player_tpl = template.Template("components/video_player.html")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 cls.imports_done = True
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
159
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 @classmethod
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 def install(cls, cant_play):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 ext_list = set()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 for data in cant_play.values():
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 ext_list.update(data['ext'])
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 for ori_video_elt in document.body.select('video'):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 sources = []
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 src = (ori_video_elt.src or '').strip()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if src:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 sources.append(src)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
170
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 for source_elt in ori_video_elt.select('source'):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 src = (source_elt.src or '').strip()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 if src:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 sources.append(src)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
175
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 # FIXME: we only use first found source
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 try:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 source = sources[0]
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 except IndexError:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 print(f"Can't find any source for following elt:\n{ori_video_elt.html}")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 continue
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
182
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 try:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 ext = f".{source.rsplit('.', 1)[1]}"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 except IndexError:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 print(
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 f"No extension found for source of following elt:\n{ori_video_elt.html}")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 continue
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 if ext and ext in ext_list:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 cls.do_imports()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 print(f"alternative player will be used for {source!r}")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 cls(ori_video_elt, sources)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
194
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 def install_if_needed():
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 CONTENT_TYPES = {
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 "ogg_theora": {"type": 'video/ogg; codecs="theora"', "ext": [".ogg"]},
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 "webm_vp8": {"type": 'video/webm; codecs="vp8, vorbis"', "ext": [".webm"]},
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 "webm_vp9": {"type": 'video/webm; codecs="vp9"', "ext": [".webm"]},
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 # FIXME: handle audio
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 # "ogg_vorbis": {"type": 'audio/ogg; codecs="vorbis"', "ext": ".ogg"},
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 }
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 test_video_elt = document.createElement("video")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 cant_play = {k:d for k,d in CONTENT_TYPES.items() if test_video_elt.canPlayType(d['type']) != "probably"}
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
205
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 if cant_play:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 cant_play_list = '\n'.join(f"- {k} ({d['type']})" for k, d in cant_play.items())
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 print(
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 "This browser is incompatible with following content types, using alternative:\n"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 f"{cant_play_list}"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 )
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 try:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 VideoPlayer.install(cant_play)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 except NotImplementedError:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 pass
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 print("This browser can play natively all requested open video/audio formats")