NODE.JS Error: Cannot find ffmpeg while using module Audioconcat - javascript

Want to concatenate two audio files. i used an npm known as audioconcate but when i installed and configured the below code i am confronted with the following error
Error: Error: Cannot find ffmpeg
at E:\VoiceMan\registercheck\node_modules\fluent-ffmpeg\lib\processor.js:136:22
at E:\VoiceMan\registercheck\node_modules\fluent-ffmpeg\lib\capabilities.js:123:9
at E:\VoiceMan\registercheck\node_modules\async\dist\async.js:356:16
at nextTask (E:\VoiceMan\registercheck\node_modules\async\dist\async.js:5057:29)
at E:\VoiceMan\registercheck\node_modules\async\dist\async.js:5064:13
at apply (E:\VoiceMan\registercheck\node_modules\async\dist\async.js:21:25)
at E:\VoiceMan\registercheck\node_modules\async\dist\async.js:56:12
at E:\VoiceMan\registercheck\node_modules\async\dist\async.js:840:16
at E:\VoiceMan\registercheck\node_modules\fluent-ffmpeg\lib\capabilities.js:116:11
at E:\VoiceMan\registercheck\node_modules\fluent-ffmpeg\lib\utils.js:223:16
ffmpeg stderr: undefined
here is the code i am using: (all audio files are in the same folder also)
var audioconcat = require('audioconcat')
var songs = [
'a(1).mp3',
'a(2).mp3',
'a(3).mp3'
]
audioconcat(songs)
.concat('all.mp3')
.on('start', function (command) {
console.log('ffmpeg process started:', command)
})
.on('error', function (err, stdout, stderr) {
console.error('Error:', err)
console.error('ffmpeg stderr:', stderr)
})
.on('end', function (output) {
console.error('Audio created in:', output)
})

Does not look like you have ffmpeg installed, it is a requirement for that package.
Requirements
ffmpeg with additional compilation flags --enable-libass --enable-libmp3lame
https://www.npmjs.com/package/audioconcat

Related

How to run a jar file in javascript and have it return a json

I have a jar file that returns a json. How can I run it and take the output through javascript?
There is a no browser side java compiler i guess, so you must use javascript on the backend side, i suggest use nodejs
const { exec } = require('child_process');
exec('java -jar /file/to/path.jar',(error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});

'sed' Command doesn't work with java script

I want to exectue following Shell Command with the Execute Option form Javascript:
"sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf"
Then i try this command in the shell console it works perfektly. But after starting it with the .js programm nothing is happen. I just want to copy the first 5 lines in template and add them to the config file.
You can do with exec from child_process
const exec = require('child_process').exec
exec("sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf", (err, stdout, stderr) => {
if (err) {
return console.error(err)
}
console.log(`stdout: ${stdout}`)
console.log(`stderr: ${stderr}`)
})
You can use the below code to execute any shell command and replace the o/p to a new file(create a new file) everytime you run it. And, you should use the line-reader module to get the line from the files.
npm install --save line-reader
const fs = require('fs');
const lineReader = require('line-reader');
const { exec } = require('child_process');
const cmd = "sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf"
exec(cmd, (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
fs.writeFile('file path', stdout, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('file saved');
readFile1();
});
}
});
const readFile1 = () => lineReader.eachLine('file path', function(line) {
//make logic to take number of lines you want to take.
console.log(line);
});

How to use node command to run multiple .js files one after the other?

