FFmpeg with node.js. Transcode a file to another format - node.js

Have a bit of a problem with this, I have an .avi file in which I would like to transcode into a .flv file using FFmpeg, here is what I have so far:
var ffmpeg = require('fluent-ffmpeg');
//make sure you set the correct path to your video file
var proc = new ffmpeg({ source: 'C:/Users/Jay/Documents/movie/drop.avi', nolog: true })
//Set the path to where FFmpeg is installed
.setFfmpegPath("C:\Users\Jay\Documents\FFMPEG")
//set the size
.withSize('50%')
// set fps
.withFps(24)
// set output format to force
.toFormat('flv')
// setup event handlers
.on('end', function() {
console.log('file has been converted successfully');
})
.on('error', function(err) {
console.log('an error happened: ' + err.message);
})
// save to file <-- the new file I want -->
.saveToFile('C:/Users/Jay/Documents/movie/drop.flv');
It seems straightforward enough and I can do it through the FFmpeg command line, but I am trying to get it working within a node.js app, here is the error it is returning:
C:\Users\Jay\workspace\FFMPEGtest\test.js:17
.withSize('50%')
^
TypeError: Cannot call method 'withSize' of undefined
at Object.<anonymous> (C:\Users\Jay\workspace\FFMPEGtest\test.js:17:2)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
It throws the same error for each built in FFmpeg function (.toFormat, .withFPS etc)
If anyone has a solution to this, I'd greatly appreciate it

setFfmpegPath() doesn't return an instance of this, as seen in the source here.
Meaning you can't chain the method.
Change it to
var ffmpeg = require('fluent-ffmpeg');
//make sure you set the correct path to your video file
var proc = new ffmpeg({ source: 'C:/Users/Jay/Documents/movie/drop.avi', nolog: true })
//Set the path to where FFmpeg is installed
proc.setFfmpegPath("C:\\Users\\Jay\\Documents\\FFMPEG")
proc
//set the size
.withSize('50%')
// set fps
.withFps(24)
// set output format to force
.toFormat('flv')
// setup event handlers
.on('end', function() {
console.log('file has been converted successfully');
})
.on('error', function(err) {
console.log('an error happened: ' + err.message);
})
// save to file <-- the new file I want -->
.saveToFile('C:/Users/Jay/Documents/movie/drop.flv');

Related

Reading content of HTML File through Node js

