-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathrenderer.js
382 lines (261 loc) · 9.04 KB
/
renderer.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
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
var fs = require('fs-extra');
const spawn = require('child_process').spawn;
const ytdl = require('ytdl-core');
var youtubedl = require('youtube-dl');
const { shell } = require('electron');
const homedir = require('os').homedir();
const {dialog} = require('electron').remote;
const downloader = require('./downloadBinary');
const ffmpeg = require('@ffmpeg-installer/ffmpeg');
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
console.log(`ffmpeg path: ${ffmpegPath}`);
const youtubeBinaryFilePath = youtubedl.getYtdlBinary();
console.log(`youtube-dl binary path: ${youtubeBinaryFilePath}`);
// create videos file if doesn't exist
var dir = `${homedir}/videodownloadervideos`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
// select video input
var selectVideoDirectoryInput = document.getElementsByClassName(
'selectVideoDirectoryInput'
)[0];
var playlistDownloadingDiv = document.getElementsByClassName(
'playlistDownloadingDiv'
)[0];
var titleDiv = document.getElementsByClassName('titleDiv')[0];
var downloadPlaylistText = document.getElementsByClassName(
'downloadPlaylistText'
)[0];
// var url = 'https://www.youtube.com/watch?v=ZcAiayke00I';
function download(url, title, downloadAsAudio, youtubeUrl, saveAsTitleValue) {
let arguments = [];
// set the url for ytdl
arguments.push(url);
// verbose output
arguments.push('-v');
// arguments.push('-f', 'bestvideo+bestaudio/best');
arguments.push('--add-metadata');
arguments.push('--ffmpeg-location');
arguments.push(ffmpegPath);
arguments.push('--no-mtime');
arguments.push('--ignore-errors');
// select download as audio or video
if (downloadAsAudio) {
arguments.push('-f');
// arguments.push('bestaudio');
// don't want webm as audio
arguments.push('bestaudio[ext!=webm]');
/** conversion taking too long atm **/
// arguments.push('--extract-audio');
//
// arguments.push('--audio-format');
//
// arguments.push('mp3');
// can add something here later
} else {
// download as mp4 if it's youtube (tired of reconverting .flv files)
const isYouTubeDownload = url.match('youtube');
if(isYouTubeDownload){
console.log('downloading from youtube');
arguments.push('-f');
arguments.push('bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4');
}
// arguments.push('best');
}
// // verbose output
console.log(title);
// replace forward slashes with underscores
if (title) {
title = title.replace(/\//g, '_');
console.log('replacing');
}
// TODO: trim to max 255 letters
// title is that passed or the one from youtube
const fileName = title || '%(title)s';
console.log(title);
let inputtedUrl = selectVideoDirectoryInput.value;
console.log(inputtedUrl);
// create
if (!fs.existsSync(inputtedUrl)) {
fs.mkdirp(inputtedUrl);
}
console.log(__dirname);
const filePath = inputtedUrl;
const fileExtension = `%(ext)s`;
let saveToFolder = `${filePath}/${fileName}.${fileExtension}`;
console.log(saveToFolder);
// save to videos directory
arguments.push('-o', saveToFolder);
console.log(arguments);
// deleted for now since it requires ffmpeg
// download as audio if needed
// if(downloadAsAudio){
// console.log('Download as audio');
// arguments.push('-x');
// }
console.log(arguments);
const ls = spawn(youtubeBinaryFilePath, arguments);
ls.stdout.on('data', data => {
percentage.innerText = data;
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', data => {
percentage.innerText = data;
console.log(`stderr: ${data}`);
});
ls.on('close', code => {
playlistDownloadingDiv.style.display = 'none';
titleDiv.style.display = '';
// clear out inputs after
youtubeUrl.value = '';
saveAsTitleValue.value = '';
// if it ends successfully say download completed
if (code == 0) {
percentage.innerText = 'Download completed';
}
console.log(`child process exited with code ${code}`);
});
}
// start download button
var startDownload = document.getElementsByClassName('startDownload')[0];
// open folder button
var openFolder = document.getElementsByClassName('openFolder')[0];
// percentage div
var percentage = document.getElementsByClassName('percentage')[0];
// playlistDownloadingDiv
// titleDiv
// downloadPlaylistText
openFolder.onclick = function(){
var value = document.getElementsByClassName('selectVideoDirectoryInput')[0].value;
shell.openItem(value);
};
startDownload.onclick = function() {
var youtubeUrl = document.getElementsByClassName('youtubeUrl')[0];
var downloadAsAudio = document.getElementsByClassName('downloadAsAudio')[0];
var saveAsTitle = document.getElementsByClassName('saveAsTitle')[0];
var youtubeUrlValue = youtubeUrl.value;
var saveAsTitleValue = saveAsTitle.value;
var downloadAsAudioValue = downloadAsAudio.checked;
download(
youtubeUrlValue,
saveAsTitleValue,
downloadAsAudioValue,
youtubeUrl,
saveAsTitle
);
percentage.scrollIntoView();
};
function youtubeDlInfoAsync(url, options) {
return new Promise(function(resolve, reject) {
youtubedl.getInfo(url, options, function(err, data) {
if (err !== null) reject(err);
else resolve(data);
});
});
}
async function populateTitle() {
// get save as title div
var saveAsTitle = document.getElementsByClassName('saveAsTitle')[0];
// get text from youtube url div value
let text = document.getElementsByClassName('youtubeUrl')[0].value;
const isBrighteonDownload = text.match('brighteon');
let options;
if (isBrighteonDownload) {
options = ['-f bestvideo'];
} else {
options = ['-j', '--flat-playlist', '--dump-single-json'];
}
const info = await youtubeDlInfoAsync(text, options);
// if its a playlist or channel
if (info.length > 2) {
console.log(info);
const playlistinfo = info[info.length - 1];
const uploader = playlistinfo.uploader;
const amountOfUploads = playlistinfo.entries.length;
console.log(uploader, amountOfUploads);
downloadPlaylistText.innerHTML = `${amountOfUploads} Item Playlist or Channel To Be Downloaded`;
playlistDownloadingDiv.style.display = '';
titleDiv.style.display = 'none';
selectVideoDirectoryInput.value =
selectVideoDirectoryInput.value + '/' + uploader;
console.log('an array');
} else if (info.length == 2) {
// TODO: trim here
const trimmedTitle = info[0].title.substring(0, 200);
saveAsTitle.value = trimmedTitle;
playlistDownloadingDiv.style.display = 'none';
titleDiv.style.display = '';
playlistDownloadingDiv.style.display = 'none';
titleDiv.style.display = '';
console.log('single item');
} else if (info && info.title) {
const trimmedTitle = info.title.substring(0, 200);
// TODO: trim here
saveAsTitle.value = trimmedTitle;
playlistDownloadingDiv.style.display = 'none';
titleDiv.style.display = '';
console.log('single item');
} else {
console.log('ERROR');
}
console.log(info);
}
document.getElementsByClassName('youtubeUrl')[0].onblur = async function() {
await populateTitle();
};
// frontend code
function myFunction() {
/** WHEN PASTED **/
// get the copied text off the clipboard
navigator.clipboard
.readText()
.then(async text => {
// update frontend to reflect text from clipboard
document.getElementsByClassName('youtubeUrl')[0].value = text;
//
await populateTitle();
})
.catch(err => {
console.log(err);
});
}
/** SELECT DIRECTORY **/
const saveToDirectory = dir;
selectVideoDirectoryInput.value = saveToDirectory;
const selectVideoDirectoryButton = document.getElementsByClassName(
'selectVideoDirectory'
)[0];
const selectVideoDirectory = (selectVideoDirectoryButton.onclick = function() {
// get path from electron and load it as selectedPath
var selectedPath = dialog.showOpenDialog({
defaultPath: './videos',
properties: ['openDirectory']
});
console.log(selectedPath[0]);
// test if it's a shorter url because its within contained
var newThing = selectedPath[0].split(__dirname)[1];
let adjustedUrlWithCurrentDirectory;
if (newThing) {
adjustedUrlWithCurrentDirectory = `.${newThing}`;
} else {
adjustedUrlWithCurrentDirectory = selectedPath[0];
}
console.log(newThing);
// console.log(newThing);
selectVideoDirectoryInput.value = adjustedUrlWithCurrentDirectory;
if (!fs.existsSync(adjustedUrlWithCurrentDirectory)) {
fs.mkdirSync(adjustedUrlWithCurrentDirectory);
}
});
// remove youtubedl from pathname to give containing folder
const youtubeBinaryContainingFolder = youtubeBinaryFilePath.substr(0, youtubeBinaryFilePath.lastIndexOf("\/"));
console.log(`youtubeBinaryContainingFolder: ${youtubeBinaryContainingFolder}`);
// update binary on boot
downloader(youtubeBinaryContainingFolder, function error(err, done) {
if (err) { return console.log(err.stack); }
console.log(done);
});