I'm currently using the node command to run a few premade scripts. Right now this is what I type into Git:
node file1.js
node file2.js
node file3.js
I have to wait for each one to finish before typing the next "node file.js"
Is there a way to do that for all of the files in the folder as opposed to typing them out one after the other? Thanks!
You can use fs.readdir to first get all files in current dir
const fs = require('fs')
const files = fs.readdirSync('.')
Then filter out the .js files:
const jsFiles = files.filter(f => f.endsWith('.js'))
The execute them one by one using child_process:
const { spawnSync } = require('child_process')
for (const file of jsFiles) {
spawnSync('node', [file], { shell: true, stdio: 'inherit' })
}
I'm using spawnSync so that it'll execute the files synchronously (one-by-one).
You can use exec() function to run commands through Javascript. Insert this line of code at the top of file1.js and call exec() function at the end of the execution and so on.
Example
index.js
const { exec } = require("child_process");
exec("dir", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
The output of node index.js:
stdout: Volume in drive C is Windows 10
Volume Serial Number is 3457-05DE
Directory of C:\dev\node
10.08.2020 20:52 <DIR> .
10.08.2020 20:52 <DIR> ..
10.08.2020 20:52 <DIR> .vscode
10.08.2020 21:39 310 index.js
10.08.2020 21:15 239 package.json
2 File(s) 549 bytes

LSOpenURLsWithRole() failed for the application /location/name.app with error -10810

I'm trying to open application using nodejs (I'd like this process to be workable in cross-platform , MAC/Windows ..etc
here's the code
const { exec } = require('child_process')
const fs = require('fs')
// giving the required permissions , not really sure if 777 is the right choice
fs.chmodSync(appPath,777);
exec("open -a " + path , (err, stdout, stderr) => {
console.log(err);
console.log(stdout);
console.log(stderr);
});
console.log(err); // logs this error
{ Error: Command failed: open -a /location.app
LSOpenURLsWithRole() failed for the application /location.app with error -10810
when executing the same command in terminal (out of node) , it fires the same issue

Running a command with gulp to start Node.js server

So I am using gulp-exec (https://www.npmjs.com/package/gulp-exec) which after reading some of the documentation it mentions that if I want to just run a command I shouldn't use the plugin and make use of the code i've tried using below.
var exec = require('child_process').exec;
gulp.task('server', function (cb) {
exec('start server', function (err, stdout, stderr) {
.pipe(stdin(['node lib/app.js', 'mongod --dbpath ./data']))
console.log(stdout);
console.log(stderr);
cb(err);
});
})
I'm trying to get gulp to start my Node.js server and MongoDB. This is what i'm trying to accomplish. In my terminal window, its complaining about my
.pipe
However, I'm new to gulp and I thought that is how you pass through commands/tasks. Any help is appreciated, thank you.
gulp.task('server', function (cb) {
exec('node lib/app.js', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
exec('mongod --dbpath ./data', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
})
For future reference and if anyone else comes across this problem.
The above code fixed my problem. So basically, I found out that the above is its own function and therefore, doesn't need to:
.pipe
I thought that this code:
exec('start server', function (err, stdout, stderr) {
was the name of the task I am running however, it is actually what command I will be running. Therefore, I changed this to point to app.js which runs my server and did the same to point to my MongoDB.
EDIT
As #N1mr0d mentioned below with having no server output a better method to run your server would be to use nodemon. You can simply run nodemon server.js like you would run node server.js.
The below code snippet is what I use in my gulp task to run my server now using nodemon :
// start our server and listen for changes
gulp.task('server', function() {
// configure nodemon
nodemon({
// the script to run the app
script: 'server.js',
// this listens to changes in any of these files/routes and restarts the application
watch: ["server.js", "app.js", "routes/", 'public/*', 'public/*/**'],
ext: 'js'
// Below i'm using es6 arrow functions but you can remove the arrow and have it a normal .on('restart', function() { // then place your stuff in here }
}).on('restart', () => {
gulp.src('server.js')
// I've added notify, which displays a message on restart. Was more for me to test so you can remove this
.pipe(notify('Running the start tasks and stuff'));
});
});
Link to install Nodemon : https://www.npmjs.com/package/gulp-nodemon
This solution has stdout/stderr shown as they occur and does not use 3rd party libs:
var spawn = require('child_process').spawn;
gulp.task('serve', function() {
spawn('node', ['lib/app.js'], { stdio: 'inherit' });
});
You can also create gulp node server task runner like this:
gulp.task('server', (cb) => {
exec('node server.js', err => err);
});
If you want your console to output everything that the child process outputs, as well as pass to the child process all environment variables you already have set:
const exec = require('child_process').exec;
function runCommand(command, cb) {
const child = exec(command, { env: process.env }, function (err) {
cb(err);
})
child.stdout.on('data', (data) => {
process.stdout.write(data);
});
child.stderr.on('data', (data) => {
process.stdout.write(`Error: [${data}]`);
});
}
Note that both out and err write to stdout, this is intentional for my case but you can adapt to whatever you need.

Categories