-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
244 lines (215 loc) · 7.42 KB
/
index.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
const { app, BrowserWindow, ipcMain, nativeTheme, dialog } = require('electron');
const fs = require('fs');
const path = require('path');
const YTDlpWrap = require('yt-dlp-wrap').default;
const clipboardListener = require('clipboard-event');
if (require('electron-squirrel-startup')) return app.quit();
const ffmpeg = require('fluent-ffmpeg');
const ytdl = require('ytdl-core');
if (process.platform == "darwin" && process.mainModule.filename.indexOf('app.asar') == -1)
{
app.disableHardwareAcceleration();
console.log("Disabled hardware acceleration");
}
var ffmpegPath = require('@ffmpeg-installer/ffmpeg').path.replace('app.asar', 'app.asar.unpacked');
var ffprobePath = require('@ffprobe-installer/ffprobe').path.replace('app.asar', 'app.asar.unpacked');
ffmpeg.setFfmpegPath(ffmpegPath);
ffmpeg.setFfprobePath(ffprobePath);
require("./updater");
clipboardListener.startListening();
function createWindow ()
{
var win = new BrowserWindow({
width: 800,
height: 652,
minHeight: 566,
maximizable: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
devTools: true,
backgroundThrottling: false,
sandbox: false,
},
backgroundColor: nativeTheme.shouldUseDarkColors ? '#1c1c1c' : "#ffffff",
icon: process.platform == "darwin" ? "./icon.icns" : "./icon.ico",
title: "YouTube Downloader",
autoHideMenuBar: true
});
win.loadFile('./app/index.html').then(() =>
{
win.on('show', () =>
{
setTimeout(() =>
{
win.focus();
}, 200);
});
win.show();
});
win.on("resize", () =>
{
win.webContents.send("getHeight");
});
win.setAspectRatio(785 / 660);
clipboardListener.on('change', () =>
{
win.webContents.send("clipboardChange");
});
if (process.platform == "darwin")
{
win.on("focus", () =>
{
win.webContents.send("clipboardChange");
});
}
nativeTheme.themeSource = "system";
}
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock)
{
app.quit();
} else
{
app.on('second-instance', () =>
{
// Someone tried to run a second instance, we should focus our window.
var myWindow = BrowserWindow.getAllWindows()[0];
if (myWindow)
{
if (myWindow.isMinimized()) myWindow.restore();
myWindow.focus();
}
});
// Create myWindow, load the rest of the app, etc...
app.whenReady().then(() =>
{
createWindow();
// Start convert handler script
require("./downloaders/convertHandler");
// Download yt-dlp
var ext = process.platform == "win32" ? ".exe" : "";
if (!fs.existsSync(path.join(process.resourcesPath, "./yt-dlp" + ext)))
{
YTDlpWrap.downloadFromGithub(path.join(process.resourcesPath, "./yt-dlp" + ext)).then(() =>
{
}).catch((err) =>
{
console.log(err.statusMessage);
dialog.showErrorBox("An error occured downloading yt-dlp!", err.statusCode + ": " + err.statusMessage);
});
}else {
// Check version if yt-dlp needs updating
var dlp = new YTDlpWrap(path.join(process.resourcesPath, "./yt-dlp" + ext))
YTDlpWrap.getGithubReleases(1, 1).then(async (versions) => {
const remoteVersion = versions[0].tag_name.replace(/[^A-z0-9.]/g, "")
const localVersion = (await dlp.getVersion()).replace(/[^A-z0-9.]/g, "")
if (remoteVersion !== localVersion) {
YTDlpWrap.downloadFromGithub(path.join(process.resourcesPath, "./yt-dlp" + ext)).then(() =>
{
dialog.showMessageBox(BrowserWindow.getAllWindows()[0], {
title: "Restart required.",
message:
"The YouTube downloader used internally has updated, would you like to restart the application?\n\n" +
"A non-updated version may result in videos not being able to download.",
buttons: ["OK", "Continue without update"],
noLink: true
}).then((val) => {
if (val.response == 0) {
app.quit()
app.relaunch()
}
})
}).catch((err) =>
{
console.log(err.statusMessage);
dialog.showErrorBox("An error occured downloading yt-dlp!", err.statusCode + ": " + err.statusMessage);
});
}
})
}
app.on('activate', () =>
{
if (BrowserWindow.getAllWindows().length === 0)
{
createWindow();
}
});
});
}
app.on('window-all-closed', () =>
{
app.quit();
});
ipcMain.on("download", async (ev, url, type, quality) =>
{
if (type == "audioOnly")
{
require("./downloaders/audioDownloader")(url);
} else if (type == "videoOnly")
{
require("./downloaders/videoDownloader")(url, quality);
} else if (type == "videoAudio")
{
require("./downloaders/VAdownloader")(url, quality);
return;
}
});
ipcMain.on("getFormats", async (ev, url) =>
{
var formatList = [];
ytdl.getInfo(url).then((info) =>
{
var audioFormats = info.formats.filter((format) =>
{
return !format.hasVideo && format.hasAudio && format.contentLength;
});
audioFormats = audioFormats.sort((a, b) =>
{
var aQ = a.audioBitrate;
var bQ = b.audioBitrate;
return bQ - aQ;
});
var formats = info.formats.filter((format) =>
{
return format.hasVideo && !format.hasAudio && !format.isDashMPD && !format.videoCodec.includes("avc1") && !format.videoCodec.includes("av01");
});
formats = formats.sort((a, b) =>
{
var aHDR = a.colorInfo ? parseFloat((a.colorInfo.primaries ?? "0").replace("COLOR_PRIMARIES_BT", "")) : 0;
var bHDR = b.colorInfo ? parseFloat((b.colorInfo.primaries ?? "0").replace("COLOR_PRIMARIES_BT", "")) : 0;
var aQ = parseFloat(a.bitrate);
var bQ = parseFloat(b.bitrate);
return bHDR - aHDR || bQ - aQ;
});
formats.forEach((format) =>
{
formatList.push({
format: format.container,
resolution: format.qualityLabel.replace(/p.*/, "p"),
fps: format.fps,
hdr: format.qualityLabel.includes("HDR"),
size: format.contentLength,
audioSize: audioFormats[0].contentLength
});
});
ev.sender.send("vidFormats", formatList);
});
});
function sendPercent (percent)
{
var win = BrowserWindow.getAllWindows()[0];
win.webContents.send("percent", percent);
win.setProgressBar(percent / 100);
}
function doneDownload (isError)
{
var win = BrowserWindow.getAllWindows()[0];
win.setProgressBar(-1);
win.webContents.send("doneDownload", isError);
}
module.exports = {
sendPercent,
doneDownload,
ffmpegPath,
ffprobePath
};