forked from mayeaux/videodownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
316 lines (196 loc) · 7.17 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
// 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');
const spawn = require('child_process').spawn;
const ytdl = require('ytdl-core');
const ffmpeg = require('fluent-ffmpeg');
var youtubedl = require('youtube-dl');
const {dialog} = require('electron').remote;
// create videos file if doesn't exist
var dir = './videos';
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');
// select download as audio or video
if(downloadAsAudio){
arguments.push('-f');
arguments.push('bestaudio');
// can add something here later
} else {
// arguments.push('best');
}
// // verbose output
console.log(title);
// replace forward slashes with underscores
if(title){
title = title.replace(/\//g , '_');
console.log('replacing');
}
// title is that passed or the one from youtube
const fileName = title || '%(title)s';
console.log(title);
// TODO: here is something
let inputtedUrl = selectVideoDirectoryInput.value;
console.log(inputtedUrl);
// create
if (!fs.existsSync(inputtedUrl)){
fs.mkdirSync(inputtedUrl);
}
console.log(__dirname);
let toAttachToDirname = inputtedUrl;
// remove dot to fix path
while(toAttachToDirname.charAt(0) === '.')
{
toAttachToDirname = toAttachToDirname.substr(1);
}
const filePath = __dirname + toAttachToDirname;
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');
// }
const youtubeBinaryFilePath = 'node_modules/youtube-dl/bin/youtube-dl';
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(){
shell.openItem('./videos');
};
const {shell} = require('electron');
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);
});
});
}
// frontend code
function myFunction() {
var youtubeUrl = document.getElementsByClassName('youtubeUrl')[0];
var downloadAsAudio = document.getElementsByClassName('downloadAsAudio')[0];
var saveAsTitle = document.getElementsByClassName('saveAsTitle')[0];
// var saveAsTitleValue = saveAsTitle.value;
var youtubeUrlValue = youtubeUrl.value;
/** WHEN PASTED **/
navigator.clipboard.readText()
.then(async text => {
// update frontend to reflect text from clipboard
document.getElementsByClassName("youtubeUrl")[0].value = text;
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(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 {
saveAsTitle.value = info[0].title;
playlistDownloadingDiv.style.display = 'none';
titleDiv.style.display = '';
console.log('single item')
}
console.log(info);
})
.catch(err => {
console.log(err);
});
}
/** SELECT DIRECTORY **/
const saveToDirectory = './videos';
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);
}
};