Skip to content

Commit

Permalink
Refactor FFmpeg checker for improved reliability (#13)
Browse files Browse the repository at this point in the history
These changes aim to improve the robustness and reliability of the FFmpeg binary checking process. By validating the `FFMPEG_PATH` environment variable and using `child_process.spawn` for the check, we enhance the overall stability and accuracy of the FFmpeg verification process, especially across different operating systems.

Signed-off-by: Ryuu Mitsuki <[email protected]>
  • Loading branch information
mitsuki31 committed Jul 21, 2024
2 parents 9e6a769 + bf42954 commit e09386e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/audioconv.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const childProcess = require('node:child_process');
const ffmpeg = require('fluent-ffmpeg');

// Promisify the `exec` function
const exec = promisify(childProcess.exec);
const spawn = promisify(childProcess.exec);

const { logger: log } = require('./utils');

Expand Down Expand Up @@ -150,15 +150,22 @@ function resolveOptions(options, useDefault=true) {
async function checkFfmpeg(verbose=false) {
verbose && log.debug('Checking `ffmpeg` binary...');
if (process.env.FFMPEG_PATH) {
if ((await fs.promises.stat(process.env.FFMPEG_PATH)).isDirectory()) {
const msg = '[EISDIR] Please set the FFMPEG_PATH environment variable '
+ 'to the path of the `ffmpeg` binary';
verbose && log.warn(msg);
throw new Error(msg);
}

verbose && log.debug('`ffmpeg` installed on system');
return true;
}

// This try-catch block to handle error,
// in case the `ffmpeg` binary is not installed on system
try {
const { stdout, stderr } = await exec('ffmpeg -version');
if (!stderr && stdout) {
const { status } = await spawn('ffmpeg -version', { shell: true });
if (status !== 0) {
verbose && log.debug('`ffmpeg` installed on system');
return true;
}
Expand Down

0 comments on commit e09386e

Please sign in to comment.