delete twopass files created by ffmpeg if needed

This commit is contained in:
Mylloon 2023-08-01 15:57:01 +02:00
parent 9119114c8d
commit 7eafb0b27c
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 37 additions and 13 deletions

View file

@ -1,6 +1,8 @@
import { BrowserWindow, app, dialog, ipcMain } from "electron"; import { BrowserWindow, app, dialog, ipcMain } from "electron";
import { statSync, unlink } from "fs"; import { statSync } from "fs";
import { import {
deleteFile,
deleteTwoPassFiles,
execute, execute,
getNewFilename, getNewFilename,
getVideoDuration, getVideoDuration,
@ -63,12 +65,8 @@ app.whenReady().then(() => {
`"${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)); ).catch((e) => printAndDevTool(win, e));
// Delete the temporary file // Delete the temporary video file
unlink(tmpFile, (err) => { deleteFile(tmpFile);
if (err) {
throw err;
}
});
const duration = getVideoDuration(outFile); const duration = getVideoDuration(outFile);
const stats = statSync(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}"` "${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)); ).catch((e) => printAndDevTool(win, e));
// Delete the old file // Delete the old video file
unlink(file, (err) => { deleteFile(file);
if (err) {
throw err; // Delete the 2 pass temporary files
} deleteTwoPassFiles(file);
});
return finalFile; return finalFile;
}; };

View file

@ -2,6 +2,7 @@ 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"; import { BrowserWindow } from "electron";
import { existsSync, unlink } from "fs";
/** 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) => {
@ -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);
}
};