forked from alyssaxuu/screenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoeditor.js
282 lines (251 loc) · 8.51 KB
/
videoeditor.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
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
$(document).ready(function(){
var blobs = recordedBlobs;
var player;
var trimslider = document.getElementById('trimslider');
var removeslider = document.getElementById('removeslider');
var setup = true;
// Show recorded video
if (url == "" || url == null) {
// Show recorded video
var superBuffer = new Blob(recordedBlobs, {
type: 'video/webm'
});
// Create the src url from the blob. #t=duration is a Chrome bug workaround, as the webm generated through Media Recorder has a N/A duration in its metadata, so you can't seek the video in the player. Using Media Fragments (https://www.w3.org/TR/media-frags/#URIfragment-user-agent) and setting the duration manually in the src url fixes the issue.
var url = window.URL.createObjectURL(superBuffer);
}
$("#video").attr("src", url);
$("#g-savetodrive").attr("src", url);
$("#format-select").niceSelect();
// Convert seconds to timestamp
function timestamp(value) {
var sec_num = value;
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
}
// Initialize range sliders
function initRanges() {
noUiSlider.create(trimslider, {
start: [blobs.length],
connect: "upper",
range: {
'min': 0,
'max': blobs.length
}
});
$("#trim-end input").val(timestamp(blobs.length));
noUiSlider.create(removeslider, {
start: [0, blobs.length],
connect: true,
range: {
'min': 0,
'max': blobs.length
}
});
$("#remove-end input").val(timestamp(blobs.length));
}
// Update range values
function updateRanges(blobs) {
trimslider.noUiSlider.updateOptions({
start: [blobs.length],
range: {
'min': 0,
'max': blobs.length
}
});
$("#trim-start input").val(timestamp(0));
$("#trim-end input").val(timestamp(blobs.length));
removeslider.noUiSlider.updateOptions({
start: [0, blobs.length],
range: {
'min': 0,
'max': blobs.length
}
});
$("#remove-start input").val(timestamp(0));
$("#remove-end input").val(timestamp(blobs.length));
window.setTimeout(function(){
player.currentTime = 0;
}, 500)
player.restart();
}
// Reset video
function reset() {
blobs = recordedBlobs;
var superBuffer = new Blob(blobs, {
type: 'video/webm'
});
var url = window.URL.createObjectURL(superBuffer);
$("#video").attr("src", url+"#t="+blobs.length);
updateRanges(blobs);
}
// Trim video between two values
function trim(a, b) {
blobs = blobs.slice(a, b);
var superBuffer = new Blob(blobs, {
type: 'video/webm'
});
var url = window.URL.createObjectURL(superBuffer);
$("#video").attr("src", url+"#t="+blobs.length);
updateRanges(blobs);
}
// Remove part of the video
function remove(a, b) {
var start = blobs.slice(0, a);
var end = blobs.slice(b, blobs.length);
blobs = start.concat(end);
var superBuffer = new Blob(blobs, {
type: 'video/webm'
});
var url = window.URL.createObjectURL(superBuffer);
$("#video").attr("src", url+"#t="+blobs.length);
updateRanges(blobs);
}
// Download video in different formats
function download() {
downloaded = true;
$("#download-label").html(chrome.i18n.getMessage("downloading"))
if ($("#format-select").val() == "mp4") {
ysFixWebmDuration(blobs, blobs.length, function(fixedBlob) {
var superBuffer = new Blob(fixedBlob, {
type: 'video/mp4'
});
var url = window.URL.createObjectURL(superBuffer);
chrome.downloads.download({
url: url
});
$("#download-label").html(chrome.i18n.getMessage("download"))
});
} else if ($("#format-select").val() == "webm") {
ysFixWebmDuration(blobs, blobs.length, function(fixedBlob) {
var superBuffer = new Blob(fixedBlob, {
type: 'video/webm'
});
var url = window.URL.createObjectURL(superBuffer);
chrome.downloads.download({
url: url
});
$("#download-label").html(chrome.i18n.getMessage("download"))
});
} else if ($("#format-select").val() == "gif") {
var superBuffer = new Blob(blobs, {
type: 'video/webm'
});
convertStreams(superBuffer, "gif");
}
}
// Save on Drive
function saveDrive() {
downloaded = true;
chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
if (!token) {
return;
}
$("#share span").html(chrome.i18n.getMessage("saving"));
$("#share").css("pointer-events", "none");
var metadata = {
name: 'video.mp4',
mimeType: 'video/mp4'
};
var superBuffer = new Blob(blobs, {
type: 'video/mp4'
});
var form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], {type: 'application/json'}));
form.append('file', superBuffer);
// Upload to Drive
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart');
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
xhr.responseType = 'json';
xhr.onload = () => {
var fileId = xhr.response.id;
$("#share span").html("Save to Drive");
$("#share").css("pointer-events", "all");
// Open file in Drive in a new tab
chrome.tabs.create({
url: "https://drive.google.com/file/d/"+fileId
});
};
xhr.send(form);
});
}
// Check when video has been loaded
$("#video").on("loadedmetadata", function(){
// Initialize custom video player
player = new Plyr('#video', {
controls: ['play-large', 'play', 'progress', 'current-time', 'duration', 'mute', 'volume', 'fullscreen'],
ratio: '16:9'
});
// Check when player is ready
player.on("canplay", function(){
// First time setup
if (setup) {
setup = false;
initRanges();
player.currentTime = 0;
}
// Check when trim slider values change
trimslider.noUiSlider.on('slide', function(values, handle) {
$("#trim-start input").val(timestamp(0));
$("#trim-end input").val(timestamp(values[0]));
player.currentTime = parseFloat(values[handle]);
});
// Check when remove slider values change
removeslider.noUiSlider.on('slide', function(values, handle) {
$("#remove-start input").val(timestamp(values[0]));
$("#remove-end input").val(timestamp(values[1]));
player.currentTime = parseFloat(values[handle]);
});
});
})
// Applying a trim
$("#apply-trim").on("click", function(){
trim(0, parseInt(trimslider.noUiSlider.get()[0]));
});
// Removing part of the video
$("#apply-remove").on("click", function(){
remove(parseInt(removeslider.noUiSlider.get()[0]), parseInt(removeslider.noUiSlider.get()[1]));
});
// Download video
$("#download").on("click", function(){
download();
});
// Save on Drive
$("#share").on("click", function(){
saveDrive();
});
// Revert changes made to the video
$("#reset").on("click", function(){
reset();
});
// For mobile version
$("#show-hide").on("click", function(){
$("#settings").toggleClass("hidepanel");
$("#export").toggleClass("hidepanel");
}) ;
// Localization (strings in different languages)
$("#made-with").html(chrome.i18n.getMessage("made_with"));
$("#by-alyssa").html(chrome.i18n.getMessage("by_alyssa"));
$("#rate-label").html(chrome.i18n.getMessage("rate_extension"));
$("#show-hide").html(chrome.i18n.getMessage("show_hide"));
$("#edit-label").html(chrome.i18n.getMessage("edit_recording"));
$("h2").html(chrome.i18n.getMessage("edit_recording_desc"));
$("#format-select-label").html(chrome.i18n.getMessage("format"));
$("#webm-default").html(chrome.i18n.getMessage("webm"));
$("#trim-label").html(chrome.i18n.getMessage("trim_video"));
$(".start-label").html(chrome.i18n.getMessage("start"));
$(".end-label").html(chrome.i18n.getMessage("end"));
$("#apply-trim").html(chrome.i18n.getMessage("apply"));
$("#remove-label").html(chrome.i18n.getMessage("remove_part"));
$("#format-select-label").html(chrome.i18n.getMessage("format"));
$("#apply-remove").html(chrome.i18n.getMessage("apply"));
$("#reset").html(chrome.i18n.getMessage("reset"));
$("#download-label").html(chrome.i18n.getMessage("download"));
$("#share span").html(chrome.i18n.getMessage("save_drive"));
$("#apply-trim").html(chrome.i18n.getMessage("apply"));
});