const http=require('http');
const fs=require('fs');
http.createServer(function (req, res) {
//Open a file on the server and return its content:
fs.readFile('./index.html',null, (err, data) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(data);
return res.end();
});
}).listen(8080);
I am newbie in node js.Just started learning nodejs recently.
Getting this error while trying to Reading content of HTML File through Nodejs. I have updated
npm module but it is not working.->
throw new ERR_INVALID_ARG_TYPE(
^TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an
instance
of Buffer or Uint8Array. Received undefined
←[90m at new NodeError (node:internal/errors:371:5)←[39m
←[90m at write_ (node:_http_outgoing:742:11)←[39m
←[90m at ServerResponse.write (node:_http_outgoing:707:15)←[39m
at ReadFileContext.callback (H:\node\nodetutorial\coremodule\Readingdata.js:8:11)
←[90m at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:314:13)←[39m {
code: ←[32m'ERR_INVALID_ARG_TYPE'←[39m
}
The res.write() function cannot accept an undefined object (which is what you're passing right now). Try stringifying your response like this:
res.write(JSON.stringify(data));
Additionally, check why data is undefined
The path to the ./index.html file is probably incorrect. You can find out what directory you're currently in by executing console.log(__dirname). You can either change the path to the file (eg: ./../index.html) or move the file according to the output.

Nodejs, Electron nightmare is not defined when it's installed?

I've installed Nightmare via NPM this is my code:
var jquery = require('jquery')
var nightmare = require('nightmare')
var nightmare = Nightmare({ show: true });
$( "#test" ).addEventListener('click',() => {
nightmare
.goto('http://akhiljose.me/master/paste/')
.type('.form-control', 'Test')
.type('input[type=test]', 'nightmare_test')
.click('input[type=submit]')
.wait(7000)
.evaluate(function () {
return document.querySelector('pre').innerText;
})
.end()
.then(function (result) {
console.log(result);
})
.cat(function (error) {
console.error('Search failed:', error);
})});
However console logs:
C:\Users\ninja_000\Desktop\clu-gen\index.js:3 Uncaught ReferenceError: Nightmare is not defined
at Object.<anonymous> (C:\Users\ninja_000\Desktop\clu-gen\index.js:3:17)
at Object.<anonymous> (C:\Users\ninja_000\Desktop\clu-gen\index.js:22:3)
at Module._compile (module.js:642:30)
at Object.Module._extensions..js (module.js:653:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:504:12)
at Function.Module._load (module.js:496:3)
at Module.require (module.js:586:17)
at require (internal/module.js:11:18)
at file:///C:/Users/ninja_000/Desktop/clu-gen/index.html:12:5
I'm very new to nodejs what is causing this error? Am I do something wrong?
You are calling an undefined variable.
var jquery = require('jquery')
var nightmare = require('nightmare')
var nightmare = Nightmare({ show: true });
The second line declares a variable nightmare but the next line you are calling Nightmare. Make the second line uppercase.
var jquery = require('jquery')
var Nightmare = require('nightmare')
var nightmare = Nightmare({ show: true });
You can see from the second line of the stack trace:
at Object.<anonymous> (C:\Users\ninja_000\Desktop\clu-gen\index.js:3:17)
Line 3:17, there is an uncaught ReferenceError: Nightmare. This make sense because Nightmare is not defined, so nodejs cannot find it. The line numbers in the stack trace are useful to pinpoint where in the code the error is occurring. You can also use a linter which will show an error for trying to use an undefined variable. Something like eslint.
Should've defined as Nightmare not nightmare

nodejs. watcher returned from fs.watch() listen 'error' event does not trigger

I add 'error' event Listener to watcher returned from fs.watch().
But error event handler does not trigger when I watch a file which is not existed.
const filename = 'test/target.txt';
const filenameNotExist = 'test/not-exist-target.txt';
console.log('Now watching the target.txt for changes...');
const watcher = fs.watch(filenameNotExist || filename);
watcher.on('change', (eventType, fname) => {
switch (eventType) {
case 'rename':
console.log(`file ${fname} renamed`);
break;
case 'change':
console.log(`file ${fname} changed`);
break;
default:
break;
}
});
// error event handler does not trigger.
watcher.on('error', err => {
console.log('filename is not correct');
if (err) throw err;
});
stdout give me this output:
Now watching the target.txt for changes...
fs.js:1384
throw error;
^
Error: watch test/not-exist-target.txt ENOENT
at _errnoException (util.js:1041:11)
at FSWatcher.start (fs.js:1382:19)
at Object.fs.watch (fs.js:1408:11)
at Object.<anonymous> (/Users/elsa/workspace/Training.nodejs/examples/api/fs/watcher.js:10:20)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
There is no filename is not correct log.
which case will trigger watcher error event handler?
The fs.FSWatcher docs says that it is returned on a successful fs.watch() call. Check the returned object, maybe it is an Error (ENOENT) and not an FSWatcher you expect.
(Actually I believe an ENOENT is thrown, so your watcher.on(...) calls are never executed. You can use a try-catch to make sure.)

How to read data from console in Node.js?

Actually I have tried one code. This code is working fine but I want to access this information outside of callback function. But i am unable to find solution.
var prompt = require('prompt');
prompt.start();
prompt.get(['username', 'email'], function (err, result) {
console.log('Command-line input received:');
console.log(' username: ' + result.username);
console.log(' email: ' + result.email);
data = result.username;
});
console.log(data);
Here if i'm trying to retrieve print data variable it show's error.
Admins-MacBook-Pro:Basic node programs Sandeep$ node Node3.js
prompt: username: /Users/Sandeep/Desktop/NodeJS/Node example/Basic node programs/Node3.js:22
console.log(data);
^
ReferenceError: data is not defined
at Object.<anonymous> (/Users/Sandeep/Desktop/NodeJS/Node example/Basic node programs/Node3.js:22:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
You could use promise, check docs about promise and newer es 6 Async/ await. With promise you could use something like this:
var prompt = require('prompt');
function getPromt () { return new Promise( (resolve, recect) => {
prompt.start();
prompt.get(['username', 'email'], function (err, result) {
console.log('Command-line input received:');
console.log(' username: ' + result.username);
console.log(' email: ' + result.email);
resolve( result.username);
});
});
}
getPromt().then(data =>
console.log('After promise ' + data), ()=>{});
"You won't be able to access that variable outside the callback function. The reason is, the Node.js has a special feature of passing a callback function as the next block of code to be executed after performing an asynchronous IO task."
Refer to this one: how can find return variable value outside anonymous function in node js mysql query function
You can also check this link to learn how to handle with async flow: http://book.mixu.net/node/ch7.html

Error in sending message from an Node.js app to Slack channel

Trying to send message to slack from Node.js app
I have used following package https://github.com/xoxco/node-slack
For Detailed reference full source code is in https://github.com/sudanvellakovilkanakavel/attendence-app
I couldn't understand
what is hook_url
How to define it
Error output
CJBTDLDD0003:backend kanaks$ node readingFile.js
/Users/kanaks/Desktop/backend/readingFile.js:2
var slack = new Slack(hook_url,options);
^
ReferenceError: hook_url is not defined
at Object.<anonymous> (/Users/kanaks/Desktop/backend/readingFile.js:2:23)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
My Code
var Slack = require('node-slack');
var slack = new Slack(hook_url,options);
//var slack = new Slack(hook_url,{proxy: http_proxy});
//Trying to send message to slack from Node.js app
//I have used following package https://github.com/xoxco/node-slack
slack.send({
text: 'Meassage from node.js app to slack!',
channel: '#attendance',
username: 'Bot'
});
var fs = require("fs");
//reading the file
fs.readFile('Attendance01-04-2016.dat','utf8',function (err,data){
if (err) {
return console.error(err);
}
//spliting each line
var lines = data.split("\n");
//storeing in array and printing the file
for (i=0;i<=lines.length;)
{
console.log(lines[i]);
i=i+1;
}
});

Resources