-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.js
More file actions
212 lines (192 loc) · 6.31 KB
/
Copy pathmetadata.js
File metadata and controls
212 lines (192 loc) · 6.31 KB
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
/**
* Media probing for VWall: browser hints + optional MuStream ffprobe bridge.
* Run locally: `mustream probe-serve` → http://127.0.0.1:18765/v1/probe?url=…
*/
(function (global) {
const PROBE_BASE =
global.localStorage.getItem("vwallProbeBase") ||
global.localStorage.getItem("mustreamProbeBase") ||
"http://127.0.0.1:18765";
function fmtBytes(n) {
if (n == null || !Number.isFinite(n)) return "—";
if (n < 1024) return `${n} B`;
if (n < 1048576) return `${(n / 1024).toFixed(1)} KB`;
if (n < 1073741824) return `${(n / 1048576).toFixed(1)} MB`;
return `${(n / 1073741824).toFixed(2)} GB`;
}
function fmtBitrate(bps) {
if (bps == null || !Number.isFinite(bps)) return "—";
if (bps < 1000) return `${Math.round(bps)} bps`;
if (bps < 1e6) return `${(bps / 1000).toFixed(1)} Kbps`;
return `${(bps / 1e6).toFixed(2)} Mbps`;
}
async function headProbe(url) {
try {
const res = await fetch(url, { method: "HEAD", mode: "cors" });
const len = res.headers.get("content-length");
const type = res.headers.get("content-type");
return {
size_bytes: len ? parseInt(len, 10) : null,
content_type: type || null
};
} catch {
return {};
}
}
function probeImage(url) {
return new Promise((resolve) => {
const img = new Image();
img.crossOrigin = "anonymous";
img.onload = () =>
resolve({
width: img.naturalWidth,
height: img.naturalHeight,
duration_sec: null
});
img.onerror = () => resolve(null);
img.src = url;
});
}
function probeVideo(url) {
return new Promise((resolve) => {
const v = document.createElement("video");
v.preload = "metadata";
v.crossOrigin = "anonymous";
const done = (meta) => {
v.removeAttribute("src");
v.load();
resolve(meta);
};
v.onloadedmetadata = () =>
done({
width: v.videoWidth,
height: v.videoHeight,
duration_sec: Number.isFinite(v.duration) ? v.duration : null
});
v.onerror = () => done(null);
v.src = url;
});
}
function probeAudio(url) {
return new Promise((resolve) => {
const a = document.createElement("audio");
a.preload = "metadata";
a.crossOrigin = "anonymous";
const done = (meta) => {
a.removeAttribute("src");
a.load();
resolve(meta);
};
a.onloadedmetadata = () =>
done({
duration_sec: Number.isFinite(a.duration) ? a.duration : null
});
a.onerror = () => done(null);
a.src = url;
});
}
async function fetchMuStreamFfprobe(url) {
try {
const u = `${PROBE_BASE}/v1/probe?url=${encodeURIComponent(url)}`;
const res = await fetch(u, { mode: "cors" });
if (!res.ok) return null;
return await res.json();
} catch {
return null;
}
}
function mergeSummary(browser, head, ms) {
const s = { ...(browser || {}), ...(head || {}) };
if (ms?.summary && typeof ms.summary === "object") {
Object.assign(s, ms.summary);
}
if (head?.size_bytes && !s.size_bytes) s.size_bytes = head.size_bytes;
if (head?.content_type && !s.content_type) s.content_type = head.content_type;
const br = s.bit_rate || (s.size_bytes && s.duration_sec
? (s.size_bytes * 8) / s.duration_sec
: null);
if (br && !s.bit_rate) s.bit_rate = br;
if (!s.buffer_est_bytes) {
if (s.bit_rate) s.buffer_est_bytes = Math.round((s.bit_rate * 3) / 8);
else if (s.size_bytes) s.buffer_est_bytes = Math.min(s.size_bytes, 2_500_000);
}
return s;
}
function extractExifFromFfprobe(ms) {
const out = {};
const fp = ms?.ffprobe;
const tags = fp?.format?.tags;
if (tags) {
for (const k of ["make", "model", "creation_time", "encoder", "copyright", "title", "comment"]) {
const v = tags[k] || tags[k.toUpperCase()];
if (v) out[k] = v;
}
}
const v0 = fp?.streams?.find((s) => s.codec_type === "video");
if (v0?.tags) {
for (const k of ["camera", "lens", "location", "creation_time"]) {
const v = v0.tags[k];
if (v) out[k] = v;
}
}
return out;
}
async function probeItem(item) {
const url = item.url;
const mt = item.mediaType;
const [head, ms] = await Promise.all([
headProbe(url),
fetchMuStreamFfprobe(url)
]);
let browser = null;
if (mt === "image" || mt === "gif") browser = await probeImage(url);
else if (mt === "video" || mt === "live") browser = await probeVideo(url);
else if (mt === "audio") browser = await probeAudio(url);
const summary = mergeSummary(browser, head, ms);
const exif = extractExifFromFfprobe(ms);
Object.assign(summary, exif);
return {
browser,
head,
mustream: ms,
summary,
exif,
exiftool_skipped: ms?.exiftool_skipped || null
};
}
function formatMetaRows(meta, mediaType) {
if (!meta?.summary) return "";
const s = meta.summary;
const rows = [];
const push = (k, v) => {
if (v != null && v !== "" && v !== "—") rows.push(`<p><span>${k}:</span> ${v}</p>`);
};
push("Dimensions", s.width && s.height ? `${s.width}×${s.height}` : null);
push("Duration", s.duration_sec != null ? `${s.duration_sec.toFixed(2)}s` : null);
push("Size", fmtBytes(s.size_bytes));
push("Bitrate", fmtBitrate(s.bit_rate));
push("Buffer est.", fmtBytes(s.buffer_est_bytes));
push("Video", s.video_codec ? String(s.video_codec).replace(/"/g, "") : null);
push("Audio", s.audio_codec ? String(s.audio_codec).replace(/"/g, "") : null);
push("FPS", s.fps || null);
push("Pixel fmt", s.pix_fmt || null);
push("Encoder", s.format_encoder || s.encoder || null);
push("Created", s.format_creation_time || s.creation_time || null);
push("Camera", s.make && s.model ? `${s.make} ${s.model}` : s.make || s.model || s.camera || null);
push("Lens", s.lens || null);
push("GPS", s.gps || s.location || null);
if (meta.mustream?.error) {
push("ffprobe", `⚠ ${meta.mustream.error}`);
} else if (meta.mustream?.ffprobe) {
push("Probe", "ok");
}
return rows.join("");
}
global.VWallMeta = {
PROBE_BASE,
probeItem,
formatMetaRows,
fmtBytes,
fmtBitrate
};
})(window);