/
index.vue
332 lines (295 loc) · 9.8 KB
/
index.vue
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<script setup lang="ts">
import { computed, ref, watchEffect, reactive } from "vue";
import { error } from "../../components/message";
import { getImage, formatTime, sleep, createBlob, createDownloadManager, FrameController, isEscaped } from "../../utils";
import { mmcRadar } from "./data.ts";
import prompt from "./recorder-prompt.tsx";
// import jsZip from "jszip";
import * as fflate from "fflate";
function getTimeVariables(date: Date) {
const vars: ObjectData = {
UTC_YEAR: date.getUTCFullYear(),
UTC_MONTH: date.getUTCMonth() + 1,
UTC_DATE: date.getUTCDate(),
UTC_HOUR: date.getUTCHours(),
UTC_MINUTE: date.getMinutes(),
YEAR: date.getFullYear(),
MONTH: date.getMonth() + 1,
DATE: date.getDate(),
HOUR: date.getHours(),
MINUTE: date.getMinutes(),
}
Object.keys(vars).forEach((key) => {
vars[key] = vars[key].toString().padStart(2, "0");
});
return vars;
}
const form = reactive({
selectArea: mmcRadar[0].name,
selectDuration: 3 * 60 * 60 * 1000,
selectedProxy: isEscaped() ? "escape" : "appmask"
});
const selectArea = computed<UrlTemplate | null>(() => {
return mmcRadar.find(item => form.selectArea === item.name) || null;
});
const loadedArea = ref<UrlTemplate>()
/**
* 根据
*/
function getUrls(tmpl: UrlTemplate, duration: number) {
const cur = Date.now() - tmpl.delay;
const tsMin = cur - duration;
let tail = cur - cur % tmpl.interval;
const result = [];
while (tail > tsMin) {
result.push(tail);
tail -= tmpl.interval
}
return result.map(ts => {
let url = tmpl.url;
const timeVars = getTimeVariables(new Date(ts));
url = url.replace(/\$\{(.+?)\}/g, (_, fn1) => {
if (fn1 in timeVars) {
return "" + timeVars[fn1 as keyof typeof timeVars]
}
return fn1;
});
const seq: Seq = {
url,
ts,
status: "loading"
}
return seq;
});
}
let seqs = ref<Seq[]>([]);
const loadedSeq = computed(() => {
return seqs.value.filter(item => {
return item.status === "loaded"
})
});
let loading = ref(false);
let recording = ref(false);
/**
* 创建序列并加载图片
*/
async function createSequence() {
if (loading.value) {
loading.value = false;
} else {
loading.value = true;
try {
if (!selectArea.value) {
return;
}
loadedArea.value = selectArea.value;
seqs.value = getUrls(selectArea.value, form.selectDuration).reverse();
currentView.value = seqs.value[seqs.value.length - 1].ts.toString();
for (let i = seqs.value.length - 1; i >= 0; i--) {
if (!loading.value) {
break; // 中断加载
}
const imageElement = await getImage(seqs.value[i].url, form.selectedProxy);
seqs.value[i].status = "loaded";
seqs.value[i].node = imageElement;
// console.log("loaded:", imageElement);
}
} catch (e: any) {
if (e instanceof Error) {
error(e)
}
} finally {
loading.value = false;
}
}
};
let currentView = ref("0");
const ImageCanvas = ref();
watchEffect(() => {
if (currentView.value && ImageCanvas.value) {
const seq = loadedSeq.value.find(item => item.ts == parseInt(currentView.value));
if (seq && seq.node) {
const ctx = ImageCanvas.value?.getContext("2d");
ctx.drawImage(seq.node, 0, 0);
}
}
});
const zipLoading = ref(false);
async function zipDownload() {
if (zipLoading.value) {
zipLoading.value = false;
return
}
zipLoading.value = true;
try {
if (!loadedArea.value) {
return
}
const downloadManager = await createDownloadManager(
`RadarPack_${selectArea.value?.name ?? ['未知']}_${formatTime()}.zip`,
{
"application/zip": ".zip"
},
true
);
const zip = new fflate.Zip((err, dat, final) => {
if (!err) {
downloadManager.write(dat);
if (final) {
downloadManager.close();
}
} else {
downloadManager.close();
}
});
for (let item of loadedSeq.value) {
if (!zipLoading.value) {
break;
}
if (item.node) {
const blob = await createBlob(item.node);
const image = new fflate.ZipDeflate(`${formatTime(new Date(item.ts))}.png`, { level: 9 });
zip.add(image);
image.push(new Uint8Array(await blob.arrayBuffer()), true);
}
}
zip.end();
} catch (e) {
if (e instanceof Error) {
error(e)
}
} finally {
zipLoading.value = false;
}
}
async function record() {
if (recording.value) {
recording.value = false;
} else {
recording.value = true;
try {
const canvas = ImageCanvas.value;
// const recordedChunks: any[] = [];
const { mimeType, videoBitsPerSecond, speed, fps } = await prompt();
const stream = canvas.captureStream(fps);
const frameController = new FrameController(speed);
const downloadManager = await createDownloadManager(
`RadarRecord_${selectArea.value?.name ?? ['未知']}_${formatTime()}.webm`,
{
[mimeType.split(";")[0]]: `.${mimeType.split(";")[0].split("/")[1]}`
},
true
)
const mediaRecorder = new MediaRecorder(stream, { mimeType, videoBitsPerSecond });
mediaRecorder.ondataavailable = async (event: any) => {
downloadManager.write(new Uint8Array(await event.data.arrayBuffer()));
};
mediaRecorder.onerror = (event: any) => {
error(event.error as Error)
recording.value = false;
}
mediaRecorder.onstop = async () => {
await sleep(500)
downloadManager.close()
}
let index = 0;
let bufferTimeStamp = Date.now();
currentView.value = loadedSeq.value[index].ts.toString();
mediaRecorder.start();
while (recording.value && (parseInt(currentView.value) < loadedSeq.value[loadedSeq.value.length - 1].ts)) {
await frameController.nextFrame();
index += 1;
currentView.value = loadedSeq.value[index].ts.toString();
const currentTs = Date.now()
if (currentTs - bufferTimeStamp > 1000) {
// 每1秒写一次缓存
mediaRecorder.requestData();
bufferTimeStamp = currentTs;
}
}
frameController.end();
mediaRecorder.stop();
} catch (e) {
if (e instanceof Error) {
error(e)
}
} finally {
recording.value = false;
}
}
}
</script>
<template>
<div class="flex-auto flex flex-col chart items-center">
<div class="line form flex flex-row justify-center flex-none gap-2">
<select class="select select-primary" v-model="form.selectArea">
<option v-for="item of mmcRadar" :key="item.name" :value="item.name">{{ item.name }}</option>
</select>
<select class="select select-primary" v-model="form.selectDuration">
<option :value="3 * 60 * 60 * 1000">3h</option>
<option :value="6 * 60 * 60 * 1000">6h</option>
<option :value="12 * 60 * 60 * 1000">12h</option>
<option :value="24 * 60 * 60 * 1000">24h</option>
<option :value="3 * 24 * 60 * 60 * 1000">3d</option>
<option :value="7 * 24 * 60 * 60 * 1000">7d</option>
<option :value="15 * 24 * 60 * 60 * 1000">15d</option>
</select>
<select class="select select-primary" v-model="form.selectedProxy">
<option value="escape">线路0:越狱</option>
<option value="appmask">线路1:appmask(慢)</option>
<option value="baidu">线路2:百度(不支持录制和下载)</option>
<option value="wsrv">线路3:wsrv.nl(不稳定)</option>
<option value="">线路4:原始地址(不支持 HTTPS)</option>
<option value="mscnmc">线路5:非常慢</option>
</select>
<button class="btn btn-primary" @click="createSequence" :disabled="recording">
<template v-if="loading">
<span class="loading loading-spinner"></span>
加载中 {{ loadedSeq.length }} {{ "/" }} {{ seqs.length }}
</template>
<template v-else>
加载
</template>
</button>
</div>
<div class="preview flex-auto flex flex-col items-center w-full pt-6" v-if="loadedSeq && loadedSeq.length">
<div class="form flex-none w-full flex flex-row py-4 gap-4 items-center xl:max-w-[1200px]">
<div class=" btn btn-outline">
{{ loadedArea?.name }}
</div>
<div class="btn btn-outline font-mono">
{{ formatTime(new Date(parseInt(currentView))) }}
</div>
<input type="range" class="range range-primary flex-auto" :min="loadedSeq[0].ts"
:max="loadedSeq[loadedSeq.length - 1].ts" :step="loadedArea?.interval" v-model="currentView"
:disabled="recording">
<button type="button" class="btn btn-primary" :disabled="loading" @click="record">
<template v-if="recording">
<span class="loading loading-spinner"></span>
录制中
</template>
<template v-else>
录制
</template>
</button>
<button type="button" class="btn btn-primary" :disabled="loading" @click="zipDownload">
<template v-if="zipLoading">
<span class="loading loading-spinner"></span>
打包中
</template>
<template v-else>
归档下载
</template>
</button>
</div>
<div class="render flex-auto box-border border-primary border w-full xl:max-w-[1200px]">
<svg :viewBox="`0 0 ${loadedArea?.width} ${loadedArea?.height}`" xmlns="http://www.w3.org/2000/svg"
class="w-full">
<foreignObject x="0" y="0" :width="loadedArea?.width" :height="loadedArea?.height">
<canvas ref="ImageCanvas" :height="loadedArea?.height" :width="loadedArea?.width" class="canvas"></canvas>
</foreignObject>
</svg>
</div>
</div>
</div>
</template>