How to read metadata from mp4 using mp4.js node module? - javascript

I am trying to read the metadata tags from an mp4 file into my javascript using the node module mp4js. I followed the model provided on that page but with some issue.
I tried the following:
var mp4 = require('mp4js');
mp4({ file: 'small.mp4', type: 'local' }, function (err, tags) {
// tags now contains your MP4 tags
console.log('Tags: ' + tags)
$scope.mp4Tags = tags;
});
and I was met with Uncaught TypeError: Cannot read property 'join' of undefined
Does anyone know what I'm missing here? Could it be that the mp4 doesn't have any tags? I just googled "sample mp4" and downloaded something. It's my first time trying this module and I am pretty new to node.
I'm also open to other suggestions for reading mp4 metadata.
Thank you very much for your time and let me know if you need any additional from me.
EDIT 8.26.2015
Following Brad's suggestion, I tried the following code to read metadata from an mp4:
var child_process = require('child_process');
ls = child_process.spawn('ffmpeg/bin/ffprobe.exe', ['-print_format', 'json', 'out.mp4']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
$scope.mp4data = data;
});
I created mp4.out by following this example for adding a title meta tag using ffmpeg from the command line. Here is a screenshot of my output. Here is the command I entered:
ffmpeg -i in.avi -metadata title="my title" out.flv
After running my app with the code above, the value stored in $scope.mp4data is a Native Buffer that looks like this. All I see are some numbers, but no metadata tags. How can I view the metadata such as the title that I set using the command above? Am I missing something in the arguments? Or maybe I should be looking at something other than data?
Thanks for the help. I may just post this as a new question since the focus has shifted.
EDIT: Solution
Here's where I landed thanks to Brad's help:
$scope.mp4data = "";
ls.stdout.on('data', function (data) {
$scope.mp4data = $scope.mp4data + data.toString();
});
ls.stdout.on('end', function (data) {
$scope.mp4data = JSON.parse($scope.mp4data);
});
works great for reading metadata from my mp4! Thanks Brad!

I recommend using FFprobe (from the FFmpeg project) to do this instead. It supports many containers, including MP4.
You can use ffprobe -print_format json to have it output JSON data that you can parse inside your Node.js app.
Some examples here: https://stackoverflow.com/a/8191228/362536

Related

ITK JS readImageDICOMFileSeries() reports 'filePath is undefined' on console

I'm new to Javascript and ITK. I started by cloning the ITK-JS repository, and am working thru the examples.
My objective -- from my browser, to read all of the images in a directory (an MR series) into a single ITK image (3D volume).
I started with the 'webpack' example in the ITK-JS example -- got that working in my browser.
I then:
1. Changed the index.html to enable multiple for the <input type='file'> element. I confirmed that worked properly by writing the contents of the FileList object to the html element, and got that working with the 'readFile()'; then
2. Replaced the 'readFile(null, files[0])' with 'readImageDICOMFileSeries(null, files)'.
When I run the code with the 'readImageDICOMFileSeries()' I get the console message: "TypeError: filePath is undefined".
I have tried several variants, but always seem to get the same error. Here is my current code:
//
// read the images
// return readFile(null, files[0]) // -- this line works
return readImageDICOMFileSeries(null, files) // the causes TypeError: filePath is undefined
.then((worker)=>{
const {image}=worker;
let mFile = JSON.stringify(image) ;
alert(mFile)
console.log(image);
})
.catch(error=>{
console.error(error);
alert(error.message);
})
What am I doing wrong?
Thanks in advance for your help.
Doug
This is an example using ITK and emscripten to compile an ITK library to read most medical image formats. It works in the browser and node.js.
https://www.npmjs.com/package/med-img-reader
I hope it helps.
There may be additional resources/examples here https://github.com/InsightSoftwareConsortium/itk-js/tree/master/examples/

HTML5 read file from IPad document Directory

I have created html5 based app in Titanium for ipad. Using Titanium I have stored a file named demo.txt in
/Users/anonymous/Library/Developer/CoreSimulator/Devices/FE1CFXXX0AC-D5BD-9615-C58D80B5A9C6/data/Containers/Data/Application/2D25XXXX-4687-B28A-1EA7B7EA3013/Documents/
in same application itself. Now I need to want to access content of demo.txt in my index.html. Anyone having idea regarding this please help me out.
You can use similar technique to read a file:
var pagesDetailFile = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "demo.txt");
alert(pagesDetailFile.read()); // Read the file
pagesDetailFile = null; // remember to release the file
You should read the documentation carefully for available functions.
You will have to use the techniques described at https://appcelerator.github.io/appc-docs/platform/latest/#!/guide/Communication_Between_WebViews_and_Titanium to fire an event in the WebView HTML which you'd pick up in Titanium JS, get the file and then use evalJS or fireEvent to send it back to the WebView HTML.
I used jquery in order to read data from the simulator and it works for devices too.
var filePath = "/Users/anonymous/Library/Developer/CoreSimulator/Devices/FE1CFXXX0AC-D5BD-9615-C58D80B5A9C6/data/Containers/Data/Application/2D25XXXX-4687-B28A-1EA7B7EA3013/Documents/admin/content.txt";
$.ajax({
url: filePath,
context: document.body,
async: false,
success: function(response){
Ti.API.info("FILE CONTENT " + response);
},
error: function(data){
alert('does not exist');
}
});
filePath is actual path in which we are storing data.

