-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrim.html
229 lines (200 loc) · 7.73 KB
/
trim.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Video to GIF</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.5.12/cropper.min.css" />
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
}
.container {
text-align: center;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 80%;
max-width: 600px;
}
video {
width: 100%;
max-width: 600px;
height: auto;
margin-bottom: 10px;
}
#controls {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
input[type="range"] {
width: 100%;
}
button {
background-color: #0068de;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1>Trim and Convert Video to GIF</h1>
<input type="file" id="fileInput" accept="video/*">
<div id="videoContainer" style="display: none;">
<video id="videoPreview" controls></video>
<div id="controls">
<label for="startRange">Start:</label>
<input type="range" id="startRange" min="0" step="0.1">
<span id="startTime">0.0s</span>
<label for="endRange">End:</label>
<input type="range" id="endRange" min="0" step="0.1">
<span id="endTime">0.0s</span>
</div>
<button id="trimBtn">Trim Video</button>
<button id="convertBtn" style="display: none;">Convert to GIF</button>
</div>
<div id="status"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gif.js/0.2.0/gif.js"></script>
<script>
const fileInput = document.getElementById('fileInput');
const videoPreview = document.getElementById('videoPreview');
const videoContainer = document.getElementById('videoContainer');
const startRange = document.getElementById('startRange');
const endRange = document.getElementById('endRange');
const startTimeDisplay = document.getElementById('startTime');
const endTimeDisplay = document.getElementById('endTime');
const trimBtn = document.getElementById('trimBtn');
const convertBtn = document.getElementById('convertBtn');
const status = document.getElementById('status');
let videoDuration = 0;
let startTime = 0;
let endTime = 0;
fileInput.addEventListener('change', function (e) {
const file = e.target.files[0];
if (file) {
const videoUrl = URL.createObjectURL(file);
videoPreview.src = videoUrl;
videoContainer.style.display = 'block';
videoPreview.onloadedmetadata = function () {
videoDuration = videoPreview.duration;
startRange.max = videoDuration;
endRange.max = videoDuration;
endRange.value = videoDuration;
startTime = 0;
endTime = videoDuration;
updateRangeDisplays();
};
}
});
startRange.addEventListener('input', function () {
startTime = parseFloat(startRange.value);
if (startTime >= endTime) {
endTime = startTime;
endRange.value = endTime;
}
updateRangeDisplays();
});
endRange.addEventListener('input', function () {
endTime = parseFloat(endRange.value);
if (endTime <= startTime) {
startTime = endTime;
startRange.value = startTime;
}
updateRangeDisplays();
});
function updateRangeDisplays() {
startTimeDisplay.textContent = `${startTime.toFixed(1)}s`;
endTimeDisplay.textContent = `${endTime.toFixed(1)}s`;
}
trimBtn.addEventListener('click', function () {
trimVideo(videoPreview, startTime, endTime);
convertBtn.style.display = 'block';
});
convertBtn.addEventListener('click', convertToGif);
function trimVideo(videoElement, start, end) {
videoElement.currentTime = start;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const duration = end - start;
const fps = 10;
const totalFrames = Math.floor(duration * fps);
let currentFrame = 0;
canvas.width = videoElement.videoWidth;
canvas.height = videoElement.videoHeight;
function captureFrame() {
if (currentFrame < totalFrames) {
videoElement.currentTime = start + (currentFrame / fps);
videoElement.onseeked = () => {
ctx.drawImage(videoElement, 0, 0, canvas.width, canvas.height);
currentFrame++;
captureFrame();
};
} else {
console.log('Trimming complete');
}
}
captureFrame();
}
function convertToGif() {
status.textContent = 'Converting...';
const gif = new GIF({
workers: 2,
quality: 10,
width: videoPreview.videoWidth,
height: videoPreview.videoHeight,
});
videoPreview.currentTime = startTime;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = videoPreview.videoWidth;
canvas.height = videoPreview.videoHeight;
const duration = endTime - startTime;
const fps = 10;
const totalFrames = Math.floor(duration * fps);
let currentFrame = 0;
function addFrame() {
if (currentFrame < totalFrames) {
videoPreview.currentTime = startTime + (currentFrame / fps);
videoPreview.onseeked = () => {
ctx.drawImage(videoPreview, 0, 0, canvas.width, canvas.height);
gif.addFrame(ctx, { copy: true, delay: 100 / fps });
currentFrame++;
status.textContent = `Converting... ${Math.round((currentFrame / totalFrames) * 100)}%`;
addFrame();
};
} else {
gif.render();
}
}
gif.on('finished', function (blob) {
const gifUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = gifUrl;
a.download = 'output.gif';
a.textContent = 'Download GIF';
status.textContent = 'Conversion complete. ';
status.appendChild(a);
});
addFrame();
}
</script>
</body>
</html>