support multiple files at once (#3)

This commit is contained in:
Mylloon 2023-08-24 15:59:46 +02:00
parent 53d19bdb74
commit 33a86cecbd
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 71 additions and 32 deletions

View file

@ -41,11 +41,11 @@ const createWindow = () => {
app.whenReady().then(() => { app.whenReady().then(() => {
const win = createWindow(); const win = createWindow();
/** Ask user a file */ /** Ask user files */
const askFile = () => { const askFiles = () => {
return dialog.showOpenDialogSync(win, { return dialog.showOpenDialogSync(win, {
filters: [moviesFilter], filters: [moviesFilter],
properties: ["openFile", "dontAddToRecent"], properties: ["openFile", "dontAddToRecent", "multiSelections"],
}); });
}; };
@ -54,6 +54,9 @@ app.whenReady().then(() => {
await dialog.showMessageBox(win, { message }); await dialog.showMessageBox(win, { message });
}; };
/** Get filename of a path */
const getFilename = (filepath: string) => path.parse(filepath).base;
/** 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_");
@ -128,7 +131,8 @@ app.whenReady().then(() => {
/* Context bridge */ /* Context bridge */
ipcMain.handle("argv", () => process.argv); ipcMain.handle("argv", () => process.argv);
ipcMain.handle("allowedExtensions", () => moviesFilter); ipcMain.handle("allowedExtensions", () => moviesFilter);
ipcMain.handle("askFile", () => askFile()); ipcMain.handle("getFilename", (_, filepath: string) => getFilename(filepath));
ipcMain.handle("askFiles", () => askFiles());
ipcMain.handle("mergeAudio", (_, file: string) => mergeAudio(file)); ipcMain.handle("mergeAudio", (_, file: string) => mergeAudio(file));
ipcMain.handle("reduceSize", (_, file: string, bitrate: number) => ipcMain.handle("reduceSize", (_, file: string, bitrate: number) =>
reduceSize(file, bitrate) reduceSize(file, bitrate)

View file

@ -9,7 +9,9 @@ ipcRenderer.on("error", (_, err) => {
contextBridge.exposeInMainWorld("internals", { contextBridge.exposeInMainWorld("internals", {
argv: () => ipcRenderer.invoke("argv"), argv: () => ipcRenderer.invoke("argv"),
allowedExtensions: () => ipcRenderer.invoke("allowedExtensions"), allowedExtensions: () => ipcRenderer.invoke("allowedExtensions"),
askFile: () => ipcRenderer.invoke("askFile"), getFilename: (filepath: string) =>
ipcRenderer.invoke("getFilename", filepath),
askFiles: () => ipcRenderer.invoke("askFiles"),
mergeAudio: (file: string) => ipcRenderer.invoke("mergeAudio", file), mergeAudio: (file: string) => ipcRenderer.invoke("mergeAudio", file),
reduceSize: (file: string, bitrate: number) => reduceSize: (file: string, bitrate: number) =>
ipcRenderer.invoke("reduceSize", file, bitrate), ipcRenderer.invoke("reduceSize", file, bitrate),

View file

@ -4,7 +4,8 @@ let internals: {
allowedExtensions: () => Promise<{ allowedExtensions: () => Promise<{
extensions: string[]; extensions: string[];
}>; }>;
askFile: () => Promise<string[] | undefined>; getFilename: (filepath: string) => Promise<string>;
askFiles: () => Promise<string[] | undefined>;
exit: () => Promise<void>; exit: () => Promise<void>;
mergeAudio: ( mergeAudio: (
filename: string filename: string
@ -13,22 +14,30 @@ let internals: {
confirmation: (text: string) => Promise<void>; confirmation: (text: string) => Promise<void>;
}; };
/** Search for a file */ /** Search for files */
const getFile = async () => { const getFiles = async () => {
const allowedExtensions = (await internals.allowedExtensions()).extensions; const allowedExtensions = (await internals.allowedExtensions()).extensions;
const argv = await internals.argv(); const argv = await internals.argv();
if (argv.length === 2) { if (argv.length >= 2) {
const file = argv.pop(); const files = argv.slice(1);
if (allowedExtensions.some((ext) => file.endsWith(ext))) {
return file;
}
}
const file = await internals.askFile(); // Exit if a file isn't supported in the list
if (file === undefined || file.length === 0) { if (
files.filter((file) =>
allowedExtensions.some((ext) => file.endsWith(ext))
).length !== files.length
) {
await internals.exit(); await internals.exit();
} }
return file.join("");
return files;
}
const files = await internals.askFiles();
if (files === undefined || files.length === 0) {
await internals.exit();
}
return files;
}; };
/** Either replace the message, or add some info */ /** Either replace the message, or add some info */
@ -63,12 +72,25 @@ const updateMessage = (
/** Main function */ /** Main function */
const main = async () => { const main = async () => {
const maxSizeDiscord = 25; const maxSizeDiscord = 25;
updateMessage("Récupération du fichier..."); updateMessage("Récupération des fichiers...");
const file = await getFile(); const files = await getFiles();
updateMessage("Mélange des pistes audios vers la piste 1...", true); let processedFiles = "";
// Iterate over all the retrieved files
for (const [idx, file] of files.entries()) {
const counter = `${idx + 1}/${files.length}`;
const filename = await internals.getFilename(file);
updateMessage(
`${counter} - Mélange des pistes audios de ${filename}...`,
true
);
const newFile = await internals.mergeAudio(file); const newFile = await internals.mergeAudio(file);
let finalTitle = newFile.title; let finalTitle = newFile.title;
updateMessage(`Taille calculée : ~${Math.round(newFile.size)}Mio`); updateMessage(
`${counter} - Taille calculée : ~${Math.round(newFile.size)}Mio`
);
// Compress video if needed
if (newFile.size > maxSizeDiscord) { if (newFile.size > maxSizeDiscord) {
const targetSize = maxSizeDiscord - 2; // keep some room const targetSize = maxSizeDiscord - 2; // keep some room
@ -80,10 +102,20 @@ const main = async () => {
true, true,
Mode.Append Mode.Append
); );
// Compress the video and change the title to the new one
finalTitle = await internals.reduceSize(newFile.title, bitrate); finalTitle = await internals.reduceSize(newFile.title, bitrate);
} }
updateMessage("Fichier prêt ! :)");
await internals.confirmation(`File ok @ ${finalTitle}!`); // Append title to the list of processed files
processedFiles += `\n- ${finalTitle}`;
updateMessage(`Fichier ${counter} traités.`);
}
// Send confirmation to the user that we're done
await internals.confirmation(
`${files.length} fichiers traités : ${processedFiles}`
);
await internals.exit(); await internals.exit();
}; };
main(); main();

View file

@ -1,6 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"downlevelIteration": true,
"noImplicitAny": true, "noImplicitAny": true,
"sourceMap": true, "sourceMap": true,
"outDir": "dist", "outDir": "dist",