Skip to content

Commit ca17410

Browse files
committed
video: better extension extraction for explicit video info (use format
if ext is unknown) improved support for _pywbvid=html, forces generic html player over yt player, better deletion of ytplayer
1 parent ad5a43d commit ca17410

File tree

1 file changed

+69
-38
lines changed

1 file changed

+69
-38
lines changed

pywb/static/vidrw.js

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ __wbvidrw = (function() {
2323

2424
var found_embeds = false;
2525

26-
var vid_type = "html";
26+
var vid_type = "default";
2727

2828
var FLASH_PLAYER = wbinfo.static_prefix + "/flowplayer/flowplayer-3.2.18.swf";
2929

@@ -61,31 +61,12 @@ __wbvidrw = (function() {
6161

6262
found_embeds = true;
6363

64+
handle_yt_videos(vid_type);
65+
6466
//window.setInterval(handle_all_embeds, 1000);
6567

6668
//_wb_wombat.add_tag_handler("embed", handle_all_embeds);
6769
//_wb_wombat.add_tag_handler("object", handle_all_objects);
68-
69-
// special case: yt
70-
/*
71-
if (!found_embeds && wbinfo.url.indexOf("://www.youtube.com/watch") > 0) {
72-
var ytvideo = document.getElementsByTagName("video");
73-
74-
if (ytvideo.length == 1) {
75-
if (ytvideo[0].getAttribute("data-youtube-id") != "") {
76-
// Wait to see if video is playing, if so, don't replace it
77-
window.setTimeout(function() {
78-
if (!ytvideo || !ytvideo.length || ytvideo[0].readyState == 0) {
79-
delete window.yt;
80-
delete window.ytplayer;
81-
console.log("REPLACING YT: " + wbinfo.url);
82-
check_replacement(ytvideo[0], wbinfo.url);
83-
}
84-
}, 4000);
85-
}
86-
}
87-
}
88-
*/
8970
}
9071

9172
function handle_embed_tag(elem)
@@ -151,11 +132,52 @@ __wbvidrw = (function() {
151132
return false;
152133
}
153134

154-
var YT_RX = /^(https?:\/\/.*youtube.com)\/v\/([^&?]+)(.*)$/;
155-
135+
var YT_W_E_RX = /^(https?:\/\/.*youtube.com)\/(watch|embed).*$/;
136+
var YT_V_RX = /^(https?:\/\/.*youtube.com)\/v\/([^&?]+)(.*)$/;
156137
var VIMEO_RX = /^https?:\/\/.*vimeo.*clip_id=([^&]+)/;
157138

158139

140+
function handle_yt_videos(vid_type)
141+
{
142+
function do_yt_video_replace()
143+
{
144+
console.log("REPLACING YT: " + wbinfo.url);
145+
ytvideo[0].autoplay = false;
146+
ytvideo[0].preload = "none";
147+
148+
var elem = ytvideo[0];
149+
// get ancestor 'div'
150+
if (elem.parentElement) {
151+
elem = elem.parentElement;
152+
}
153+
if (elem.parentElement) {
154+
elem = elem.parentElement;
155+
}
156+
console.log(elem);
157+
158+
// Experimental
159+
160+
check_replacement(elem, wbinfo.url);
161+
}
162+
163+
// special case: yt
164+
if (wbinfo.url.match(YT_W_E_RX)) {
165+
var ytvideo = document.getElementsByTagName("video");
166+
167+
if (ytvideo.length == 1 && ytvideo[0].getAttribute("data-youtube-id") != "") {
168+
if (vid_type == "html") {
169+
do_yt_video_replace();
170+
} else {
171+
setTimeout(function() {
172+
if (!ytvideo || !ytvideo.length || ytvideo[0].readyState == 0) {
173+
do_yt_video_replace();
174+
}
175+
}, 4000);
176+
}
177+
}
178+
}
179+
}
180+
159181
function check_replacement(elem, src, no_retry) {
160182
if (!src || src.indexOf("javascript:") == 0) {
161183
return;
@@ -176,19 +198,12 @@ __wbvidrw = (function() {
176198
src = src.replace(VIMEO_RX, "http://player.vimeo.com/video/$1");
177199

178200
if (vid_type == "orig") {
179-
var repl_src = src.replace(YT_RX, "$1/embed/$2?$3&controls=0");
201+
var repl_src = src.replace(YT_V_RX, "$1/embed/$2?$3&controls=0");
180202
if (repl_src != src) {
181203
do_replace_iframe(elem, repl_src);
182204
return;
183205
}
184206
}
185-
186-
if (window.yt) {
187-
delete window.yt;
188-
}
189-
if (window.ytplayer) {
190-
delete window.ytplayer;
191-
}
192207
// end special cases
193208

194209
var xhr = new XMLHttpRequest();
@@ -257,8 +272,14 @@ __wbvidrw = (function() {
257272

258273
elem.parentNode.replaceChild(replacement, elem);
259274

260-
} else if (tag_name == "video") {
261-
elem.parentNode.replaceChild(replacement, elem);
275+
} else {
276+
elem.parentNode.replaceChild(replacement, elem);
277+
}
278+
279+
if (window.yt) {
280+
yt.player.Application.create("player-api", ytplayer.config).dispose();
281+
delete window.yt;
282+
delete window.ytplayer;
262283
}
263284
}
264285

@@ -316,6 +337,14 @@ __wbvidrw = (function() {
316337
}
317338
}
318339

340+
function get_format_ext(info_format) {
341+
if (info_format.ext == "unknown_video") {
342+
return info_format.format_id;
343+
} else {
344+
return info_format.ext;
345+
}
346+
}
347+
319348
function can_play_video_or_audio(elem, info) {
320349
var types = ["video", "audio"];
321350
var type = undefined;
@@ -330,7 +359,9 @@ __wbvidrw = (function() {
330359
}
331360

332361
for (var j = 0; j < types.length; j++) {
333-
if (can_play(elem, info.formats[i].ext, types[j])) {
362+
var ext = get_format_ext(info.formats[i]);
363+
364+
if (can_play(elem, ext, types[j])) {
334365
info.formats[i]._wb_canPlay = true;
335366
info._wb_avail++;
336367
type = types[j];
@@ -389,13 +420,13 @@ __wbvidrw = (function() {
389420

390421
if (i < 0) {
391422
url = info.url;
392-
format = info.ext;
423+
format = get_format_ext(info);
393424
} else {
394-
url = info.formats[i].url;
395-
format = info.formats[i].ext;
396425
if (!info.formats[i]._wb_canPlay) {
397426
continue;
398427
}
428+
url = info.formats[i].url;
429+
format = get_format_ext(info.formats[i]);
399430
}
400431

401432
url = wbinfo.prefix + url;

0 commit comments

Comments
 (0)