Run ffmpeg from its parent directory in node js application in its own directory

Viewed 38

I have a node js application which needs to access ffmpeg. My approach to this has been to use {exec} from the child_processes module built into Node. The problem is that {exec} always starts the cmd line from the current directory and I can't figure out how to point the directory context to where ffmpeg.exe is located so that I can access the program. Is my approach flawed? How can I access a seperate CLI application from Node?
This code returns "'ffmpeg' is not recognized as an internal or external command" because I'm obviously in Node's execution context which is not where ffmpeg is located.
I also do not want to store the node application in the directory of ffmpeg.exe because that's just lazy and impractical.

exec(`ffmpeg -i ${filepathToMedia} --vf fps=1 ${outputdirectory}/out%d.png`, (error, stdout, stderr) => {
if (error) {
    console.log(`error: ${error.message}`);
    return;
}
if (stderr) {
    console.log(`stderr: ${stderr}`);
    return;
}
if(stdout) {
    console.log(`success: ${stdout}`)
} });
1 Answers

You could do one of two things here:

  1. Use the absolute path to the ffmpeg executable so instead of just exec('ffmpeg ...') you'd do something likeexec('C:\Users\user\ffmpeg_dir\ffmpeg ...') This isn't very clean and will probably break if someone else tries to use your code.

  2. Add your ffmpeg directory to your system's PATH environment variable. If you add ffmpeg to your PATH it becomes available as ffmpeg regardless of what folder you're in, allowing the script to work as-is. This'll also make it easier for other people to run your script.

Related