-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
160 lines (136 loc) · 5.57 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video to Colored ASCII Art Converter</title>
<style>
* {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
#ascii-art {
font-family: monospace;
white-space: pre;
font-size: 8px;
}
.ascii-char {
display: inline-block;
width: 8px;
height: 8px;
line-height: 8px;
}
button, input[type="file"] {
border: none;
padding: 10px;
cursor: pointer;
font-weight: bold;
box-shadow: 0 0 10px rgb(201, 201, 201) inset;
border-radius: 10px;
}
</style>
</head>
<body>
<h1>Video to Colored ASCII Art Converter</h1>
<span style="color: red;">To view another video reload the page</span><br><br>
<span>Load the video: </span>
<input type="file" id="videoInput" accept="video/*" onchange="handleVideo()">
<button onclick="exportFramesToFile()">Download the export</button>
<button onclick="importFramesFromFile()">Import from a export file</button>
<div id="ascii-art"></div>
<a href="http://safakamali.ir" target="_blank"><h2>Created by Mohammad Safa Kamali: safakamali.ir</h2></a>
<script>
const asciiArtDiv = document.getElementById('ascii-art');
const videoInput = document.getElementById('videoInput');
let exportFrames = [];
function handleVideo() {
const file = videoInput.files[0];
if (file) {
const video = document.createElement('video');
video.src = URL.createObjectURL(file);
video.play();
video.addEventListener('play', function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 50;
canvas.height = 50;
setInterval(function () {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
const asciiArt = convertToColoredAscii(imageData, canvas.width);
exportFrames.push(imageData);
asciiArtDiv.innerHTML = asciiArt;
}, 100); // Update every 100 milliseconds
});
}
}
function convertToColoredAscii(imageData, width) {
const asciiChars = ['@', '#', 'S', '%', '?', '*', '+', ';', ':', ',', '.'];
let asciiArt = '';
let _length;
if ((typeof imageData[0])=='number') {
_length = Object.keys(imageData).length
} else {
_length = imageData.length
}
for (let i = 0; i < _length; i += 4) {
const avgBrightness = (imageData[i] + imageData[i + 1] + imageData[i + 2]) / 3;
const asciiIndex = Math.round((avgBrightness / 255) * (asciiChars.length - 1));
const color = `rgb(${imageData[i]}, ${imageData[i + 1]}, ${imageData[i + 2]})`;
const span = `<span class="ascii-char" style="background-color: ${color};">${asciiChars[asciiIndex]}</span>`;
asciiArt += span;
if ((i / 4) % width === 0 && i !== 0) {
asciiArt += '<br>';
}
}
return asciiArt;
}
function exportFramesToFile() {
const blob = new Blob([JSON.stringify(exportFrames)], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'exportedFrames.json';
link.click();
}
function importFramesFromFile() {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.json';
fileInput.addEventListener('change', handleImportFile);
fileInput.click();
}
function handleImportFile(event) {
const fileInput = event.target;
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
try {
exportFrames = JSON.parse(e.target.result);
playImportedFrames();
alert('Frames imported successfully!');
} catch (error) {
alert(error)
alert('Error importing frames. Please make sure the file is a valid JSON.');
}
};
reader.readAsText(file);
}
}
function playImportedFrames() {
let frameIndex = 0;
function renderNextFrame() {
if (frameIndex < exportFrames.length) {
const imageData = exportFrames[frameIndex];
const asciiArt = convertToColoredAscii(imageData, 50);
asciiArtDiv.innerHTML = asciiArt;
frameIndex++;
if (frameIndex < exportFrames.length) {
setTimeout(renderNextFrame, 100);
}
}
}
renderNextFrame();
}
</script>
</body>
</html>