From 4f34191d2f39c13a8237ff804f82ef491739cd5b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 1 Oct 2024 16:55:06 +0200 Subject: [PATCH] don't crash when trying to compress an enormous file --- src/main.ts | 81 ++++++++++++++++++++++------------------- src/scripts/renderer.ts | 19 ++++++++-- 2 files changed, 60 insertions(+), 40 deletions(-) diff --git a/src/main.ts b/src/main.ts index a821bca..d3b9fcb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -144,7 +144,9 @@ app.whenReady().then(() => { }; }; - /** Reduce size of a file */ + /** Reduce size of a file + * Returns an empty string in case of failing + */ const reduceSize = async ( file: string, bitrate: number, @@ -153,46 +155,48 @@ app.whenReady().then(() => { const audioBitrate = Math.ceil( audioTracks.reduce((sum, current) => current + sum, 0) ); - let videoBitrate = bitrate - audioBitrate; + const videoBitrate = bitrate - audioBitrate; + let finalFile; - const finalFile = getNewFilename(file, "Compressed - "); + if (videoBitrate > 0) { + finalFile = getNewFilename(file, "Compressed - "); - // Trash the output, depends on the platform - const nul = process.platform === "win32" ? "NUL" : "/dev/null"; + // Trash the output, depends on the platform + const nul = process.platform === "win32" ? "NUL" : "/dev/null"; - // Mapping of tracks for FFMPEG, adding 1 for the video stream - const mappingTracks = Array(audioTracks.length + 1) - .fill("-map 0:") - .map((str, index) => { - return str + index; - }) - .join(" "); + // Mapping of tracks for FFMPEG, adding 1 for the video stream + const mappingTracks = Array(audioTracks.length + 1) + .fill("-map 0:") + .map((str, index) => { + return str + index; + }) + .join(" "); - let codec = "libx264"; - let hwAcc = ""; + let codec = "libx264"; + let hwAcc = ""; - const argv = process.argv; - if (argv.includes("/nvenc_h264")) { - // Use NVenc H.264 - codec = "h264_nvenc"; - hwAcc = "-hwaccel cuda"; - } + const argv = process.argv; + if (argv.includes("/nvenc_h264")) { + // Use NVenc H.264 + codec = "h264_nvenc"; + hwAcc = "-hwaccel cuda"; + } - if (argv.includes("/nvenc_h265")) { - // Use NVenc H.265 - codec = "hevc_nvenc"; - hwAcc = "-hwaccel cuda"; - } + if (argv.includes("/nvenc_h265")) { + // Use NVenc H.265 + codec = "hevc_nvenc"; + hwAcc = "-hwaccel cuda"; + } - if (argv.includes("/h265")) { - // Use H.265 encoder - codec = "libx265"; - } + if (argv.includes("/h265")) { + // Use H.265 encoder + codec = "libx265"; + } - // Compress the video - // Add metadata to audio's track - await execute( - `"${ffmpegPath}" -y ${hwAcc} \ + // Compress the video + // Add metadata to audio's track + await execute( + `"${ffmpegPath}" -y ${hwAcc} \ -i "${file}" \ -c:v ${codec} -b:v ${videoBitrate}k -pass 1 -an -f mp4 \ ${nul} \ @@ -205,14 +209,17 @@ app.whenReady().then(() => { ${audioTracks.length === metadataAudioSize ? metadataAudio : ""} \ ${shareOpt} \ "${finalFile}"` - ).catch((e) => registerError(win, e)); + ).catch((e) => registerError(win, e)); + + // Delete the 2 pass temporary files + deleteTwoPassFiles(process.cwd()); + } else { + finalFile = ""; + } // Delete the old video file deleteFile(file); - // Delete the 2 pass temporary files - deleteTwoPassFiles(process.cwd()); - return finalFile; }; diff --git a/src/scripts/renderer.ts b/src/scripts/renderer.ts index eadeffa..f924240 100644 --- a/src/scripts/renderer.ts +++ b/src/scripts/renderer.ts @@ -103,6 +103,7 @@ const main = async () => { updateMessage("Récupération des fichiers..."); const files = await getFiles(); let processedFiles = ""; + let numberOfUncompressableFiles = 0; // Iterate over all the retrieved files for (const [idx, file] of files.entries()) { @@ -148,14 +149,26 @@ const main = async () => { } // Append title to the list of processed files - processedFiles += `\n- ${finalTitle}`; - updateMessage(`Fichier ${counter} traités.`); + if (finalTitle.length > 0) { + processedFiles += `\n- ${finalTitle}`; + updateMessage(`Fichier ${counter} traités.`); + } else { + processedFiles += `\n- ${file} [incompressable]`; + updateMessage(`Fichier ${counter} trop large pour être compressé.`); + numberOfUncompressableFiles++; + } + } + + let errorMessage = ""; + if (numberOfUncompressableFiles > 0) { + errorMessage += `\nNombre de fichier incompressable : ${numberOfUncompressableFiles}.`; } // Send confirmation to the user that we're done await internals.confirmation( - `${files.length} fichiers traités : ${processedFiles}` + `${files.length} fichiers traités : ${processedFiles}` + errorMessage ); + await internals.exit(); };