-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
219 lines (180 loc) · 5.3 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<body>
<style>
html,
body {
margin: 0;
padding: 0;
min-height: 100vh;
width: 100%;
background: #fff;
}
body.dark {
background: rgb(44, 44, 44);
color: #fff;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.wrapper {
display: flex;
width: 100%;
cursor: pointer;
justify-content: center;
flex-wrap: wrap;
}
.info {
margin: auto;
font-size: 1.3rem;
text-align: center;
}
.video-cont {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
max-width: 600px;
aspect-ratio: 16 / 9;
box-sizing: border-box;
padding: 20px;
}
.filename {
padding-bottom: 15px;
}
.filename .copy {
font-weight: bold;
;
}
video {
width: 100%;
display: none;
}
input[type="file"] {
display: none;
}
.theme-switcher {
display: flex;
gap: .5rem;
position: fixed;
top: 0;
right: 0;
user-select: none;
padding: 1rem;
pointer-events: none;
}
.theme-button {
pointer-events: all;
}
.theme-button.active {
font-weight: bold;
color: rgb(36, 181, 218);
}
</style>
<label class="wrapper" for="file">
<div class="theme-switcher">
<div class="theme-button" data-theme="auto">Auto</div>
<div class="theme-button" data-theme="light">Light</div>
<div class="theme-button" data-theme="dark">Dark</div>
</div>
<div class="info">Click or drop video here<br><small>(multiple files supported)</small></div>
<input type="file" id="file" multiple accept=".mov,.mp4,.wmv,.avi,.flv,.mkv,.webm,.mpeg,.ogg,.mpg,.ogg" />
</label>
<script>
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
let theme = localStorage.getItem('theme') || 'light';
const themeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
themeMediaQuery.addListener(() => {
if (localStorage.getItem('theme') === 'auto') {
setBodyTheme(themeMediaQuery.matches ? 'dark' : 'light');
}
})
const setBodyTheme = (theme) => {
theme = theme === "auto" ? (themeMediaQuery.matches ? 'dark' : 'light') : theme;
document.body.classList.remove('dark', 'light');
document.body.classList.add(theme);
}
const setTheme = (theme) => {
[...$$('.theme-button')].forEach(node => node.classList.remove('active'));
$(`.theme-button[data-theme=${theme}]`).classList.add('active');
localStorage.setItem('theme', theme);
setBodyTheme(theme);
}
setTheme(theme)
document.addEventListener('click', (event) => {
const $button = event.target.closest('.theme-button');
if ($button) {
event.stopPropagation();
event.preventDefault();
setTheme($button.dataset.theme);
}
})
const selectElement = (elem) => {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(elem);
if (selection) {
selection.removeAllRanges();
selection.addRange(range);
}
};
// https://stackoverflow.com/a/4652513/927631
const aspectRatio = (numerator, denominator) => {
const gcd = (a, b) => b ? gcd(b, a % b) : a;
d = gcd(numerator, denominator);
return [numerator / d, denominator / d].join(":");
}
const createVideo = (file) => {
const filesize = (file.size / 1024 / 1024).toPrecision(3);
const video = document.createElement('video');
video.autoplay = "true";
video.controls = "true";
video.loop = "true";
video.src = URL.createObjectURL(file);
video.style.display = "block";
const videoCont = document.createElement('div');
videoCont.className = 'video-cont';
videoCont.appendChild(video);
video.onloadedmetadata = () => {
const name = file.name;
const filenameDiv = document.createElement('div');
filenameDiv.className = "filename";
const { videoWidth, videoHeight } = video;
filenameDiv.innerHTML = `<span class="copy">${name}</span> <small>(${videoWidth}x${videoHeight}, ${aspectRatio(videoWidth, videoHeight)}, ${filesize}Mb)</small>`;
videoCont.prepend(filenameDiv);
}
return videoCont;
}
const loadVideoFiles = (files) => {
const wrapper = $(".wrapper");
wrapper.style.cursor = "auto";
wrapper.replaceChildren();
for (const file of files) {
wrapper.appendChild(createVideo(file));
}
}
const input = $('input[type="file"]');
input.addEventListener("change", event => {
loadVideoFiles(event.target.files);
})
// handle drag&drop
window.addEventListener("dragover", event => {
event.preventDefault()
})
window.addEventListener("drop", event => {
event.preventDefault()
loadVideoFiles(event.dataTransfer.files);
})
document.addEventListener('click', (event) => {
if (event.target.closest('.copy')) {
selectElement(event.target);
document.execCommand('copy');
}
});
</script>
</body>
</html>