1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-02-17 19:19:23 +01:00
upscayl/electron/index.ts

549 lines
15 KiB
TypeScript
Raw Normal View History

2023-03-18 17:45:48 +05:30
import {
getBatchArguments,
getBatchSharpenArguments,
getDoubleUpscaleArguments,
getDoubleUpscaleSecondPassArguments,
getSingleImageArguments,
getSingleImageSharpenArguments,
} from "./utils/getArguments";
2022-08-15 10:23:14 +05:30
// Native
2022-11-12 02:09:28 +05:30
import { autoUpdater } from "electron-updater";
2022-11-12 02:17:00 +05:30
import getPlatform from "./getPlatform";
2022-11-23 23:54:30 +05:30
import ffmpeg from "upscayl-ffmpeg";
2023-03-18 17:51:02 +05:30
import { join, parse } from "path";
2023-03-31 16:03:48 +05:30
import log from "electron-log";
2023-03-18 17:51:02 +05:30
import { format } from "url";
import fs from "fs";
2022-08-15 10:23:14 +05:30
2022-11-12 02:09:28 +05:30
import { execPath, modelsPath } from "./binaries";
2022-08-16 07:47:27 +05:30
2022-08-15 10:23:14 +05:30
// Packages
2022-11-12 02:09:28 +05:30
import {
2022-08-17 08:07:50 +05:30
BrowserWindow,
app,
ipcMain,
dialog,
2022-08-18 15:23:23 +05:30
shell,
2022-11-12 02:09:28 +05:30
MessageBoxOptions,
} from "electron";
2022-09-25 16:45:43 +05:30
2023-03-18 17:51:02 +05:30
import { spawnUpscayl } from "./upscayl";
2022-11-12 02:09:28 +05:30
import prepareNext from "electron-next";
2023-03-18 17:51:02 +05:30
import isDev from "electron-is-dev";
2022-11-12 02:09:28 +05:30
import commands from "./commands";
2022-08-15 10:23:14 +05:30
2023-03-31 16:03:48 +05:30
log.initialize({ preload: true });
2022-08-15 10:23:14 +05:30
// Prepare the renderer once the app is ready
2022-08-17 17:06:19 +05:30
let mainWindow;
2022-08-15 10:23:14 +05:30
app.on("ready", async () => {
await prepareNext("./renderer");
2023-03-31 16:03:48 +05:30
log.info("🚀 ICON PATH: ", join(__dirname, "build", "icon.png"));
log.info("🚀 UPSCAYL EXEC PATH: ", execPath(""));
log.info("🚀 MODELS PATH: ", modelsPath);
log.info("🚀 FFMPEG PATH: ", ffmpeg.path);
2022-09-07 08:01:28 +05:30
2022-08-17 17:06:19 +05:30
mainWindow = new BrowserWindow({
2022-09-30 22:09:14 +05:30
icon: join(__dirname, "build", "icon.png"),
2022-12-22 20:29:11 +05:30
width: 1300,
height: 940,
2022-08-18 15:23:23 +05:30
minHeight: 500,
minWidth: 500,
show: false,
2022-08-27 19:49:19 -04:00
backgroundColor: "#171717",
2022-08-15 10:23:14 +05:30
webPreferences: {
2022-08-16 07:47:27 +05:30
nodeIntegration: true,
2022-08-18 15:23:23 +05:30
webSecurity: false,
2022-08-15 10:23:14 +05:30
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 15:23:23 +05:30
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: "deny" };
});
2022-08-21 20:41:11 +05:30
2022-08-30 14:15:06 +05:30
mainWindow.once("ready-to-show", () => {
mainWindow.show();
2022-09-30 22:06:16 +05:30
mainWindow.webContents.setZoomFactor(1);
2022-08-30 14:15:06 +05:30
});
2022-08-21 20:41:11 +05:30
if (!isDev) {
autoUpdater.checkForUpdates();
2022-08-23 08:43:08 +05:30
}
2022-08-15 10:23:14 +05:30
});
// Quit the app once all windows are closed
app.on("window-all-closed", app.quit);
2023-03-31 16:03:48 +05:30
log.log(app.getAppPath());
2023-04-09 09:23:01 +05:30
let imagePath: string | undefined = undefined;
let folderPath: string | undefined = undefined;
let customModelsFolderPath: string | undefined = undefined;
2022-09-25 16:45:43 +05:30
//------------------------Select File-----------------------------//
2022-08-15 12:51:12 +05:30
// ! DONT FORGET TO RESTART THE APP WHEN YOU CHANGE CODE HERE
2022-08-18 15:23:23 +05:30
ipcMain.handle(commands.SELECT_FILE, async () => {
2022-08-16 07:47:27 +05:30
const { canceled, filePaths } = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"],
2023-04-09 09:23:01 +05:30
title: "Select Image",
defaultPath: imagePath,
2022-08-16 07:47:27 +05:30
});
2022-08-15 15:42:48 +05:30
if (canceled) {
2023-04-08 12:23:32 +05:30
log.log("File Operation Cancelled");
2022-08-16 07:47:27 +05:30
return "cancelled";
} else {
2023-04-08 12:23:32 +05:30
log.log("Selected File Path: ", filePaths[0]);
2023-04-09 09:23:01 +05:30
imagePath = filePaths[0];
2022-08-29 19:11:59 +05:30
// CREATE input AND upscaled FOLDER
return filePaths[0];
}
2022-08-18 15:23:23 +05:30
});
2022-09-25 16:45:43 +05:30
//------------------------Select Folder-----------------------------//
2022-08-29 19:11:59 +05:30
ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => {
2023-04-09 09:23:01 +05:30
const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({
2022-08-29 19:11:59 +05:30
properties: ["openDirectory"],
2023-04-09 09:23:01 +05:30
defaultPath: folderPath,
});
if (canceled) {
2023-04-08 12:23:32 +05:30
log.log("operation cancelled");
return "cancelled";
2022-08-18 15:23:23 +05:30
} else {
2023-04-09 09:23:01 +05:30
log.log("Selected Folder Path: ", folderPaths[0]);
folderPath = folderPaths[0];
return folderPaths[0];
}
});
//------------------------Select Custom Models Folder---------------------//
ipcMain.handle(commands.SELECT_CUSTOM_MODEL_FOLDER, async (event, message) => {
const { canceled, filePaths: folderPaths } = await dialog.showOpenDialog({
properties: ["openDirectory"],
title: "Select Custom Models Folder",
defaultPath: customModelsFolderPath,
});
if (canceled) {
log.log("operation cancelled");
return "cancelled";
} else {
log.log("Custom Folder Path: ", folderPaths[0]);
customModelsFolderPath = folderPaths[0];
return folderPaths[0];
}
2022-08-18 15:23:23 +05:30
});
2022-08-16 07:47:27 +05:30
2022-12-02 19:51:42 +05:30
//------------------------Open Folder-----------------------------//
ipcMain.on(commands.OPEN_FOLDER, async (event, payload) => {
2023-04-08 12:23:32 +05:30
log.log(payload);
2022-12-02 19:51:42 +05:30
shell.openPath(payload);
});
//------------------------Double Upscayl-----------------------------//
2022-09-17 15:10:47 +05:30
ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
2023-03-18 17:45:48 +05:30
const model = payload.model as string;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
let outputDir = payload.outputPath as string;
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
2022-09-10 20:22:53 +05:30
// COPY IMAGE TO TMP FOLDER
2022-09-17 15:10:47 +05:30
const platform = getPlatform();
2022-09-10 20:22:53 +05:30
const fullfileName =
platform === "win"
2023-03-18 17:45:48 +05:30
? (payload.imagePath.split("\\").slice(-1)[0] as string)
: (payload.imagePath.split("/").slice(-1)[0] as string);
2022-09-10 20:22:53 +05:30
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
2022-12-16 21:50:46 +05:30
const outFile =
2023-03-18 17:28:38 +05:30
outputDir + "/" + fileName + "_upscayl_16x_" + model + "." + saveImageAs;
2022-09-10 20:22:53 +05:30
2022-09-17 15:10:47 +05:30
// UPSCALE
2023-03-18 17:28:38 +05:30
let upscayl = spawnUpscayl(
"realesrgan",
2023-03-18 17:45:48 +05:30
getDoubleUpscaleArguments(
2023-03-18 17:28:38 +05:30
inputDir,
fullfileName,
2023-03-18 17:45:48 +05:30
outFile,
2022-09-17 15:10:47 +05:30
modelsPath,
model,
2023-03-18 17:28:38 +05:30
gpuId,
saveImageAs
)
2022-12-02 19:51:42 +05:30
);
2022-09-11 15:57:47 +05:30
let failed = false;
2022-12-27 12:15:16 +05:30
let isAlpha = false;
2023-03-18 17:28:38 +05:30
let failed2 = false;
2022-12-27 12:15:16 +05:30
2023-03-18 17:28:38 +05:30
const onData = (data) => {
2022-09-17 15:10:47 +05:30
// CONVERT DATA TO STRING
2022-09-11 15:57:47 +05:30
data = data.toString();
2022-09-17 15:10:47 +05:30
// PRINT TO CONSOLE
2023-04-08 12:23:32 +05:30
log.log(data);
2022-09-17 15:10:47 +05:30
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
2022-09-11 15:57:47 +05:30
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
2022-12-27 12:15:16 +05:30
if (data.includes("has alpha channel")) {
isAlpha = true;
}
2023-03-18 17:28:38 +05:30
};
const onError = (data) => {
2022-09-17 15:10:47 +05:30
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed = true;
return;
2023-03-18 17:28:38 +05:30
};
const onData2 = (data) => {
// CONVERT DATA TO STRING
data = data.toString();
// PRINT TO CONSOLE
2023-04-08 12:23:32 +05:30
log.log(data);
2023-03-18 17:28:38 +05:30
// 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")) {
failed2 = true;
}
};
const onError2 = (data) => {
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed2 = true;
return;
};
const onClose2 = (code) => {
if (!failed2) {
2023-04-08 12:23:32 +05:30
log.log("Done upscaling");
2023-03-18 17:28:38 +05:30
mainWindow.webContents.send(
commands.DOUBLE_UPSCAYL_DONE,
isAlpha ? outFile + ".png" : outFile
);
}
};
2022-09-17 15:10:47 +05:30
2023-03-18 17:28:38 +05:30
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", (code) => {
2022-09-17 15:10:47 +05:30
// IF NOT FAILED
if (!failed) {
// UPSCALE
2023-03-18 17:28:38 +05:30
let upscayl2 = spawnUpscayl(
"realesrgan",
2023-03-18 17:45:48 +05:30
getDoubleUpscaleSecondPassArguments(
isAlpha,
2023-03-18 17:28:38 +05:30
outFile,
2022-12-16 21:50:46 +05:30
modelsPath,
model,
2023-03-18 17:28:38 +05:30
gpuId,
2023-03-18 17:45:48 +05:30
saveImageAs
2023-03-18 17:28:38 +05:30
)
);
2022-09-17 15:10:47 +05:30
2023-03-18 17:28:38 +05:30
upscayl2.process.stderr.on("data", onData2);
upscayl2.process.on("error", onError2);
upscayl2.process.on("close", onClose2);
2022-09-11 15:57:47 +05:30
}
2022-09-17 15:10:47 +05:30
});
2022-09-10 20:22:53 +05:30
});
//------------------------Image Upscayl-----------------------------//
2022-08-18 15:23:23 +05:30
ipcMain.on(commands.UPSCAYL, async (event, payload) => {
2023-03-18 17:45:48 +05:30
const model = payload.model as string;
2022-08-18 15:23:23 +05:30
const scale = payload.scaleFactor;
2023-03-18 17:45:48 +05:30
const gpuId = payload.gpuId as string;
const saveImageAs = payload.saveImageAs as string;
let inputDir = (payload.imagePath.match(/(.*)[\/\\]/)[1] || "") as string;
let outputDir = payload.outputPath as string;
2022-09-10 20:22:53 +05:30
// COPY IMAGE TO TMP FOLDER
2023-03-18 17:45:48 +05:30
const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "") as string;
2022-11-26 15:12:16 +05:30
const fileName = parse(fullfileName).name;
2023-04-08 12:23:32 +05:30
log.log("🚀 => fileName", fileName);
2022-12-27 12:15:16 +05:30
const fileExt = parse(fullfileName).ext;
2023-04-08 12:23:32 +05:30
log.log("🚀 => fileExt", fileExt);
2022-12-27 12:15:16 +05:30
2023-03-18 18:03:17 +05:30
const outFile =
outputDir +
"/" +
fileName +
"_upscayl_" +
scale +
"x_" +
model +
"." +
saveImageAs;
2022-12-02 19:51:42 +05:30
// UPSCALE
2022-08-29 19:11:59 +05:30
if (fs.existsSync(outFile)) {
2022-09-07 08:01:28 +05:30
// If already upscayled, just output that file
2022-08-30 14:15:06 +05:30
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
2022-11-25 13:00:03 +05:30
} else {
2023-03-18 18:03:17 +05:30
const upscayl = spawnUpscayl(
"realesrgan",
getSingleImageArguments(
inputDir,
fullfileName,
outFile,
modelsPath,
model,
scale,
gpuId,
saveImageAs
)
);
2022-11-25 13:00:03 +05:30
2022-12-27 12:15:16 +05:30
let isAlpha = false;
2022-11-25 13:00:03 +05:30
let failed = false;
2022-12-27 12:15:16 +05:30
2023-03-12 15:19:02 +05:30
const onData = (data: string) => {
2023-04-08 12:23:32 +05:30
log.log("image upscayl: ", data.toString());
2022-11-25 13:00:03 +05:30
data = data.toString();
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
2022-12-27 12:15:16 +05:30
if (data.includes("has alpha channel")) {
2023-04-08 12:23:32 +05:30
log.log("INCLUDES ALPHA CHANNEL, CHANGING OUTFILE NAME!");
2022-12-27 12:15:16 +05:30
isAlpha = true;
}
2023-03-12 15:19:02 +05:30
};
const onError = (data) => {
2022-11-25 13:00:03 +05:30
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
failed = true;
return;
2023-03-12 15:19:02 +05:30
};
const onClose = () => {
2022-11-25 13:00:03 +05:30
if (failed !== true) {
2023-04-08 12:23:32 +05:30
log.log("Done upscaling");
2022-12-27 12:15:16 +05:30
mainWindow.webContents.send(
commands.UPSCAYL_DONE,
isAlpha ? outFile + ".png" : outFile
);
2022-11-25 13:00:03 +05:30
}
2023-03-12 15:19:02 +05:30
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
2022-11-25 13:00:03 +05:30
}
});
//------------------------Upscayl Folder-----------------------------//
ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
2022-12-02 19:51:42 +05:30
// GET THE MODEL
const model = payload.model;
2022-12-16 21:50:46 +05:30
const gpuId = payload.gpuId;
const saveImageAs = payload.saveImageAs;
2022-12-02 19:51:42 +05:30
// GET THE IMAGE DIRECTORY
let inputDir = payload.batchFolderPath;
2022-12-02 19:51:42 +05:30
// GET THE OUTPUT DIRECTORY
2023-03-18 18:03:17 +05:30
let outputDir = payload.outputPath;
2022-12-02 19:51:42 +05:30
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// UPSCALE
2023-03-18 18:03:17 +05:30
const upscayl = spawnUpscayl(
"realesrgan",
getBatchArguments(
inputDir,
outputDir,
modelsPath,
model,
gpuId,
saveImageAs
)
);
let failed = false;
2023-03-18 18:03:17 +05:30
const onData = (data: any) => {
2023-04-08 12:23:32 +05:30
log.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;
}
2023-03-18 17:28:38 +05:30
};
2023-03-18 18:03:17 +05:30
const onError = (data: any) => {
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
failed = true;
return;
2023-03-18 17:28:38 +05:30
};
2023-03-18 17:47:06 +05:30
const onClose = () => {
if (failed !== true) {
2023-04-08 12:23:32 +05:30
log.log("Done upscaling");
mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir);
}
2023-03-18 17:28:38 +05:30
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
});
//------------------------Auto-Update Code-----------------------------//
2022-08-30 14:15:06 +05:30
// ! AUTO UPDATE STUFF
2022-11-12 02:09:28 +05:30
autoUpdater.on("update-available", ({ releaseNotes, releaseName }) => {
2022-08-21 20:41:11 +05:30
const dialogOpts = {
2022-08-23 08:43:08 +05:30
type: "info",
2023-03-18 17:50:32 +05:30
buttons: ["Ok cool"],
title: "New Upscayl Update",
2023-03-18 17:47:06 +05:30
message: releaseName as string,
2023-03-18 17:50:32 +05:30
detail:
"A new version is being downloaded. Please check GitHub for more details.",
2022-08-23 08:43:08 +05:30
};
2022-11-12 02:09:28 +05:30
dialog.showMessageBox(dialogOpts).then((returnValue) => {});
2022-08-23 08:43:08 +05:30
});
2022-11-12 02:09:28 +05:30
autoUpdater.on("update-downloaded", (event) => {
const dialogOpts: MessageBoxOptions = {
2022-08-23 08:43:08 +05:30
type: "info",
buttons: ["Restart", "Later"],
2023-03-18 17:50:32 +05:30
title: "New Upscayl Update",
2023-03-18 17:47:06 +05:30
message: event.releaseName as string,
2022-08-23 08:43:08 +05:30
detail:
"A new version has been downloaded. Restart the application to apply the updates.",
2022-08-21 20:41:11 +05:30
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
2022-08-23 08:43:08 +05:30
if (returnValue.response === 0) autoUpdater.quitAndInstall();
});
2022-08-21 20:41:11 +05:30
});
2023-03-18 17:47:06 +05:30
//------------------------Video Upscayl-----------------------------//
// ipcMain.on(commands.UPSCAYL_VIDEO, async (event, payload) => {
// // Extract the model
// const model = payload.model;
// // Extract the Video Directory
// let videoFileName = payload.videoPath.replace(/^.*[\\\/]/, "");
// const justFileName = parse(videoFileName).name;
// let inputDir = payload.videoPath.match(/(.*)[\/\\]/)[1] || "";
2023-04-08 12:23:32 +05:30
// log.log("🚀 => file: index.ts => line 337 => inputDir", inputDir);
2023-03-18 17:47:06 +05:30
// // Set the output directory
// let outputDir = payload.outputPath + "_frames";
2023-04-08 12:23:32 +05:30
// log.log("🚀 => file: index.ts => line 340 => outputDir", outputDir);
2023-03-18 17:47:06 +05:30
// let frameExtractionPath = join(inputDir, justFileName + "_f");
// let frameUpscalePath = join(inputDir, justFileName + "_u");
2023-04-08 12:23:32 +05:30
// log.log(
2023-03-18 17:47:06 +05:30
// "🚀 => file: index.ts => line 342 => frameExtractionPath",
// frameExtractionPath,
// frameUpscalePath
// );
// if (!fs.existsSync(frameExtractionPath)) {
// fs.mkdirSync(frameExtractionPath, { recursive: true });
// }
// if (!fs.existsSync(frameUpscalePath)) {
// fs.mkdirSync(frameUpscalePath, { recursive: true });
// }
// let ffmpegProcess: ChildProcessWithoutNullStreams | null = null;
// ffmpegProcess = spawn(
// ffmpeg.path,
// [
// "-i",
// inputDir + "/" + videoFileName,
// frameExtractionPath + "/" + "out%d.png",
// ],
// {
// cwd: undefined,
// detached: false,
// }
// );
// let failed = false;
// ffmpegProcess?.stderr.on("data", (data: string) => {
2023-04-08 12:23:32 +05:30
// log.log("🚀 => file: index.ts:420 => data", data.toString());
2023-03-18 17:47:06 +05:30
// data = data.toString();
// mainWindow.webContents.send(
// commands.FFMPEG_VIDEO_PROGRESS,
// data.toString()
// );
// });
// ffmpegProcess?.on("error", (data: string) => {
// mainWindow.webContents.send(
// commands.FFMPEG_VIDEO_PROGRESS,
// data.toString()
// );
// failed = true;
// return;
// });
// // Send done comamnd when
// ffmpegProcess?.on("close", (code: number) => {
// if (failed !== true) {
2023-04-08 12:23:32 +05:30
// log.log("Frame extraction successful!");
2023-03-18 17:47:06 +05:30
// 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) => {
2023-04-08 12:23:32 +05:30
// log.log(
2023-03-18 17:47:06 +05:30
// "🚀 => upscayl.stderr.on => stderr.toString()",
// data.toString()
// );
// data = data.toString();
// mainWindow.webContents.send(
// commands.FFMPEG_VIDEO_PROGRESS,
// data.toString()
// );
// });
// }
// });
// });