forked from xifangczy/cat-catch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpd.js
144 lines (136 loc) · 5.57 KB
/
mpd.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
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
// url 参数解析
const params = new URL(location.href).searchParams;
const _url = params.get("url");
// const _referer = params.get("referer");
const _requestHeaders = params.get("requestHeaders");
const _title = params.get("title");
// 修改当前标签下的所有xhr的requestHeaders
let requestHeaders = JSONparse(_requestHeaders);
setRequestHeaders(requestHeaders, () => { awaitG(init); });
var mpdJson = {}; // 解析器json结果
var mpdXml = {}; // 解析器xml结果
var mpdContent; // mpd文件内容
var m3u8Content = ""; //m3u8内容
var mediaInfo = "" // 媒体文件信息
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message == "getM3u8") {
sendResponse({ m3u8Content, mediaInfo });
}
});
function init() {
$(`<style>${G.css}</style>`).appendTo("head");
if (_url) {
fetch(_url)
.then(response => response.text())
.then(function (text) {
mpdContent = text;
parseMPD(mpdContent);
$("#mpd_url").html(_url).attr("href", _url);
});
} else {
$("#loading").hide();
$("#mpdCustom").show();
$("#parse").click(function () {
let url = $("#mpdUrl").val().trim();;
url = "mpd.html?url=" + encodeURIComponent(url);
let referer = $("#referer").val().trim();;
if (referer) { url += "&requestHeaders=" + JSON.stringify({ referer: referer }); }
chrome.tabs.update({ url: url });
});
}
$("#mpdVideoLists, #mpdAudioLists").change(function () {
let type = this.id == "mpdVideoLists" ? "video" : "audio";
showSegment(type, $(this).val());
});
$("#getVideo, #getAudio").click(function () {
let type = "video";
let index = $("#mpdVideoLists").val();
if (this.id == "getAudio") {
type = "audio";
index = $("#mpdAudioLists").val();
}
showSegment(type, index);
});
$("#videoToM3u8, #audioToM3u8").click(function () {
let index = $("#mpdVideoLists").val();
let items = mpdJson.playlists[index];
let type = "video";
if (this.id == "audioToM3u8") {
index = $("#mpdAudioLists").val();
let temp = index.split("$-bmmmd-$");
index = temp[0];
let index2 = temp[1];
items = mpdJson.mediaGroups.AUDIO.audio[index].playlists[index2];
type = "audio";
}
mediaInfo = getInfo(type);
m3u8Content = "#EXTM3U\n";
m3u8Content += "#EXT-X-VERSION:3\n";
m3u8Content += "#EXT-X-TARGETDURATION:" + items.targetDuration + "\n";
m3u8Content += "#EXT-X-MEDIA-SEQUENCE:0\n";
m3u8Content += "#EXT-X-PLAYLIST-TYPE:VOD\n";
m3u8Content += '#EXT-X-MAP:URI="' + items.segments[0].map.resolvedUri + '"\n';
for (let key in items.segments) {
m3u8Content += "#EXTINF:" + items.segments[key].duration + ",\n"
m3u8Content += items.segments[key].resolvedUri + "\n";
}
m3u8Content += "#EXT-X-ENDLIST";
// $("#media_file").html(m3u8Content); return;
chrome.tabs.getCurrent(function (tabs) {
chrome.tabs.create({ url: "m3u8.html?getId=" + tabs.id });
});
});
}
function parseMPD() {
$("#loading").hide(); $("#main").show();
mpdJson = mpdParser.parse(mpdContent, { manifestUri: _url });
mpdXml = $(mpdContent);
console.log(mpdJson);
if (mpdXml.find("contentprotection").length > 0) {
$("#loading").show();
$("#loading .optionBox").html(i18n.DRMerror);
}
for (let key in mpdJson.playlists) {
$("#mpdVideoLists").append(`<option value='${key}'>${mpdJson.playlists[key].attributes.NAME
} | ${(mpdJson.playlists[key].attributes.BANDWIDTH / 1024).toFixed(1)
} kbps | ${mpdJson.playlists[key].attributes["FRAME-RATE"].toFixed(1)
} fps | ${mpdJson.playlists[key].attributes.RESOLUTION.width
} x ${mpdJson.playlists[key].attributes.RESOLUTION.height
}</option>`);
}
for (let key in mpdJson.mediaGroups.AUDIO.audio) {
for (let index in mpdJson.mediaGroups.AUDIO.audio[key].playlists) {
let item = mpdJson.mediaGroups.AUDIO.audio[key].playlists[index];
// console.log(item);
$("#mpdAudioLists").append(`<option value='${key}$-bmmmd-$${index}'>${key} | ${item.attributes.NAME} | ${item.attributes.BANDWIDTH / 1000}Kbps</option>`);
}
}
$("#info").html(getInfo("video"));
showSegment("video", 0);
}
function showSegment(type, index) {
let textarea = "";
let items;
if (type == "video") {
items = mpdJson.playlists[index];
} else {
let temp = index.split("$-bmmmd-$");
index = temp[0];
let index2 = temp[1];
items = mpdJson.mediaGroups.AUDIO.audio[index].playlists[index2];
}
for (let key in items.segments) {
textarea += items.segments[key].resolvedUri + "\n\n";
}
$("#media_file").html(textarea);
$("#count").html(i18n("m3u8Info", [items.segments.length, secToTime(mpdJson.duration)]));
items.segments.length > 0 && $("#tips").html('initialization: <input type="text" value="' + items.segments[0].map.resolvedUri + '" spellcheck="false" readonly="readonly" class="width100">');
$("#info").html(getInfo(type));
}
function getInfo(type = "audio") {
if (type == "audio") {
return i18n.audio + ": " + $("#mpdAudioLists").find("option:selected").text();
} else {
return i18n.video + ": " + $("#mpdVideoLists").find("option:selected").text();
}
}