From 7eafb0b27c679cb7e607dbc7a27aefc70fb4f943 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 1 Aug 2023 15:57:01 +0200 Subject: [PATCH] delete twopass files created by ffmpeg if needed --- src/main.ts | 23 ++++++++++------------- src/utils/misc.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/main.ts b/src/main.ts index e342811..314f116 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,8 @@ import { BrowserWindow, app, dialog, ipcMain } from "electron"; -import { statSync, unlink } from "fs"; +import { statSync } from "fs"; import { + deleteFile, + deleteTwoPassFiles, execute, getNewFilename, getVideoDuration, @@ -63,12 +65,8 @@ app.whenReady().then(() => { `"${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; - } - }); + // Delete the temporary video file + deleteFile(tmpFile); const duration = getVideoDuration(outFile); const stats = statSync(outFile); @@ -91,12 +89,11 @@ app.whenReady().then(() => { "${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; - } - }); + // Delete the old video file + deleteFile(file); + + // Delete the 2 pass temporary files + deleteTwoPassFiles(file); return finalFile; }; diff --git a/src/utils/misc.ts b/src/utils/misc.ts index 1d68c78..8f098bd 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -2,6 +2,7 @@ import ffprobe = require("ffprobe-static"); import child_process = require("child_process"); import path = require("path"); import { BrowserWindow } from "electron"; +import { existsSync, unlink } from "fs"; /** Create a new filename from the OG one */ export const getNewFilename = (ogFile: string, part: string) => { @@ -34,3 +35,29 @@ export const execute = (command: string) => { }); }); }; + +/** Delete a file */ +export const deleteFile = (file: string) => { + unlink(file, (err) => { + if (err) { + throw err; + } + }); +}; + +/** Delete the 2pass files generated by ffmpeg, we're using + * the file location to know in which directory they are. + * We're checking first if they exists where we are looking at. */ +export const deleteTwoPassFiles = (file: string) => { + const directory = path.parse(file).dir; + const logFile = path.join(directory, "ffmpeg2pass-0.log"); + + if (existsSync(logFile)) { + deleteFile(logFile); + } + + const mbtreeFile = path.join(directory, "ffmpeg2pass-0.log.mbtree"); + if (existsSync(mbtreeFile)) { + deleteFile(mbtreeFile); + } +};