Node.JS fs.WriteFile path as a string

I am trying to save a simple file to a directory other then my root directory, but it seems to error out when using a variable instead of a direct string.Has anyone tried using a variable or see if i am missing something.
Thanks
var procedures = "SomeString";
var moveTo = "C:/SavedFiles";
//////////////////
fs.writeFile(moveTo ,procedures, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});`
Am I correct that "C:/SavedFiles" is a directory? Is so, then specify a file to write to like:
var moveTo = "C:/SavedFiles/myfile.txt";
// keep the rest the same
Not sure about the slashes though, not running Windows ATM.
So it looks like my error was due to a rouge loop above my code not posted. Sorry i didn't post the rest of the code

How to write big array to .txt file using node.js?

The obvious solution is to fs.writeFile, I assume.
But the answer of this question suggests that I should be using a Stream technique.
I'm currently trying this code in order to remove a line from the text file by converting it to an array:
var index = randomIntFromInterval(1, unfinished_searches.length-1); // remove index
unfinished_searches.splice(index, 1);
fs.truncate('unfinished_searches.txt', 0, function()
{
var file = fs.createWriteStream('unfinished_searches.txt');
file.on('error', function(err) { /* error handling */ });
unfinished_searches.forEach(function(v) { file.write(v.join(', ') + '\n'); });
file.end();
})
which returns the following error:
TypeError: undefined is not a function
at join in this line:
unfinished_searches.forEach(function(v) { file.write(v.join(', ') + '\n'); });
If you want to remove line from a big text file, consider to use pipes. Just create read stream, pipe it to your "checking function", and then pipe it to write stream.
This way you don't have to load whole file into memory. In memory will be only small amount of data at once. Of course you can't write it to the same file. But after complete operation, you can rename this file and delete old one.
EDIT:
This post can be usefull for You: Parsing huge logfiles in Node.js - read in line-by-line

I tried to use browserify with 'temp' and 'fs' modules, and all I got was this lousy error

I'm trying to migrate a node project to run in the browser. The project (nevermind the reason for the moment) writes to a temporary file, using temp with code similar to this:
var temp = require('temp');
var fs = require('fs');
temp.open('helloworld.txt', function (err, info) {
if (err) {
console.log('Failed to open a temp file:', err);
return;
}
console.log('temp: ', info.path);
fs.writeFile(info.path, 'Hello World!', function (err2) {
if (err2) {
console.log('Failed to write to a temp file:', err2);
return;
}
});
});
When running the browserified code I get the following error:
Uncaught TypeError: undefined is not a function
for the following line:
fs.open(filePath, RDWR_EXCL, 0600, function(err, fd) {
which is called by temp.open.
I'm very much aware you cannot write files using plain ol' javascript in the browser, but I was wondering if browserify have some replacement for that, especially since we're talking about a temp file.
I found browserify-fs, and tried:
browserify -r fs:browserify-fs my-example.js -o bfied.js
but I get the exact same error. I tried to replace require('fs') with require('browserify-fs') but this resulted in a similar error as well (I of course installed browserify-fs module..)
Any suggested solution would be appreciated :) If this type of migration is not possible, I'd also be glad to know..
Thanks!
I stumbled across this post as I think I was trying to do pretty much the exact same thing as you. I am not sure where you got to with this, but I found that when using browserify-fs, you cannot access files outside the sandboxed state of the browser.
So when trying...
var fs = require('broserify-fs);
fs.read('hello.txt', 'utf8' ....
I got the same error as you, but when doing this...
fs.mkdir('/home', function() {
fs.writeFile('/home/hello-world.txt', 'Hello world!\n', function() {
fs.readFile('/home/hello-world.txt', 'utf-8', function(err, data) {
console.log(data);
});
});
});
Everything worked fine (using just temporary files I think).
I am super new to this so I'm not sure what the best approach is. I imagine you either want to run a Node.js server locally and issue ajax commands to it to perform read/write operations, or investigate the HTML5 file system stuff - this looks like a good article - http://www.html5rocks.com/en/tutorials/file/dndfiles/
Hope that's some help (sorry you've had to wait 2 months for an answer ;) It was serendipitous I happened to stumble across your question and my heart sank when there were no answers)
Tom

Categories

Resources