1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-11-13 18:30:54 +01:00

Added upscayl video functions

This commit is contained in:
Feenix 2022-11-26 15:12:16 +05:30
parent 4caa5bc7ce
commit 0ce6046350
9 changed files with 37 additions and 193 deletions

View File

@ -229,11 +229,8 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
let outputDir = payload.outputPath; let outputDir = payload.outputPath;
// COPY IMAGE TO TMP FOLDER // COPY IMAGE TO TMP FOLDER
const platform = getPlatform(); const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "");
const fullfileName =
platform === "win"
? payload.imagePath.split("\\").slice(-1)[0]
: payload.imagePath.split("/").slice(-1)[0];
console.log(fullfileName); console.log(fullfileName);
const fileName = parse(fullfileName).name; const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext; const fileExt = parse(fullfileName).ext;
@ -329,107 +326,28 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
//------------------------Video Upscayl-----------------------------// //------------------------Video Upscayl-----------------------------//
ipcMain.on(commands.UPSCAYL_VIDEO, async (event, payload) => { ipcMain.on(commands.UPSCAYL_VIDEO, async (event, payload) => {
// Extract the model
const model = payload.model; const model = payload.model;
const scale = payload.scaleFactor;
// Extract the Video Directory
let videoFileName = payload.videoPath.replace(/^.*[\\\/]/, "");
const justFileName = parse(videoFileName).name;
let inputDir = payload.videoPath.match(/(.*)[\/\\]/)[1] || ""; let inputDir = payload.videoPath.match(/(.*)[\/\\]/)[1] || "";
let outputDir = payload.outputPath; console.log("🚀 => file: index.ts => line 337 => inputDir", inputDir);
// COPY IMAGE TO TMP FOLDER // Set the output directory
const platform = getPlatform(); let outputDir = payload.outputPath + "_frames";
const fullfileName = console.log("🚀 => file: index.ts => line 340 => outputDir", outputDir);
platform === "win"
? payload.videoPath.split("\\").slice(-1)[0]
: payload.videoPath.split("/").slice(-1)[0];
console.log(fullfileName);
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
const outFile = model.includes("realesrgan")
? outputDir + "/" + fileName + "_upscayl_" + scale + "x_" + model + fileExt
: outputDir +
"/" +
fileName +
"_upscayl_sharpened_" +
scale +
"x_" +
model +
fileExt;
// UPSCALE
if (fs.existsSync(outFile)) {
// If already upscayled, just output that file
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
} else {
let upscayl: ChildProcessWithoutNullStreams | null = null;
switch (model) {
case "realesrgan-x4plus":
case "realesrgan-x4plus-anime":
upscayl = spawn(
execPath("realesrgan"),
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
modelsPath,
"-n",
model,
],
{
cwd: undefined,
detached: false,
}
);
break;
case "models-DF2K":
upscayl = spawn(
execPath("realsr"),
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
modelsPath + "/" + model,
],
{
cwd: undefined,
detached: false,
}
);
break;
}
let failed = false; let frameExtractionPath = join(inputDir, justFileName + "_frames");
upscayl?.stderr.on("data", (data) => { console.log(
console.log( "🚀 => file: index.ts => line 342 => frameExtractionPath",
"🚀 => upscayl.stderr.on => stderr.toString()", frameExtractionPath
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) => { if (!fs.existsSync(frameExtractionPath)) {
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString()); fs.mkdirSync(frameExtractionPath, { recursive: true });
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);
}
});
} }
}); });

View File

