-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
executable file
·401 lines (320 loc) · 15.7 KB
/
upload.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
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
var client = new WebTorrent();
var currentStatus = "";
var qualityOptions = [];
var lasPerentage = 0;
var processOriginalRes = false;
var originalRes = {};
var thumbnails = ['thumbnail','thumbnail_2','thumbnail_3']
var selectedThumbnail = 0;
var videoLength = 0;
var supportedQualities = "";
var videoFps = 0;
var torrentData;
const message = document.getElementById('message');
const { createFFmpeg, fetchFile } = FFmpeg;
const ffmpeg = createFFmpeg({
log: false,
progress: ({ ratio }) => {
const stagePercentage = lasPerentage+((ratio * 100.0))/(qualityOptions.length);
if (stagePercentage > 0) {
$('.progress-bar').css('width', stagePercentage+'%').attr('aria-valuenow', stagePercentage);
$(".base-size").text(`${currentStatus} ${Math.round(stagePercentage)}%`)
}
//message.innerHTML = `Complete: ${}%`;
},
});
/**
* The complete transoding pipeline. From determining the highest resolution of the video (and obtaining video metadata) to transcoding it to appropriate qualities.
*
* @param {Array} Array of Files (FIXME: Should be just 1 file to continue).
* @return null.
*/
const transcode = async ({ target: { files } }) => {
MediaInfo({ format: 'object' }, async (mediainfo) => {
const file = files[0];
if (file) {
message.innerHTML = 'Loading Media Metadata...';
const getSize = () => file.size
const readChunk = (chunkSize, offset) =>
new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = (event) => {
if (event.target.error) {
reject(event.target.error)
}
resolve(new Uint8Array(event.target.result))
}
reader.readAsArrayBuffer(file.slice(offset, offset + chunkSize))
})
mediainfo
.analyzeData(getSize, readChunk)
.then(async (result) => {
console.log("ress: ",result);
var thumbnailPos = result.media.track[0].Duration/3;
var thumbnailPosB = result.media.track[0].Duration/4;
var thumbnailPosC = result.media.track[0].Duration/2;
videoLength = result.media.track[0].Duration;
videoFps = result.media.track[0].FrameRate;
for (var i = 0; i < result.media.track.length; i++) {
console.log("result.media.track[i]['@type']: ",result.media.track[i]['@type']);
if (result.media.track[i]['@type'] == "Video") {
console.log("found video...");
if (result.media.track[i].Sampled_Width > 720) { //Will support 4k in a bit. Fo now this means it's 1080p or close.
if (result.media.track[i].Sampled_Width != 1080 && result.media.track[i].Sampled_Width < 1080) {
//It's some weird resolution below 1080p so lets try to keep it to maintain aspect ratio, but label it as 1080p
processOriginalRes = true;
originalRes = {width: result.media.track[i].Sampled_Width, height: result.media.track[i].Sampled_Height};
}
qualityOptions = [1080,720,480];
supportedQualities = "1080,720,480";
} else if (result.media.track[i].Sampled_Width > 480) {
if (result.media.track[i].Sampled_Width != 720 && result.media.track[i].Sampled_Width < 720) {
//It's some weird resolution below 720 so lets try to keep it to maintain aspect ratio, but label it as 720
processOriginalRes = true;
originalRes = {width: result.media.track[i].Sampled_Width, height: result.media.track[i].Sampled_Height};
}
qualityOptions = [720,480];
supportedQualities = "720,480";
} else { //480p or below
if (result.media.track[i].Sampled_Width != 480 && result.media.track[i].Sampled_Width < 480) {
//It's some weird resolution below 480 (probs 360p or 240p in this case) so lets try to keep it to maintain aspect ratio, but label it as 480
processOriginalRes = true;
originalRes = {width: result.media.track[i].Sampled_Width, height: result.media.track[i].Sampled_Height};
}
qualityOptions = [480];
supportedQualities = "480";
}
}
}
const { name } = files[0];
$("#message-subtext").hide();
message.innerHTML = 'Setting things up...';
await ffmpeg.load();
ffmpeg.FS('writeFile', name, await fetchFile(files[0]));
$(".base-title").text(name);
$("#e1").val(name.replace(/\.[^/.]+$/, ""));
/*Generate main thumbnail*/
message.innerHTML = 'Generating thumbnails...';
await ffmpeg.run('-deinterlace','-an','-ss',String(thumbnailPos),'-i', name,'-preset','ultrafast','-f','mjpeg','-t','1','-r','1','-y','-s','640x360','thumbnail.jpg');
const thumbnail_data = ffmpeg.FS('readFile', 'thumbnail.jpg');
const thumbnail = document.getElementById('thumbnail');
thumbnail.src = URL.createObjectURL(new Blob([thumbnail_data.buffer], { type: 'image/jpg' }));
$("#thumbnail-option-0 > img").attr("src",thumbnail.src);
await ffmpeg.run('-deinterlace','-an','-ss',String(thumbnailPos),'-i', name,'-preset','ultrafast','-f','mjpeg','-t','1','-r','1','-y','-s','1280x720','thumbnail_720.jpg');
//const thumbnail_data_720 = ffmpeg.FS('readFile', 'thumbnail_720.jpg');
await ffmpeg.run('-deinterlace','-an','-ss',String(thumbnailPosB),'-i', name,'-preset','ultrafast','-f','mjpeg','-t','1','-r','1','-y','-s','640x360','thumbnail_2.jpg');
const thumbnail_data_b = ffmpeg.FS('readFile', 'thumbnail_2.jpg');
$("#thumbnail-option-1 > img").attr("src",URL.createObjectURL(new Blob([thumbnail_data_b.buffer], { type: 'image/jpg' })));
await ffmpeg.run('-deinterlace','-an','-ss',String(thumbnailPosC),'-i', name,'-preset','ultrafast','-f','mjpeg','-t','1','-r','1','-y','-s','640x360','thumbnail_3.jpg');
const thumbnail_data_c = ffmpeg.FS('readFile', 'thumbnail_3.jpg');
$("#thumbnail-option-2 > img").attr("src",URL.createObjectURL(new Blob([thumbnail_data_c.buffer], { type: 'image/jpg' })));
$("#initial-upload-page").hide();
$("#upload-details").removeClass("d-none");
console.log("qualityOptions: ",qualityOptions);
console.log("originalRes: ",originalRes);
console.log("processOriginalRes: ",processOriginalRes);
//$("#thumbnail").parent().show();
for (var i = 0; i < qualityOptions.length; i++) {
currentStatus = 'Stage '+(i+1)+' of '+(qualityOptions.length+2)+': Transcoding:';
if (i == 0 && processOriginalRes)
await ffmpeg.run('-i', name,'-preset','ultrafast','-vf','scale=-1:'+originalRes.height, qualityOptions[i]+'.mp4');
else
await ffmpeg.run('-i', name,'-preset','ultrafast','-vf','scale=-1:'+qualityOptions[i], qualityOptions[i]+'.mp4');
/*if (i == 0 && processOriginalRes)
await ffmpeg.run('-i', name,'-c:v','libx264','-preset','ultrafast','-vf','scale=-1:'+originalRes.height, qualityOptions[i]+'.mp4');
else
await ffmpeg.run('-i', name,'-c:v','libx264','-preset','ultrafast','-vf','scale=-1:'+qualityOptions[i], qualityOptions[i]+'.mp4');*/
lasPerentage += 100/qualityOptions.length;
//currentStatus = 'Completed transcoding Stage '+(i+2)+':';
}
currentStatus = 'Stage '+(qualityOptions.length+1)+' of '+(qualityOptions.length+2)+': Generating previews (this might take a moment):';
$(".base-size").text(currentStatus);
await ffmpeg.run('-i', name,'-preset','ultrafast','-filter_complex',"select='not(mod(n,120))',scale=360:202.50,tile=11x11",'-frames:v','1','-qscale:v','3','-an','preview.jpg');
//const previews_data = ffmpeg.FS('readFile', 'preview.jpg');
//$("#thumbnail-option-2 > img").attr("src",URL.createObjectURL(new Blob([previews_data.buffer], { type: 'image/jpg' })));
//console.log("vtt done here: ",vtt_data);
$(".base-size").text('Waiting for you to save changes. Select "Save Changes" below to continue.');
/*const finalStage = qualityOptions.length+1;
$(".base-size").text('Stage '+finalStage+' of '+finalStage+': Broadcasting to peers');*/
//const data = ffmpeg.FS('readFile', 'output.mp4');
//const video = document.getElementById('output-video');
//video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
})
.catch((error) => {
console.log(`An error occured:\n${error.stack}`); //Show message box
})
}
});
/*var files = [new File(data, "output2.mp4")];
console.log("files: ",files)
client.seed(files, function (torrent) {
console.log('Client is seeding ' + torrent.magnetURI)
})*/
};
//})
function selectThumbnailOptn(pos) {
$(".selected-thumbnail img").removeClass("btn-outline-secondary");
$(".selected-thumbnail").removeClass("selected-thumbnail");
$("#thumbnail-option-"+pos).parent().addClass("selected-thumbnail");
$("#thumbnail-option-"+pos+" img").addClass("btn-outline-secondary");
$("#thumbnail").attr("src",$("#thumbnail-option-"+pos+" img").attr("src"));
selectedThumbnail = pos;
}
function uploadVeels(veelId,files,token) {
var form_data = new FormData();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
console.log("response: ",xhr.responseText);
//var response = JSON.parse(xhr.responseText);
}
}
xhr.open("POST", "/upload_veel/index.php", true);
xhr.upload.addEventListener('progress', function(e){
if(e.lengthComputable){
var uploadPercent = e.loaded / e.total;
uploadPercent = (uploadPercent * 100);
console.log("upload percentage: ",uploadPercent);
}
});
form_data.append('token', token);
console.log("files uploaded to server: ",files);
for (var i = 0; i < files.length; i++) {
//files[i].path = veelId+"/"+files[i].path;
form_data.append('fileToUpload[]', files[i]);
}
//form_data.append('fileToUpload[]', fileObj);
xhr.send(form_data);
}
function startBroadcasting(veelId,token,vttData) {
let files = [];
//var Buffer = require('buffer');
for (var i = 0; i < qualityOptions.length; i++) {
let fileObj = new File([ffmpeg.FS('readFile', qualityOptions[i]+'.mp4')], qualityOptions[i]+'.mp4', {type: "video/mp4"});
fileObj.isFile = true;
fileObj.isDirectory = false;
fileObj.fullPath = "/"+veelId+"/"+qualityOptions[i]+'.mp4';
//fileObj.path = veelId+"/"+fileObj.path;
files.push(fileObj);
}
const preview_data = ffmpeg.FS('readFile', "preview.jpg");
const preview = new File([preview_data], "preview.jpg",{
type: "image/jpg",
});
preview.isFile = true;
preview.isDirectory = false;
preview.fullPath = "/"+veelId+"/preview.jpg";
files.push(preview);
const vttFile = new File([vttData], "preview.vtt",{
type: "text/plain",
});
vttFile.isFile = true;
vttFile.isDirectory = false;
vttFile.fullPath = "/"+veelId+"/preview.vtt";
files.push(vttFile);
//let files = ffmpeg.FS('readdir', '/');
console.log("files: ",files);
console.log("files to push: ",files);
client.seed(files, function (torrent) {
var totalFileSize = 0;
console.log("seeding files: ",torrent.files);
torrent.files.forEach(file => totalFileSize += file.length);
totalFileSize = totalFileSize / Math.pow(1024,2);
console.log('Client is seeding ' + torrent.magnetURI)
//uploadMetadata(torrent.magnetURI,totalFileSize,files,torrent.torrentFile);
uploadTorrentData(veelId,torrent.magnetURI,totalFileSize,token,torrent.torrentFile,files);
})
}
function uploadTorrentData(veelId,magent,size,token,torrentFile,files) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
console.log("response: ",xhr.responseText);
var response = JSON.parse(xhr.responseText);
if (response.error == null) {
uploadVeels(veelId,files,token);
}
}
}
xhr.open("POST", "/add_torrent_data/index.php", true);
var form_data = new FormData();
form_data.append('token', token);
form_data.append('magnet', magent);
form_data.append('torrentFile', new File([torrentFile], "torrentFile.torrent"));
form_data.append('total_size', size);
xhr.send(form_data);
}
function uploadMetadata() {
console.log("uploading..");
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
console.log("response: ",xhr.responseText);
var response = JSON.parse(xhr.responseText);
console.log("JSON: ",JSON.parse(xhr.responseText));
if (response.error == null) {
var vtt_data = generate_vtt(videoLength,response.id,5,360,202.50,11);
console.log("vtt_data: ",vtt_data);
console.log("video url: https://veel.tv/video?v="+response.id);
//uploadVeels(response.id,files,response.tokenId);
startBroadcasting(response.id,response.tokenId,vtt_data);
$(".base-close").hide();
$(".base-size").text("Broadcasting your veel: 10%")
$("#veelUrl").append('<a href="https://veel.tv/video?v='+response.id+'" target="_blank">https://veel.tv/video?v='+response.id+'</a>');
$(".sharer").attr("data-url",'https://veel.tv/video?v='+response.id);
$("#copyVeelUrl").attr('data-copy','https://veel.tv/video?v='+response.id);
window.Sharer.init();
$("#allProcessingOptions").addClass("d-none");
$("#completedProcessing").removeClass("d-none");
}
}
}
xhr.open("POST", "/uploader/index.php", true);
var form_data = new FormData();
var submitData = true;
if ($("#e1").val() == "") {
//show error
submitData = false;
} else {
form_data.append('title', $("#e1").val());
}
if ($("#e2").val() == "") {
//show error
submitData = false;
} else {
form_data.append('description', $("#e2").val());
}
form_data.append('privacy', $("#e4").val());
//form_data.append('magnet', magent);
//form_data.append('torrentFile', new File([torrentFile], "torrentFile.torrent"));
form_data.append('length', videoLength);
form_data.append('supported_qualities', supportedQualities);
form_data.append('fps', videoFps);
form_data.append('categories', '');
form_data.append('age_restriction', $('#e3').find(":selected").text());
form_data.append('tags', $('#e7').val());
form_data.append('cast', $('#e8').val());
form_data.append('monetized', $('#e5').find(":selected").text());
form_data.append('intergrated_Ads', false);
form_data.append('visibility', $('#e4').find(":selected").text());
form_data.append('captions', '');
//form_data.append('total_size', size);
const thumbnail_data = ffmpeg.FS('readFile', thumbnails[selectedThumbnail]+".jpg");
const thumbnail = new File([thumbnail_data], "thumbnail.jpg",{
type: "image/jpg",
});
form_data.append('thumbnail', thumbnail);
const thumbnail_lg_data = ffmpeg.FS('readFile', thumbnails[selectedThumbnail]+"_720.jpg");
const thumbnail_lg = new File([thumbnail_data], "thumbnail_lg.jpg",{
type: "image/jpg",
});
form_data.append('thumbnail_lg', thumbnail_lg);
const preview_data = ffmpeg.FS('readFile', "preview.jpg");
const preview = new File([preview_data], "preview.jpg",{
type: "image/jpg",
});
form_data.append('preview', preview);
//form_data.append('file', fileToUpload);
xhr.send(form_data);
}
document.getElementById('uploader').addEventListener('change', transcode);