Open devtool only if needed

This commit is contained in:
Mylloon 2023-08-01 09:59:37 +02:00
parent d84be4c793
commit 5fc1832fa6
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 66 additions and 55 deletions

View file

@ -1,6 +1,11 @@
import { BrowserWindow, app, dialog, ipcMain } from "electron";
import { statSync, unlink } from "fs";
import { execute, getNewFilename, getVideoDuration } from "./utils/misc";
import {
execute,
getNewFilename,
getVideoDuration,
printAndDevTool,
} from "./utils/misc";
import path = require("path");
import ffmpegPath = require("ffmpeg-static");
@ -20,64 +25,10 @@ const createWindow = () => {
});
win.loadFile(path.join(path.resolve(__dirname, ".."), "pages", "index.html"));
win.webContents.openDevTools(); // debug
return win;
};
/** Merge all audios track of a video into one */
const mergeAudio = async (file: string) => {
const tmpFile = getNewFilename(file, "TMP_");
const outFile = getNewFilename(file, "(merged audio) ");
// Merge 2 audio
await execute(
`${ffmpegPath} -y -i "${file}" -filter_complex "[0:a]amerge=inputs=2[a]" -ac 1 -map 0:v -map "[a]" -c:v copy "${tmpFile}"`
);
// Add merged audio as first position to original video
await execute(
`${ffmpegPath} -y -i "${tmpFile}" -i "${file}" -map 0 -map 1:a -c:v copy "${outFile}"`
);
// Delete the temporary file
unlink(tmpFile, (err) => {
if (err) {
throw err;
}
});
const duration = getVideoDuration(outFile);
const stats = statSync(outFile);
return { title: outFile, size: stats.size / 1024 / 1024, duration };
};
/* Reduce size of a file */
const reduceSize = async (file: string, bitrate: number) => {
const audioBitrate = 400; // keep some room
const videoBitrate = bitrate - audioBitrate;
const finalFile = getNewFilename(file, "Compressed - ");
// Trash the output, depends on the platform
const nul = process.platform === "win32" ? "NUL" : "/dev/null";
await execute(
`${ffmpegPath} -y -i "${file}" -c:v libx264 -b:v ${videoBitrate}k -pass 1 -an -f mp4 ${nul} && \
${ffmpegPath} -y -i "${file}" -c:v libx264 -b:v ${videoBitrate}k -pass 2 -c:a copy -map 0:0 -map 0:1 -map 0:2 -map 0:3 -f mp4 "${finalFile}"`
);
// Delete the old file
unlink(file, (err) => {
if (err) {
throw err;
}
});
return finalFile;
};
/* Ready to create the window */
app.whenReady().then(() => {
const win = createWindow();
@ -95,6 +46,59 @@ app.whenReady().then(() => {
dialog.showMessageBoxSync(win, { message });
};
/** Merge all audios track of a video into one */
const mergeAudio = async (file: string) => {
const tmpFile = getNewFilename(file, "TMP_");
const outFile = getNewFilename(file, "(merged audio) ");
// Merge 2 audio
await execute(
`${ffmpegPath} -y -i "${file}" -filter_complex "[0:a]amerge=inputs=2[a]" -ac 1 -map 0:v -map "[a]" -c:v copy "${tmpFile}"`
).catch((e) => printAndDevTool(win, e));
// Add merged audio as first position to original video
await execute(
`${ffmpegPath} -y -i "${tmpFile}" -i "${file}" -map 0 -map 1:a -c:v copy "${outFile}"`
).catch((e) => printAndDevTool(win, e));
// Delete the temporary file
unlink(tmpFile, (err) => {
if (err) {
throw err;
}
});
const duration = getVideoDuration(outFile);
const stats = statSync(outFile);
return { title: outFile, size: stats.size / 1024 / 1024, duration };
};
/* Reduce size of a file */
const reduceSize = async (file: string, bitrate: number) => {
const audioBitrate = 400; // keep some room
const videoBitrate = bitrate - audioBitrate;
const finalFile = getNewFilename(file, "Compressed - ");
// Trash the output, depends on the platform
const nul = process.platform === "win32" ? "NUL" : "/dev/null";
await execute(
`${ffmpegPath} -y -i "${file}" -c:v libx264 -b:v ${videoBitrate}k -pass 1 -an -f mp4 ${nul} && \
${ffmpegPath} -y -i "${file}" -c:v libx264 -b:v ${videoBitrate}k -pass 2 -c:a copy -map 0:0 -map 0:1 -map 0:2 -map 0:3 -f mp4 "${finalFile}"`
).catch((e) => printAndDevTool(win, e));
// Delete the old file
unlink(file, (err) => {
if (err) {
throw err;
}
});
return finalFile;
};
/* Context bridge */
ipcMain.handle("argv", () => process.argv);
ipcMain.handle("allowedExtensions", () => moviesFilter);

View file

@ -1,6 +1,7 @@
import ffprobe = require("ffprobe-static");
import child_process = require("child_process");
import path = require("path");
import { BrowserWindow } from "electron";
/** Create a new filename from the OG one */
export const getNewFilename = (ogFile: string, part: string) => {
@ -15,6 +16,12 @@ export const getVideoDuration = (file: string) => {
return parseFloat(durationString);
};
/** Print an error to the console and open the dev tool panel */
export const printAndDevTool = (win: BrowserWindow, error: string) => {
console.error(error);
win.webContents.openDevTools();
};
/** Run a command asynchronously */
export const execute = (command: string) => {
return new Promise((resolve, reject) => {