-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
230 lines (187 loc) · 6.36 KB
/
script.js
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
// Used for caching the script files
if (navigator.serviceWorker) {
navigator.serviceWorker.register('./sw.js')
.then(() => {
console.log("service worker registered");
})
}
const ffmpeg = new ffmpegEncoder();
let fileInput;
let startTimeEl;
let endTimeEl;
let trimBtn;
let currentFile;
let inputVideo = document.getElementById('inputVideo');
let loadFile = function (file) {
return new Promise(function (resolve, reject) {
const fileReader = new FileReader();
fileReader.onload = function (e) {
resolve(this.result);
};
fileReader.readAsArrayBuffer(file);
});
};
const secondsToTimeCode = function (timeInSeconds) {
function pad(number) {
return (number <= 9) ? '0' + number : number;
}
const hours = Math.floor(timeInSeconds / 3600)
const minutes = Math.floor((timeInSeconds - (hours * 3600)) / 60) % 60;
const seconds = timeInSeconds % 60;
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
};
const timeCodeToSeconds = function (timeCode) {
let timeParts = timeCode.split(':');
return (parseFloat(timeParts[2])
+ parseInt(timeParts[1] * 60)
+ parseInt(timeParts[0] * 3600));
};
const prepare = function (file) {
document.body.classList.add('picking');
let urlToVideo = URL.createObjectURL(file);
inputVideo = document.getElementById('inputVideo');
inputVideo.onloadedmetadata = function () {
endTimeEl.value = secondsToTimeCode(inputVideo.duration);
};
inputVideo.ondurationchange = function () {
endTimeEl.value = secondsToTimeCode(inputVideo.duration);
};
console.log('before')
inputVideo.src = urlToVideo;
console.log(inputVideo)
};
const start = function (file, startTime, endTime) {
ffmpeg.reset();
document.body.classList.add('working');
ffmpeg.videoReady.then(function (data) {
// the video has completed processing
console.log("Video trimming done");
document.body.classList.remove('working');
var buffer = data.MEMFS[0].data;
const trimmedVideoBlob = new Blob([buffer], { type: "video/mp4" });
console.log("Blob creation done");
console.log("--- Blob ", trimmedVideoBlob);
console.log("Triggering download...");
download(trimmedVideoBlob);
});
const logEl = document.getElementById('log');
logEl.innerText = '';
ffmpeg.stderr = function (msg) {
logEl.innerText += msg.data + "\n";
logEl.scrollTop = logEl.scrollHeight;
};
ffmpeg.ready.then(function () {
//the framework is ready
console.log('getting ready');
return loadFile(file);
})
.then(function (arrayBuffer) {
// We have the file that the user has input, now load the
console.log('ready to run');
ffmpeg.run([
{ name: file.name, data: arrayBuffer }
],
startTime,
endTime
);
});
};
const startExternal = (file, startTime, endTime) => {
currentFile = file;
prepare(file);
}
Comlink.expose(startExternal, window);
const selectTime = function (e) {
var element = e.target;
startTimeEl.classList.remove('editing');
endTimeEl.classList.remove('editing');
element.classList.add('editing');
// Position the video to the current time
inputVideo.currentTime = timeCodeToSeconds(element.value);
// Only one text box will
inputVideo.ontimeupdate = function () {
element.value = secondsToTimeCode(inputVideo.currentTime);
};
};
const updateTime = function (e) {
var element = e.target;
// Position the video to the current time
inputVideo.currentTime = timeCodeToSeconds(element.value);
};
const validateStartLessThanEndTimes = function () {
const start = timeCodeToSeconds(startTimeEl.value);
const end = timeCodeToSeconds(endTimeEl.value);
return (start < end);
};
const validateWithingConstrains = function (element) {
const seconds = timeCodeToSeconds(element.value);
return (seconds >= 0 && seconds <= inputVideo.duration);
};
onload = function () {
ffmpeg.ready.then(function () {
document.body.classList.remove('loading');
});
fileInput = document.getElementById("videoFile");
fileInput.onchange = function (e) {
currentFile = e.target.files[0];
prepare(currentFile);
};
startTimeEl = document.getElementById("startTime");
endTimeEl = document.getElementById("endTime");
trimBtn = document.getElementById("trim");
startTimeEl.addEventListener('click', selectTime);
endTimeEl.addEventListener('click', selectTime);
startTimeEl.addEventListener('change', updateTime);
endTimeEl.addEventListener('change', updateTime);
trimBtn.addEventListener("click", function () {
start(currentFile, startTimeEl.value, endTimeEl.value);
});
};
function download(blob) {
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'test.mp4';
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}
ondragover = function (e) {
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}
if (e.dataTransfer.items[0].type.indexOf('video/') != 0
&& e.dataTransfer.files[0].type.indexOf('video/') != 0) {
document.body.classList.add('badfile');
return false;
}
document.body.classList.add('goodfile');
e.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object.
return false;
}
ondragleave = function () {
document.body.classList.remove('goodfile');
document.body.classList.remove('badfile');
}
ondragend = function () {
document.body.classList.remove('goodfile');
document.body.classList.remove('badfile');
}
const outputBuffer = new ArrayBuffer();
ondrop = function (e) {
if (e.stopPropagation) {
e.stopPropagation(); // stops the browser from redirecting.
}
document.body.classList.remove('badfile');
document.body.classList.remove('goodfile');
if (e.dataTransfer.items[0].type.indexOf('video/') != 0) {
return false;
}
currentFile = e.dataTransfer.files[0];
prepare(currentFile);
return false;
};