1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-28 09:20:52 +01:00
upscayl/electron/index.ts

633 lines
16 KiB
TypeScript
Raw Normal View History

2022-08-15 06:53:14 +02:00
// Native
2022-11-11 21:39:28 +01:00
import { join, parse } from "path";
import { format } from "url";
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import fs from "fs";
import sizeOf from "image-size";
import { autoUpdater } from "electron-updater";
2022-11-11 21:47:00 +01:00
import getPlatform from "./getPlatform";
2022-11-23 19:24:30 +01:00
import ffmpeg from "upscayl-ffmpeg";
2022-08-15 06:53:14 +02:00
2022-11-11 21:39:28 +01:00
import { execPath, modelsPath } from "./binaries";
2022-08-16 04:17:27 +02:00
2022-08-15 06:53:14 +02:00
// Packages
2022-11-11 21:39:28 +01:00
import {
2022-08-17 04:37:50 +02:00
BrowserWindow,
app,
ipcMain,
dialog,
2022-08-18 11:53:23 +02:00
shell,
2022-11-11 21:39:28 +01:00
MessageBoxOptions,
} from "electron";
2022-09-25 13:15:43 +02:00
2022-11-11 21:39:28 +01:00
import isDev from "electron-is-dev";
import prepareNext from "electron-next";
import commands from "./commands";
2022-08-15 06:53:14 +02:00
// Prepare the renderer once the app is ready
2022-08-17 13:36:19 +02:00
let mainWindow;
2022-08-15 06:53:14 +02:00
app.on("ready", async () => {
await prepareNext("./renderer");
2022-11-23 19:24:30 +01:00
console.log("🚀 ICON PATH: ", join(__dirname, "build", "icon.png"));
console.log("🚀 UPSCAYL EXEC PATH: ", execPath(""));
console.log("🚀 MODELS PATH: ", modelsPath);
console.log("🚀 FFMPEG PATH: ", ffmpeg.path);
2022-09-07 04:31:28 +02:00
2022-08-17 13:36:19 +02:00
mainWindow = new BrowserWindow({
2022-09-30 18:39:14 +02:00
icon: join(__dirname, "build", "icon.png"),
2022-08-15 09:06:31 +02:00
width: 1100,
2022-09-19 13:14:40 +02:00
height: 740,
2022-08-18 11:53:23 +02:00
minHeight: 500,
minWidth: 500,
show: false,
2022-08-28 01:49:19 +02:00
backgroundColor: "#171717",
2022-08-15 06:53:14 +02:00
webPreferences: {
2022-08-16 04:17:27 +02:00
nodeIntegration: true,
2022-08-18 11:53:23 +02:00
webSecurity: false,
2022-08-15 06:53:14 +02:00
preload: join(__dirname, "preload.js"),
},
});
const url = isDev
? "http://localhost:8000"
: format({
pathname: join(__dirname, "../renderer/out/index.html"),
protocol: "file:",
slashes: true,
});
mainWindow.setMenuBarVisibility(false);
mainWindow.loadURL(url);
2022-08-18 11:53:23 +02:00
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: "deny" };
});
2022-08-21 17:11:11 +02:00
2022-08-30 10:45:06 +02:00
mainWindow.once("ready-to-show", () => {
mainWindow.show();
2022-09-30 18:36:16 +02:00
mainWindow.webContents.setZoomFactor(1);
2022-08-30 10:45:06 +02:00
});
2022-08-21 17:11:11 +02:00
if (!isDev) {
autoUpdater.checkForUpdates();
2022-08-23 05:13:08 +02:00
}
2022-08-15 06:53:14 +02:00
});
// Quit the app once all windows are closed
app.on("window-all-closed", app.quit);
2022-09-25 13:15:43 +02:00
console.log(app.getAppPath());
//------------------------Select File-----------------------------//
2022-08-15 09:21:12 +02:00
// ! DONT FORGET TO RESTART THE APP WHEN YOU CHANGE CODE HERE
2022-08-18 11:53:23 +02:00
ipcMain.handle(commands.SELECT_FILE, async () => {
2022-08-16 04:17:27 +02:00
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"],
});
2022-08-15 12:12:48 +02:00
if (canceled) {
2022-08-16 04:17:27 +02:00
console.log("operation cancelled");
return "cancelled";
} else {
console.log(filePaths[0]);
2022-08-29 15:41:59 +02:00
// CREATE input AND upscaled FOLDER
return filePaths[0];
}
2022-08-18 11:53:23 +02:00
});
2022-09-25 13:15:43 +02:00
//------------------------Select Folder-----------------------------//
2022-08-29 15:41:59 +02:00
ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => {
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openDirectory"],
});
if (canceled) {
console.log("operation cancelled");
return "cancelled";
2022-08-18 11:53:23 +02:00
} else {
2022-08-29 15:41:59 +02:00
console.log(filePaths[0]);
return filePaths[0];
}
2022-08-18 11:53:23 +02:00
});
2022-08-16 04:17:27 +02:00
2022-12-02 15:21:42 +01:00
//------------------------Open Folder-----------------------------//
ipcMain.on(commands.OPEN_FOLDER, async (event, payload) => {
console.log(payload);
shell.openPath(payload);
});
//------------------------Double Upscayl-----------------------------//
2022-09-17 11:40:47 +02:00
ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
const model = payload.model;
2022-09-10 16:52:53 +02:00
let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || "";
2022-09-17 11:40:47 +02:00
let outputDir = payload.outputPath;
2022-09-10 16:52:53 +02:00
// COPY IMAGE TO TMP FOLDER
2022-09-17 11:40:47 +02:00
const platform = getPlatform();
2022-09-10 16:52:53 +02:00
const fullfileName =
platform === "win"
? payload.imagePath.split("\\").slice(-1)[0]
: payload.imagePath.split("/").slice(-1)[0];
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
2022-09-17 11:40:47 +02:00
const outFile = outputDir + "/" + fileName + "_upscayl_8x_" + model + fileExt;
2022-09-10 16:52:53 +02:00
2022-09-17 11:40:47 +02:00
// UPSCALE
let upscayl = spawn(
execPath("realesrgan"),
2022-09-11 12:27:47 +02:00
[
"-i",
2022-09-17 11:40:47 +02:00
inputDir + "/" + fullfileName,
2022-09-11 12:27:47 +02:00
"-o",
2022-09-17 11:40:47 +02:00
outFile,
2022-09-11 12:27:47 +02:00
"-s",
4,
"-m",
2022-09-17 11:40:47 +02:00
modelsPath,
"-n",
model,
2022-09-11 12:27:47 +02:00
],
{
2022-11-11 21:39:28 +01:00
cwd: undefined,
2022-09-11 12:27:47 +02:00
detached: false,
}
);
2022-09-10 16:52:53 +02:00
2022-12-02 15:21:42 +01:00
console.log(
"🆙 COMMAND:",
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
4,
"-m",
modelsPath,
"-n",
model
);
2022-09-11 12:27:47 +02:00
let failed = false;
2022-09-17 11:40:47 +02:00
// TAKE UPSCAYL OUTPUT
upscayl.stderr.on("data", (data) => {
// CONVERT DATA TO STRING
2022-09-11 12:27:47 +02:00
data = data.toString();
2022-09-17 11:40:47 +02:00
// PRINT TO CONSOLE
console.log(data);
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
2022-09-11 12:27:47 +02:00
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
});
2022-09-17 11:40:47 +02:00
// IF ERROR
upscayl.on("error", (data) => {
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed = true;
return;
});
// ON UPSCAYL DONE
upscayl.on("close", (code) => {
// IF NOT FAILED
if (!failed) {
// UPSCALE
let upscayl2 = spawn(
execPath("realesrgan"),
2022-09-17 11:40:47 +02:00
["-i", outFile, "-o", outFile, "-s", 4, "-m", modelsPath, "-n", model],
{
2022-11-11 21:39:28 +01:00
cwd: undefined,
detached: false,
}
);
2022-09-17 11:40:47 +02:00
let failed2 = false;
// TAKE UPSCAYL OUTPUT
upscayl2.stderr.on("data", (data) => {
// CONVERT DATA TO STRING
data = data.toString();
2022-09-17 11:40:47 +02:00
// PRINT TO CONSOLE
console.log(data);
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
if (data.includes("invalid gpu") || data.includes("failed")) {
2022-09-17 11:40:47 +02:00
failed2 = true;
}
});
2022-09-17 11:40:47 +02:00
// IF ERROR
upscayl2.on("error", (data) => {
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed2 = true;
return;
});
2022-09-17 11:40:47 +02:00
upscayl2.on("close", (code) => {
if (!failed2) {
console.log("Done upscaling");
2022-09-17 11:40:47 +02:00
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_DONE, outFile);
}
2022-09-17 11:40:47 +02:00
});
2022-09-11 12:27:47 +02:00
}
2022-09-17 11:40:47 +02:00
});
2022-09-10 16:52:53 +02:00
});
//------------------------Image Upscayl-----------------------------//
2022-08-18 11:53:23 +02:00
ipcMain.on(commands.UPSCAYL, async (event, payload) => {
const model = payload.model;
const scale = payload.scaleFactor;
let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || "";
2022-08-29 15:41:59 +02:00
let outputDir = payload.outputPath;
2022-09-10 16:52:53 +02:00
// COPY IMAGE TO TMP FOLDER
2022-11-26 10:42:16 +01:00
const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "");
2022-09-17 11:40:47 +02:00
console.log(fullfileName);
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
2022-12-02 15:21:42 +01:00
const outFile = model.includes("models-DF2K")
? outputDir +
"/" +
fileName +
"_upscayl_sharpened_" +
scale +
"x_" +
model +
2022-12-02 15:21:42 +01:00
fileExt
: outputDir + "/" + fileName + "_upscayl_" + scale + "x_" + model + fileExt;
// UPSCALE
2022-08-29 15:41:59 +02:00
if (fs.existsSync(outFile)) {
2022-09-07 04:31:28 +02:00
// If already upscayled, just output that file
2022-08-30 10:45:06 +02:00
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
2022-11-25 08:30:03 +01:00
} else {
let upscayl: ChildProcessWithoutNullStreams | null = null;
switch (model) {
2022-12-02 15:21:42 +01:00
default:
2022-11-25 08:30:03 +01:00
upscayl = spawn(
execPath("realesrgan"),
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
modelsPath,
"-n",
model,
],
{
cwd: undefined,
detached: false,
}
);
2022-12-02 15:21:42 +01:00
console.log(
"🆙 COMMAND: ",
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
modelsPath,
"-n",
model
);
2022-11-25 08:30:03 +01:00
break;
case "models-DF2K":
upscayl = spawn(
execPath("realsr"),
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
modelsPath + "/" + model,
],
{
cwd: undefined,
detached: false,
}
);
2022-12-02 15:21:42 +01:00
console.log(
"🆙 COMMAND: ",
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
modelsPath + "/" + model
);
2022-11-25 08:30:03 +01:00
break;
}
let failed = false;
upscayl?.stderr.on("data", (data) => {
console.log(
"🚀 => upscayl.stderr.on => stderr.toString()",
data.toString()
);
data = data.toString();
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
});
upscayl?.on("error", (data) => {
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
failed = true;
return;
});
// Send done comamnd when
upscayl?.on("close", (code) => {
if (failed !== true) {
console.log("Done upscaling");
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
}
});
}
});
//------------------------Video Upscayl-----------------------------//
ipcMain.on(commands.UPSCAYL_VIDEO, async (event, payload) => {
2022-11-26 10:42:16 +01:00
// Extract the model
2022-11-25 08:30:03 +01:00
const model = payload.model;
2022-11-26 10:42:16 +01:00
// Extract the Video Directory
let videoFileName = payload.videoPath.replace(/^.*[\\\/]/, "");
const justFileName = parse(videoFileName).name;
2022-08-30 10:45:06 +02:00
2022-11-26 10:42:16 +01:00
let inputDir = payload.videoPath.match(/(.*)[\/\\]/)[1] || "";
console.log("🚀 => file: index.ts => line 337 => inputDir", inputDir);
2022-08-30 10:45:06 +02:00
2022-11-26 10:42:16 +01:00
// Set the output directory
let outputDir = payload.outputPath + "_frames";
console.log("🚀 => file: index.ts => line 340 => outputDir", outputDir);
2022-09-11 13:34:36 +02:00
2022-12-04 01:59:16 +01:00
let frameExtractionPath = join(inputDir, justFileName + "_f");
let frameUpscalePath = join(inputDir, justFileName + "_u");
2022-11-26 10:42:16 +01:00
console.log(
"🚀 => file: index.ts => line 342 => frameExtractionPath",
2022-12-04 01:59:16 +01:00
frameExtractionPath,
frameUpscalePath
2022-11-26 10:42:16 +01:00
);
if (!fs.existsSync(frameExtractionPath)) {
fs.mkdirSync(frameExtractionPath, { recursive: true });
2022-08-29 15:41:59 +02:00
}
2022-12-04 01:59:16 +01:00
if (!fs.existsSync(frameUpscalePath)) {
fs.mkdirSync(frameUpscalePath, { recursive: true });
}
2022-11-26 11:20:44 +01:00
let ffmpegProcess: ChildProcessWithoutNullStreams | null = null;
ffmpegProcess = spawn(
ffmpeg.path,
[
"-i",
inputDir + "/" + videoFileName,
frameExtractionPath + "/" + "out%d.png",
],
{
cwd: undefined,
detached: false,
}
);
let failed = false;
2022-12-04 01:59:16 +01:00
ffmpegProcess?.stderr.on("data", (data: string) => {
console.log("🚀 => file: index.ts:420 => data", data.toString());
2022-11-26 11:20:44 +01:00
data = data.toString();
mainWindow.webContents.send(
2022-12-04 01:59:16 +01:00
commands.FFMPEG_VIDEO_PROGRESS,
2022-11-26 11:20:44 +01:00
data.toString()
);
});
2022-12-04 01:59:16 +01:00
ffmpegProcess?.on("error", (data: string) => {
2022-11-26 11:20:44 +01:00
mainWindow.webContents.send(
2022-12-04 01:59:16 +01:00
commands.FFMPEG_VIDEO_PROGRESS,
2022-11-26 11:20:44 +01:00
data.toString()
);
failed = true;
return;
});
// Send done comamnd when
2022-12-04 01:59:16 +01:00
ffmpegProcess?.on("close", (code: number) => {
2022-11-26 11:20:44 +01:00
if (failed !== true) {
2022-12-04 01:59:16 +01:00
console.log("Frame extraction successful!");
mainWindow.webContents.send(commands.FFMPEG_VIDEO_DONE, outputDir);
// UPSCALE
let upscayl: ChildProcessWithoutNullStreams | null = null;
upscayl = spawn(
execPath("realesrgan"),
[
"-i",
frameExtractionPath,
"-o",
frameUpscalePath,
"-s",
4,
"-m",
modelsPath,
"-n",
model,
],
{
cwd: undefined,
detached: false,
}
);
upscayl?.stderr.on("data", (data) => {
console.log(
"🚀 => upscayl.stderr.on => stderr.toString()",
data.toString()
);
data = data.toString();
mainWindow.webContents.send(
commands.FFMPEG_VIDEO_PROGRESS,
data.toString()
);
});
2022-11-26 11:20:44 +01:00
}
});
2022-08-18 11:53:23 +02:00
});
2022-08-21 17:11:11 +02:00
//------------------------Upscayl Folder-----------------------------//
ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
2022-12-02 15:21:42 +01:00
// GET THE MODEL
const model = payload.model;
2022-12-02 15:21:42 +01:00
// GET THE IMAGE DIRECTORY
let inputDir = payload.batchFolderPath;
2022-12-02 15:21:42 +01:00
console.log("🚀 => file: index.ts => line 471 => inputDir", inputDir);
// GET THE OUTPUT DIRECTORY
let outputDir = model.includes("models-DF2K")
? payload.outputPath + "_sharpened"
: payload.outputPath;
console.log("🚀 => file: index.ts => line 474 => outputDir", outputDir);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// UPSCALE
2022-11-11 21:39:28 +01:00
let upscayl: ChildProcessWithoutNullStreams | null = null;
switch (model) {
2022-12-02 15:21:42 +01:00
default:
upscayl = spawn(
execPath("realesrgan"),
[
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-m",
modelsPath,
"-n",
model,
],
{
2022-11-11 21:39:28 +01:00
cwd: undefined,
detached: false,
}
);
2022-12-02 15:21:42 +01:00
console.log(
"🆙 COMMAND:",
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-m",
modelsPath,
"-n",
model
);
break;
case "models-DF2K":
upscayl = spawn(
execPath("realsr"),
[
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-x",
"-m",
modelsPath + "/" + model,
],
{
2022-11-11 21:39:28 +01:00
cwd: undefined,
detached: false,
}
);
2022-12-02 15:21:42 +01:00
console.log(
"🆙 COMMAND:",
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-x",
"-m",
modelsPath + "/" + model
);
break;
}
let failed = false;
2022-11-11 21:39:28 +01:00
upscayl?.stderr.on("data", (data) => {
console.log(
"🚀 => upscayl.stderr.on => stderr.toString()",
data.toString()
);
data = data.toString();
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
});
2022-11-11 21:39:28 +01:00
upscayl?.on("error", (data) => {
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
failed = true;
return;
});
// Send done comamnd when
2022-11-11 21:39:28 +01:00
upscayl?.on("close", (code) => {
if (failed !== true) {
console.log("Done upscaling");
mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir);
}
});
});
//------------------------Auto-Update Code-----------------------------//
2022-08-30 10:45:06 +02:00
// ! AUTO UPDATE STUFF
2022-11-11 21:39:28 +01:00
autoUpdater.on("update-available", ({ releaseNotes, releaseName }) => {
2022-08-21 17:11:11 +02:00
const dialogOpts = {
2022-08-23 05:13:08 +02:00
type: "info",
buttons: ["Ok"],
title: "Application Update",
2022-11-11 21:39:28 +01:00
message:
process.platform === "win32"
? (releaseNotes as string)
: (releaseName as string),
2022-08-23 05:13:08 +02:00
detail: "A new version is being downloaded.",
};
2022-11-11 21:39:28 +01:00
dialog.showMessageBox(dialogOpts).then((returnValue) => {});
2022-08-23 05:13:08 +02:00
});
2022-11-11 21:39:28 +01:00
autoUpdater.on("update-downloaded", (event) => {
const dialogOpts: MessageBoxOptions = {
2022-08-23 05:13:08 +02:00
type: "info",
buttons: ["Restart", "Later"],
title: "Application Update",
2022-11-11 21:39:28 +01:00
message:
process.platform === "win32"
? (event.releaseNotes as string)
: (event.releaseName as string),
2022-08-23 05:13:08 +02:00
detail:
"A new version has been downloaded. Restart the application to apply the updates.",
2022-08-21 17:11:11 +02:00
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
2022-08-23 05:13:08 +02:00
if (returnValue.response === 0) autoUpdater.quitAndInstall();
});
2022-08-21 17:11:11 +02:00
});