@ -202,10 +202,7 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || ""; let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || "";
let outputDir = payload.outputPath; let outputDir = payload.outputPath;
// COPY IMAGE TO TMP FOLDER // COPY IMAGE TO TMP FOLDER
const platform = (0, getPlatform_1.default)(); const fullfileName = payload.imagePath.replace(/^.*[\\\/]/, "");
const fullfileName = platform === "win"
? payload.imagePath.split("\\").slice(-1)[0]
: payload.imagePath.split("/").slice(-1)[0];
console.log(fullfileName); console.log(fullfileName);
const fileName = (0, path_1.parse)(fullfileName).name; const fileName = (0, path_1.parse)(fullfileName).name;
const fileExt = (0, path_1.parse)(fullfileName).ext; const fileExt = (0, path_1.parse)(fullfileName).ext;
@ -287,93 +284,22 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
})); }));
//------------------------Video Upscayl-----------------------------// //------------------------Video Upscayl-----------------------------//
electron_1.ipcMain.on(commands_1.default.UPSCAYL_VIDEO, (event, payload) => __awaiter(void 0, void 0, void 0, function* () { electron_1.ipcMain.on(commands_1.default.UPSCAYL_VIDEO, (event, payload) => __awaiter(void 0, void 0, void 0, function* () {
// Extract the model
const model = payload.model; const model = payload.model;
const scale = payload.scaleFactor; // Extract the Video Directory
let videoFileName = payload.videoPath.replace(/^.*[\\\/]/, "");
const justFileName = (0, path_1.parse)(videoFileName).name;
let inputDir = payload.videoPath.match(/(.*)[\/\\]/)[1] || ""; let inputDir = payload.videoPath.match(/(.*)[\/\\]/)[1] || "";
let outputDir = payload.outputPath; console.log("🚀 => file: index.ts => line 337 => inputDir", inputDir);
// COPY IMAGE TO TMP FOLDER // Set the output directory
const platform = (0, getPlatform_1.default)(); let outputDir = payload.outputPath + "_frames";
const fullfileName = platform === "win" console.log("🚀 => file: index.ts => line 340 => outputDir", outputDir);
? payload.videoPath.split("\\").slice(-1)[0] let frameExtractionPath = (0, path_1.join)(inputDir, justFileName + "_frames");
: payload.videoPath.split("/").slice(-1)[0]; console.log("🚀 => file: index.ts => line 342 => frameExtractionPath", frameExtractionPath);
console.log(fullfileName); if (!fs_1.default.existsSync(frameExtractionPath)) {
const fileName = (0, path_1.parse)(fullfileName).name; fs_1.default.mkdirSync(frameExtractionPath, { recursive: true });
const fileExt = (0, path_1.parse)(fullfileName).ext;
const outFile = model.includes("realesrgan")
? outputDir + "/" + fileName + "_upscayl_" + scale + "x_" + model + fileExt
: outputDir +
"/" +
fileName +
"_upscayl_sharpened_" +
scale +
"x_" +
model +
fileExt;
// UPSCALE
if (fs_1.default.existsSync(outFile)) {
// If already upscayled, just output that file
mainWindow.webContents.send(commands_1.default.UPSCAYL_DONE, outFile);
}
else {
let upscayl = null;
switch (model) {
case "realesrgan-x4plus":
case "realesrgan-x4plus-anime":
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realesrgan"), [
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
binaries_1.modelsPath,
"-n",
model,
], {
cwd: undefined,
detached: false,
});
break;
case "models-DF2K":
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realsr"), [
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
binaries_1.modelsPath + "/" + model,
], {
cwd: undefined,
detached: false,
});
break;
}
let failed = false;
upscayl === null || upscayl === void 0 ? void 0 : upscayl.stderr.on("data", (data) => {
console.log("🚀 => upscayl.stderr.on => stderr.toString()", data.toString());
data = data.toString();
mainWindow.webContents.send(commands_1.default.UPSCAYL_PROGRESS, data.toString());
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
});
upscayl === null || upscayl === void 0 ? void 0 : upscayl.on("error", (data) => {
mainWindow.webContents.send(commands_1.default.UPSCAYL_PROGRESS, data.toString());
failed = true;
return;
});
// Send done comamnd when
upscayl === null || upscayl === void 0 ? void 0 : upscayl.on("close", (code) => {
if (failed !== true) {
console.log("Done upscaling");
mainWindow.webContents.send(commands_1.default.UPSCAYL_DONE, outFile);
}
});
} }
console.log(upscayl_ffmpeg_1.default.path());
})); }));
//------------------------Upscayl Folder-----------------------------// //------------------------Upscayl Folder-----------------------------//
electron_1.ipcMain.on(commands_1.default.FOLDER_UPSCAYL, (event, payload) => __awaiter(void 0, void 0, void 0, function* () { electron_1.ipcMain.on(commands_1.default.FOLDER_UPSCAYL, (event, payload) => __awaiter(void 0, void 0, void 0, function* () {

View File

@ -284,7 +284,7 @@ const Home = () => {
} else { } else {
setUpscaledImagePath(""); setUpscaledImagePath("");
} }
if (imagePath !== "" || batchFolderPath !== "") { if (!isVideo && (imagePath !== "" || batchFolderPath !== "")) {
setProgress("Hold on..."); setProgress("Hold on...");
if (model === "models-DF2K") { if (model === "models-DF2K") {
setDoubleUpscayl(false); setDoubleUpscayl(false);
@ -312,15 +312,15 @@ const Home = () => {
model, model,
}); });
} }
} else if (videoPath !== "") { } else if (isVideo && videoPath !== "") {
await window.electron.send(commands.UPSCAYL_VIDEO, { await window.electron.send(commands.UPSCAYL_VIDEO, {
scaleFactor, scaleFactor,
imagePath, videoPath,
outputPath, outputPath,
model, model,
}); });
} else { } else {
alert("Please select an image to upscale"); alert(`Please select ${isVideo ? "a video" : "an image"} to upscale`);
} }
}; };