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 { BrowserWindow, app, dialog, ipcMain } from "electron";
import { statSync, unlink } from "fs"; 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 path = require("path");
import ffmpegPath = require("ffmpeg-static"); import ffmpegPath = require("ffmpeg-static");
@ -20,11 +25,27 @@ const createWindow = () => {
}); });
win.loadFile(path.join(path.resolve(__dirname, ".."), "pages", "index.html")); win.loadFile(path.join(path.resolve(__dirname, ".."), "pages", "index.html"));
win.webContents.openDevTools(); // debug
return win; return win;
}; };
/* Ready to create the window */
app.whenReady().then(() => {
const win = createWindow();
/** Ask user a file */
const askFile = async () => {
return dialog.showOpenDialogSync(win, {
filters: [moviesFilter],
properties: ["openFile", "dontAddToRecent"],
});
};
/** Send confirmation to user */
const confirmation = async (message: string) => {
dialog.showMessageBoxSync(win, { message });
};
/** Merge all audios track of a video into one */ /** Merge all audios track of a video into one */
const mergeAudio = async (file: string) => { const mergeAudio = async (file: string) => {
const tmpFile = getNewFilename(file, "TMP_"); const tmpFile = getNewFilename(file, "TMP_");
@ -33,12 +54,12 @@ const mergeAudio = async (file: string) => {
// Merge 2 audio // Merge 2 audio
await execute( 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}"` `${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 // Add merged audio as first position to original video
await execute( await execute(
`${ffmpegPath} -y -i "${tmpFile}" -i "${file}" -map 0 -map 1:a -c:v copy "${outFile}"` `${ffmpegPath} -y -i "${tmpFile}" -i "${file}" -map 0 -map 1:a -c:v copy "${outFile}"`
); ).catch((e) => printAndDevTool(win, e));
// Delete the temporary file // Delete the temporary file
unlink(tmpFile, (err) => { unlink(tmpFile, (err) => {
@ -66,7 +87,7 @@ const reduceSize = async (file: string, bitrate: number) => {
await execute( 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 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}"` ${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 // Delete the old file
unlink(file, (err) => { unlink(file, (err) => {
@ -78,23 +99,6 @@ const reduceSize = async (file: string, bitrate: number) => {
return finalFile; return finalFile;
}; };
/* Ready to create the window */
app.whenReady().then(() => {
const win = createWindow();
/** Ask user a file */
const askFile = async () => {
return dialog.showOpenDialogSync(win, {
filters: [moviesFilter],
properties: ["openFile", "dontAddToRecent"],
});
};
/** Send confirmation to user */
const confirmation = async (message: string) => {
dialog.showMessageBoxSync(win, { message });
};
/* Context bridge */ /* Context bridge */
ipcMain.handle("argv", () => process.argv); ipcMain.handle("argv", () => process.argv);
ipcMain.handle("allowedExtensions", () => moviesFilter); ipcMain.handle("allowedExtensions", () => moviesFilter);

View file

@ -1,6 +1,7 @@
import ffprobe = require("ffprobe-static"); import ffprobe = require("ffprobe-static");
import child_process = require("child_process"); import child_process = require("child_process");
import path = require("path"); import path = require("path");
import { BrowserWindow } from "electron";
/** Create a new filename from the OG one */ /** Create a new filename from the OG one */
export const getNewFilename = (ogFile: string, part: string) => { export const getNewFilename = (ogFile: string, part: string) => {
@ -15,6 +16,12 @@ export const getVideoDuration = (file: string) => {
return parseFloat(durationString); 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 */ /** Run a command asynchronously */
export const execute = (command: string) => { export const execute = (command: string) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {