forked from endlesshack/youtube-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube-video.js
60 lines (60 loc) · 2.02 KB
/
youtube-video.js
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
(function() {
window.YoutubeVideo = function(id, callback) {
return $.ajax({
url: "http://www.youtube.com/get_video_info?video_id=" + id,
dataType: "text"
}).done(function(video_info) {
var video;
video = YoutubeVideo.decodeQueryString(video_info);
if (video.status === "fail") {
return callback(video);
}
video.sources = YoutubeVideo.decodeStreamMap(video.url_encoded_fmt_stream_map);
video.getSource = function(type, quality) {
var exact, key, lowest, source, _ref;
lowest = null;
exact = null;
_ref = this.sources;
for (key in _ref) {
source = _ref[key];
if (source.type.match(type)) {
if (source.quality.match(quality)) {
exact = source;
} else {
lowest = source;
}
}
}
return exact || lowest;
};
return callback(video);
});
};
window.YoutubeVideo.decodeQueryString = function(queryString) {
var key, keyValPair, keyValPairs, r, val, _i, _len;
r = {};
keyValPairs = queryString.split("&");
for (_i = 0, _len = keyValPairs.length; _i < _len; _i++) {
keyValPair = keyValPairs[_i];
key = decodeURIComponent(keyValPair.split("=")[0]);
val = decodeURIComponent(keyValPair.split("=")[1] || "");
r[key] = val;
}
return r;
};
window.YoutubeVideo.decodeStreamMap = function(url_encoded_fmt_stream_map) {
var quality, sources, stream, type, urlEncodedStream, _i, _len, _ref;
sources = {};
_ref = url_encoded_fmt_stream_map.split(",");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
urlEncodedStream = _ref[_i];
stream = YoutubeVideo.decodeQueryString(urlEncodedStream);
type = stream.type.split(";")[0];
quality = stream.quality.split(",")[0];
stream.original_url = stream.url;
stream.url = "" + stream.url + "&signature=" + stream.sig;
sources["" + type + " " + quality] = stream;
}
return sources;
};
}).call(this);