-
Notifications
You must be signed in to change notification settings - Fork 3
/
media_drop.html
731 lines (611 loc) · 19 KB
/
media_drop.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
<!DOCTYPE HTML>
<html>
<head>
<title>MediaDrop - @kosso</title>
<!--
MediaDrop.
No dependencies.
Provides a simple interface to drag and drop a single image, audio or video file onto the browser
and read the information from it and providing the data for uploading somewhere.
- Image files do nothing but ready the file for whatever you need.
- Audio files will automatically create an audio waveform PNG file.
- Video files allow the user to to choose a frame and create a thumbnail image.
Pretty damn cool, if you ask me!
... which you didn't.
But hey.. At least you're reading this! :)
: @kosso : December 2016
: License : Attribution 3.0 Unported (CC BY 3.0) : https://creativecommons.org/licenses/by/3.0/
-->
<script>
/* Options */
var wave_col = '#333'; // Audio waveform colour
var wave_width = 1000; // Audio waveform width
var wave_height = 500; // Audio waveform height
var el_drop, el_media, el_status, el_fname, el_ftype, el_fsize, el_fduration, el_time, el_time_val, el_media_width, el_media_height, el_thumb, el_wave, f_type, f_name, f_width, f_height, f_size, f_duration, f_duration_fmt, file_blob, audioCtx, logs, btn_clear_logs, busy, busy_cont, time_now = 0;
function init(){
if (!(window.File || window.FileReader || window.Blob)) {
alert('The required File APIs are not fully supported in this browser.');
return;
}
if (!(window.AudioContext || window.webkitAudioContext)) {
alert('The required AudioContext APIs are not fully supported in this browser.');
return;
}
logs = document.getElementById('logs');
btn_clear_logs = document.getElementById('clear');
el_status = document.getElementById('status');
el_drop = document.getElementById('droptarget');
el_fname = document.getElementById('f_name');
el_ftype = document.getElementById('f_type');
el_fsize = document.getElementById('f_size');
el_fduration = document.getElementById('f_duration');
el_wave = document.getElementById('wave');
// stop file drops on the rest of the page
window.addEventListener('dragover',function(e){
e.preventDefault();
},false);
window.addEventListener('drop',function(e){
e.preventDefault();
},false);
console.log('ready');
btn_clear_logs.addEventListener('click',function(e){
logs.innerHTML = '<h6 style="margin:0">OUTPUT LOGS:</h6>';
});
}
function createBusySpinner(){
busy_cont = document.createElement('div');
busy_cont.id = 'busy_cont';
busy = document.createElement('div');
busy.id = 'busy';
busy.className = 'spinner';
var dot = document.createElement('div');
dot.id = 'dot';
busy.appendChild(dot);
busy_cont.appendChild(busy);
return busy_cont;
}
function allowDrop(ev) {
// console.log('dragover: ', ev.dataTransfer.items.length);
if(ev.dataTransfer.items.length === 1){
el_drop.style.borderColor = 'green';
ev.dataTransfer.dropEffect = 'copy';
}
ev.preventDefault();
}
function drag(ev) {
// console.log('drag: ', ev);
// ev.dataTransfer.setData("text", ev.target.id);
}
function dragout(){
el_drop.style.borderColor = '#ddd';
}
function stat(t){
el_status.innerHTML = t;
}
function msg(t){
var el_msg = document.createElement('p');
el_msg.innerHTML = t;
el_drop.appendChild(el_msg);
}
function drop(ev) {
ev.preventDefault();
el_drop.style.borderColor = '#ddd';
var dt = ev.dataTransfer;
var files = dt.files;
if(files.length === 0){
return; /* this can be triggered when accidentally grabbing an already loaded image in the drop target */
}
if(files.length > 1){
removeAllChildren(el_drop);
msg('one file only please');
return;
}
file_blob = null;
el_thumb = null;
time_now = 0;
removeAllChildren(el_drop);
el_fduration.innerHTML = el_fsize.innerHTML = el_fname.innerHTML = el_ftype.innerHTML = '';
busy_cont = createBusySpinner()
el_drop.appendChild(busy_cont);
msg('loading file');
stat('file dropped');
var _blob;
for (var i=0; i<files.length; i++) {
var file = files[i];
// console.log('loading file: ', file);
// file.size
// file.type
// file.name
f_name = file.name;
f_type = file.type;
f_size = file.size;
el_fname.innerHTML = f_name;
el_fsize.innerHTML = f_size + ' bytes';
el_ftype.innerHTML = f_type;
console.log('file: ', file);
file_blob = URL.createObjectURL(file);
// console.log('file_blob: ', file_blob);
removeAllChildren(el_drop);
if(f_type === 'image/jpeg' || f_type === 'image/gif' || f_type === 'image/png'){
busy_cont = createBusySpinner();
el_drop.appendChild(busy_cont);
el_media = document.createElement('img');
el_media.id = 'media';
el_media.src = file_blob;
el_fduration.innerHTML = '-';
el_drop.appendChild(el_media);
el_media.addEventListener('load', function(e){
// console.log('image loaded: ', e);
el_fduration.innerHTML = el_media.naturalWidth + ' x ' + el_media.naturalHeight;
stat('image loaded');
el_drop.removeChild(busy_cont);
objectUrlToBlob(file_blob, function(blob){
file_blob_data = blob;
console.log(file_blob_data);
// Image file ready (file_blob / file_blob_data)
});
});
} else if(f_type === 'video/mp4'){
stat('loading video');
busy_cont = createBusySpinner();
el_drop.appendChild(busy_cont);
el_media = document.createElement('video');
el_media.setAttribute('controls', true);
var source = document.createElement('source');
source.type = f_type;
source.src = file_blob;
el_media.appendChild(source);
el_media.addEventListener('durationchange', function(e){
// console.log('video durationchange: ', e);
f_duration = e.target.duration; // in float seconds
f_duration_fmt = SecondsTohhmmss(Math.floor(f_duration - 0.5));
el_fduration.innerHTML = f_duration + ' seconds ['+f_duration_fmt+']';
});
el_media.addEventListener('load', function(e){
//console.log('load: ', e);
});
el_media.addEventListener('timeupdate', function(e){
// console.log(e);
time_now = e.target.currentTime;
el_time_val.innerHTML = SecondsTohhmmss(time_now);
});
var meta_loaded = false;
el_media.onloadedmetadata = function(e) {
if(meta_loaded===true){
return; // just in case
}
// console.log('video metadata ready: ', e);
meta_loaded = true;
f_width = e.target.videoWidth;
f_height = e.target.videoHeight;
el_fname.innerHTML += ' ['+f_width+' x '+f_height+']';
el_media.style.width = '100%';
el_time = document.createElement('div');
el_time.className = 'op left-align';
el_time.id = 'time';
el_time_val = document.createElement('span');
el_time_val.id = 'time_val';
el_time_val.innerHTML = SecondsTohhmmss(time_now);
btn_frame = document.createElement('button');
btn_frame.className = 'btn';
btn_frame.id = 'render_frame';
btn_frame.innerHTML = 'choose time and render frame';
btn_frame.addEventListener('click', function(e){
renderFrame();
});
el_drop.removeChild(busy_cont);
el_time.appendChild(btn_frame);
el_time.appendChild(el_time_val);
el_drop.appendChild(el_time);
//el_media.play();
objectUrlToBlob(file_blob, function(blob){
file_blob_data = blob;
console.log(file_blob_data);
// Video file ready (file_blob / file_blob_data)
});
};
el_drop.appendChild(el_media);
el_media.id = 'media';
el_media.load();
} else if(f_type.split('/')[0] === 'audio' ){
stat('loading audio');
busy_cont = createBusySpinner();
el_drop.appendChild(busy_cont);
el_media = document.createElement('audio');
el_media.setAttribute('controls', true);
var source = document.createElement('source');
source.type = f_type;
source.src = file_blob;
el_media.appendChild(source);
el_media.addEventListener('durationchange', function(e){
//console.log('audio durationchange: ', e);
f_duration = e.target.duration; // in float seconds
f_duration_fmt = SecondsTohhmmss(f_duration);
el_fduration.innerHTML = f_duration + ' seconds ['+f_duration_fmt+']';
el_drop.removeChild(busy_cont);
stat('audio loaded');
objectUrlToBlob(file_blob, function(blob){
file_blob_data = blob;
console.log(file_blob_data);
blobToArrayBuffer(file_blob_data, function(audio_data){
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
audioCtx.decodeAudioData(audio_data, function(audio_buffer) {
log('duration: ' + audio_buffer.duration);
log('size: ' + audio_buffer.length);
log('channels : ' + audio_buffer.numberOfChannels);
log('sample rate: ' + audio_buffer.sampleRate);
drawWaveform( audio_buffer, wave_col, wave_width, wave_height );
// Audio file ready... (file_blob or file_blob_data)
});
});
});
});
el_drop.appendChild(el_media);
el_media.id = 'media';
el_media.load();
} else {
msg('unsupported file type<br><br>please try again');
}
}
return false;
}
function drawWaveform( buffer, col, w, h ) {
col = col || 'green';
w || 1000;
h || 1000;
var canvas_wave = document.createElement('canvas');
canvas_wave.width = w;
canvas_wave.height = h;
var wave_ctx = canvas_wave.getContext('2d');
wave_ctx.fillStyle = col;
var data = buffer.getChannelData( 0 );
var step = Math.ceil( data.length / w );
var amp = h / 2;
for(var i = 0; i < w; i++){
var min = 1.0;
var max = -1.0;
for (var j = 0; j < step; j++) {
var datum = data[ (i * step) + j ];
if (datum < min)
min = datum;
if (datum > max)
max = datum;
}
wave_ctx.fillRect(i, (1 + min) * amp, 1, Math.max(1, (max - min) * amp));
}
// create a PNG from the canvas
canvas_wave.toBlob(function(blob){
console.log('rendered waveform image blob: ', blob);
var _thumb = document.createElement('img');
_thumb.id = 'waveform';
var wave_ = URL.createObjectURL(blob);
_thumb.src = wave_;
el_drop.appendChild(_thumb);
// blob = null;
log('waveform rendered: <a href="'+wave_+'" target="_blank">view</a> - <a download="wave_2_'+f_name.split( f_name.split('.')[1] ).join('png')+'" href="'+wave_+'" target="_blank">save</a>');
// URL.revokeObjectURL(blob)
canvas_wave = null;
wave_ctx = null;
buffer = null;
}, 'image/png');
}
function renderFrame(){
// console.log('render: ', time_now);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = f_width;
canvas.height = f_height;
ctx.drawImage(el_media, 0, 0);
canvas.toBlob(function(blb){
if(el_thumb===null){
el_thumb = document.createElement('img');
el_thumb.className = 'thumb';
}
var frame_image = URL.createObjectURL(blb);
el_thumb.src = frame_image;
el_drop.appendChild(el_thumb);
log('frame @['+SecondsTohhmmss(time_now)+']: <a href="'+frame_image+'" target="_blank">view</a> - <a download="frame_'+Math.round(time_now * 1000)+'_'+f_name.split( f_name.split('.')[1] ).join('png')+'" href="'+frame_image+'" target="_blank">save</a>');
}, 'image/png');
}
function objectUrlToBlob(blob_url, callback){
var xhr = new XMLHttpRequest();
xhr.open('GET', blob_url, true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
callback(this.response);
}
};
xhr.send();
}
function blobToArrayBuffer(blob, callback) {
console.log('blobToArrayBuffer: ', blob);
var reader = new FileReader();
reader.addEventListener('progress', function (e) {
console.log('blobToArrayBuffer progress : ', e);
});
reader.addEventListener('load', function (e) {
console.log('blobToArrayBuffer loaded : ', e);
callback(e.target.result);
});
reader.addEventListener('error', function (e) {
console.log('blobToArrayBuffer error', e);
});
reader.readAsArrayBuffer(blob);
}
// Canvas.toBlob() polyfill : https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#Polyfill
if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
value: function (callback, type, quality) {
var _dat = this.toDataURL(type, quality);
var binStr = atob( _dat.split(',')[1] ),
len = binStr.length,
arr = new Uint8Array(len);
for (var i=0; i<len; i++ ) {
arr[i] = binStr.charCodeAt(i);
}
_dat = null;
binStr = null;
callback( new Blob( [arr], {type: type || 'image/png'} ) );
}
});
}
function log(message) {
var li = document.createElement('li');
li.innerHTML = message;
logs.appendChild(li);
}
function removeAllChildren(theParent){
// Create the Range object
var rangeObj = new Range();
// Select all of theParent's children
rangeObj.selectNodeContents(theParent);
// Delete everything that is selected
rangeObj.deleteContents();
}
function dataURItoBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var blob = new Blob([ab], {type: mimeString});
return blob;
}
var SecondsTohhmmss = function(totalSeconds) {
var hours = Math.floor(totalSeconds / 3600);
var minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
var seconds = totalSeconds - (hours * 3600) - (minutes * 60);
// round seconds
seconds = Math.round(seconds * 100) / 100
var result = (hours < 10 ? "0" + hours : hours);
result += ":" + (minutes < 10 ? "0" + minutes : minutes);
result += ":" + (seconds < 10 ? "0" + seconds : seconds);
return result;
}
</script>
<style type="text/css">
img#media{
width: 100%;
}
audio#media{
width: 100%;
height: 40px;
z-index: 1;
}
video#media{
z-index: 1;
}
#droptarget{
text-align: center;
border: 4px dashed #ddd;
padding: 6px;
margin: 6px;
font-family: monospace;
font-weight: bold;
font-size: 20px;
position: relative;
background: #eee;
}
#droptarget > p{
padding: 20px;
}
.op{
border: 1px solid #777;
padding: 6px;
margin: 6px;
background: #eee;
color: #222;
font-family: monospace;
}
#status{
background: #222;
color: #eee;
font-weight: bold;
}
#busy{
position: relative;
width: 60px;
height: 60px;
background: #333;
border-radius: 30px;
text-align: center;
overflow: hidden;
left: calc(50% - 30px);
top: calc(50% - 30px);
}
#dot{
position: relative;
width: 10px;
height: 10px;
margin: 12px;
background: #fff;
border-radius: 5px;
}
#busy_cont{
top:0px;
left:0px;
right: 0px;
bottom: 0px;
z-index: 1000;
background: rgba(255,255,255,0.8);
width: 100%;
height: 100%;
position: absolute;
}
li{
line-height: 1.8em;
}
.thumb{
width: 100%;
}
#time{
font-size: 18px;
}
@-webkit-keyframes webkit-anim-rotate {
0% {
transform: rotate(0);
-webkit-transform: rotate(0);
}
100% {
transform: rotate(359deg);
-webkit-transform: rotate(359deg);
}
}
@-moz-keyframes moz-anim-rotate {
0% {
-moz-transform: rotate(0);
}
100% {
-moz-transform: rotate(359deg);
}
}
@-ms-keyframes ms-anim-rotate {
0% {
-ms-transform: rotate(0);
}
100% {
-ms-transform: rotate(359deg);
}
}
@-o-keyframes o-anim-rotate {
0% {
-o-transform: rotate(0);
}
100% {
-o-transform: rotate(359deg);
}
}
@keyframes anim-rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(359deg);
}
}
.btn{
font-family: sans-serif;
color: white;
background: #333;
font-size: 13px;
}
#render_frame{
float: right;
}
.left-align{
text-align: left;
}
.spinner {
-webkit-animation: webkit-anim-rotate 2s infinite linear;
-moz-animation: moz-anim-rotate 2s infinite linear;
-ms-animation: ms-anim-rotate 2s infinite linear;
-o-animation: o-anim-rotate 2s infinite linear;
animation: anim-rotate 2s infinite linear;
}
#wave_holder{
margin-left: 0px;
margin-right: 0px;
border:0px solid #ccc;
height: 800px;
top:100px;
position: relative;
visibility: hidden;
}
#wave{
left:0;
right:0;
text-align: left;
position: absolute;
top:0px;
height: 800px;
overflow:hidden;
}
#wave_centre{
width: 100%;
height: 1px;
background: #888;
position: absolute;
top:50%;
}
img#waveform{
width: 100%;
}
@media all and (min-width:600px) {
#droptarget{
width:60%;
float: right;
}
#info{
width: 34%;
float: left;
}
}
#lnk{
position: absolute;
bottom: 10px;
right:10px;
}
button#clear{
background: #660000;
color: white;
width: 22px;
height: 22px;
border-radius: 11px;
font-size: 11px;
line-height: 6px;
text-align: center;
padding: 0px;
margin-left: 4px;
outline: none;
}
</style>
</head>
<body onload="init()">
<div id="droptarget" ondrop="drop(event)" ondragleave="dragout()" ondragover="allowDrop(event)">
<p>
drop a media file here
<span style="font-size:12px"><br>(jpg, gif, png, mp3, wav, m4a, ogg, mp4, m4v, avi) </span>
</p>
</div>
<div id="info">
<div class="op" id="f_name">.</div>
<div class="op" id="f_type">.</div>
<div class="op" id="f_size">.</div>
<div class="op" id="f_duration">.</div>
<div class="op" id="status">.</div>
<div class="op" id="logs">
<h6 style="margin:0">OUTPUT LOGS:</h6>
</div>
<div id="clear-logs"><button id="clear">x</button></div>
</div>
<div id="lnk"><a href="http://github.com/kosso">@kosso</a></div>
</body>
</html>