I want to extract the original name and date-taken from m2ts files in Node.
I found some dead code (m2ts-parser) using the package "packet". Though I'm too novice to really understand. But I want to learn :-)
So can anyone tell me how to extract the original filename and original creation date of the m2ts file (in node, but any pseudo code will do :-)
Thanks
update
I found - based on the answer of #aergistal a partial solution.
I am able to extract the Date/Time original from an m2ts file. Tested only on a Canon and Panasonic file:
var fs = require('fs');
fs.open('C:/temp/28685338476.mts', 'r', function (status, fd) {
if (status) {
console.log(status.message);
return;
}
var chunk = new Buffer(10000);
var syncFound = false;
var i, l;
fs.read(fd, chunk, 0, 10000, 0, function (err, num) {
for (i = 0, l = chunk.length; i < l; i++) {
if (chunk[i] === 0x47) {
syncFound = true;
}
if (syncFound) {
if (chunk[i] === 0x66 && chunk[i + 1] === 0x4D && chunk[i + 2] === 0x44 && chunk[i + 3] === 0x50 && chunk[i + 4] === 0x4D) {
console.log("GOT 'MDPM' at byte ", i);
var yy = chunk[i + 8].toString(16) + chunk[i + 9].toString(16);
var MM = chunk[i + 10].toString(16);
var dd = chunk[i + 12].toString(16);
var hh = chunk[i + 13].toString(16);
var mm = chunk[i + 14].toString(16);
var ss = chunk[i + 15].toString(16);
var timeStamp = yy + "/" + MM + "/" + dd + " " + hh + ":" + mm + ":" + ss;
console.log("TimeStamp: "+ timeStamp);
return timeStamp;
}
}
}
});
});
There is no such information in the MPEG-TS container.
This kind of metadata might be included in one of the elementary streams in which case its location will be different for every stream type. To make things worse there's no common standard even for the same type.
For example if your m2ts file contains a H.264 video stream such metadata might be found within the Supplemental Enhancement Information (SEI) if provided. The structure is described in the H.264 standard.
Once you know where the data is you will need to find out what format it uses. One example is the Modified Digital Video Pack Metadata (MDPM) which is supported by the exiftool and might contain a DateTimeOriginal tag. Example.
Of course, the metadata might not be present at all or stored in an auxiliary file.
Related
I am trying to find the oldest folder in a directory of my nodejs app.
Right now I get the folder names by fs.readdirSync and the I try to run through the mtime in a for-loop with the fs.stat function. But it does not return any values.
cron.schedule('* * * * *', () => {
folders = fs.readdirSync(__dirname + '/uploads/');
var oldestTime;
var oldest;
for (var i = 0; i < folders.length; i++) {
let stats = fs.statSync(folders[i]);
if (oldestTime == undefined || stats.mtime < oldestTime) {
oldestTime = stats.mtime;
oldest = folders[i];
}
}
console.log("oldest folder name is:", oldest)
}
Is there a better way?
Thank you so much!
You use the asynchronous version of fs.stat(). Try the synchronous (callback-free) fs.statSync() instead.
And don't forget that readdir returns results named . for the current directory and .. for the parent directory. You may want to exclude those from your mtime search.
And please spend some time reading up on the Javascript asynchronous programming model.
folders = fs.readdirSync(__dirname + "/uploads");
var oldestTime;
var oldest;
for (var i = 0; i < folders.length; i++) {
let stats = fs.statSync(__dirname + "/uploads/" + folders[i]);
if (oldestTime == undefined || stats.mtime < oldestTime) {
oldestTime = stats.mtime;
oldest = folders[i];
}
}
console.log("oldest folder name is:", oldest)
or use sort to get the oldest one:
const fs = require('fs');
const cron = require("cron");
var job = new cron.CronJob('* * * * * *', function () {
const folders = fs.readdirSync(__dirname + "/uploads");
var oldest = folders.sort((a, b) => fs.statSync(__dirname + '/uploads/' + a).mtime - fs.statSync(__dirname + '/uploads/' + b).mtime)[0];
console.log("oldest folder name is:", oldest)
});
job.start();
I have writtten code like below to log express logs.
const rfs = require("rotating-file-stream");
function formatDate() {
var d = new Date(),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
hour = d.getHours();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
if (hour.length < 2)
hour = '0' + hour
return [year, month, day, hour].join('-');
}
let log_directory = '/../logs/';
let log_date = formatDate()
let log_file_name = path.normalize(__dirname + log_directory + log_date, 'access.log');
let accessLogStream = rfs.createStream(log_file_name, {
size: "300M",
interval: "1d",
})
app.use(morgan(':date[iso] :method :url :body :headers :remote-addr :req[content-length] - :status :response-time ms - :res[content-length] ', { stream: accessLogStream }));
logs are getting stored out side of the project as expected.
But after rotation, the logs are storing inside the project with the below folder names
20200507-0943-01-
20200508-0943-01-
20200509-0943-01-
20200510-0943-01-
20200511-0943-01-
20200512-0943-01-
20200513-0944-01-
package.json
package-lock.json
public
routes
scripts
services
app.js
I don't want these folders created inside the project. Is there any way to handle this?
First of all you are using a static file name as filename parameter of createStream function and you are doing some computation on date to have your resulting file name.
Please note that the computation on date happens only once, before calling the createStream function. In order to do computation on date happening at each rotation you should use a filename generator function. Details can be found in readme.
More than this it seems there is a bug in this line: path.normalize accepts only one parameter.
let log_file_name = path.normalize(__dirname + log_directory + log_date, 'access.log');
Last, to specify a directory for logging, path option should be enough.
If I argued correctly your intentions, following code should solve all the problems
const rfs = require("rotating-file-stream");
function formatDate(d) {
var month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear(),
hour = d.getHours();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
if (hour.length < 2)
hour = '0' + hour;
return [year, month, day, hour].join('-');
}
function log_file_name(time, index) {
if (!time) return 'access.log';
return [formatDate(time), index, 'access.log'].join('-');
}
let accessLogStream = rfs.createStream(log_file_name, {
size: "300M",
interval: "1d",
path: '/../logs/'
});
I'm using Angular v4, i guess how can I build an Excel spreadsheet starting from an object in a component. I need to download the Excel file on the click of a button and I have to do this client side. I have a json file composed of arrays and I need to transfer this on an excel file, possibly customizable in style. Is it possible? If yes, how?
Edit:
No js libraries please, need to do this with Typescript and Angular
yourdata= jsonData
ConvertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var row = "";
for (var index in objArray[0]) {
//Now convert each value to string and comma-separated
row += index + ',';
}
row = row.slice(0, -1);
//append Label row with line break
str += row + '\r\n';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
in your html:
<button (click)="download()">export to excel</button>
in component:
download(){
var csvData = this.ConvertToCSV(yourdata);
var a = document.createElement("a");
a.setAttribute('style', 'display:none;');
document.body.appendChild(a);
var blob = new Blob([csvData], { type: 'text/csv' });
var url= window.URL.createObjectURL(blob);
a.href = url;
a.download = 'User_Results.csv';/* your file name*/
a.click();
return 'success';
}
Hope you it will help you
Vishwanath answer was working for me when i replaced "," with ";". In Typescript the implementation could look like this:
ConvertToCSV(objArray: any) {
const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray;
let str = '';
let row = '';
for (const index of Object.keys(objArray[0])) {
row += `${index};`;
}
row = row.slice(0, -1);
str += `${row}\r\n`;
for (let i = 0; i < array.length; i++) {
let line = '';
for (const index of Object.keys(array[i])) {
if (line !== '') {
line += ';';
}
line += array[i][index];
}
str += `${line}\r\n`;
}
return str;
}
I hope this helps someone.
I think you will not get that done without js libraries in the background. What you need are the typings for utilizing it in your Angular project with typescript.
For creating an excel file you could use something like exceljs. To use it in your project also install the typings you can find here. I am not sure if this library fits... haven't used it before.
For downloading you should use FileSaver.js (I have already used it).
npm install file-saver --save
... and the typings:
npm install #types/file-saver --save-dev
For using FileSaver.js put the following import to your component:
import * as FileSaver from 'file-saver';
To trigger the download use that:
FileSaver.saveAs(fileData, "filename.xlsx")
'fileData' has to be a Blob.
Hope this helps a little.
it not is possible.
XLS is a binary format.
The project Apache POI(https://en.wikipedia.org/wiki/Apache_POI) name class as HSSF (Horrible SpreadSheet Format).
my recommendation, make it in server side.
I am taking snapshot in html5 using the following code.
window.takeSnapshot = function(){
$("body").append("<canvas id='dummyCanvas' class='canvas' style='display: none'></canvas>");
var canvas = document.getElementById("dummyCanvas");
canvas.width = videoWidth;
canvas.height = videoHeight;
var context = canvas.getContext("2d");
var type = mediaType;
var tp = tupple;
context.drawImage(videoElement, 0, 0, videoWidth, videoHeight);
var contents = canvas.toDataURL('image/png');
var dt = new Date();
Message.showProgress();
var time = dt.getHours() + "_" + dt.getMinutes() + "_" + dt.getSeconds();
var file = {name: "Snapshot_" + time + ".png", contents: contents, recorded: true};
var id = "attachment_" + window.Guid();
var icon = (type==ContentTypes.Video)?("video.png"):((type==ContentTypes.Audio)?"audio.png":"image.png");
$("#attachments").append("<tr id='"+id+"'><td align='right'><img src='assets/images/progress.gif' style='width:16px'/></td><td><img src='assets/images/" + icon + "' style='width: 14px;' /></td><td style='font-size: 8pt; font-family: Arial; font-weight: bold;' style='text-decoration:none; color: #000000'>"+file.name+"</td></tr>");
Logger.log("Uploading " + file.name + " ...", LogTypes.INFO);
$("#mediaPanel").remove();
$("#attachmentPopup").show();
window.stream.stop();
var callback = function(){
Logger.log("Upload completed for " + file.name + " !", LogTypes.INFO);
CWDocumentWriter.addAttachment(tp, file.name, type);
$("#"+id).find('td').eq(0).html("<a href='javascript:void(0)' title='Delete attachment' onclick='window.deleteAttachment(["+tp[0]+","+tp[1]+","+tp[2]+","+tp[3]+"],\""+file.name+"\", event)'><img src='assets/images/delete.png' style='width:16px'/></a>");
$("#"+id).find('td').eq(2).html("<a href='javascript:void(0)' title='"+file.name+"' onclick='window.showContent(\"" + file.name + "\", " + type + ")'>"+file.name+"</a>");
Message.hideProgress();
};
if(Cloud.isLive())
Cloud.writeFile(file, contents, callback);
else{
var canvasImage = canvas.toDataURL("image/png").split(',')[1];
var decodedImg = window.atob(canvasImage);
var img = new Buffer(decodedImg, encoding='base64');
file.contents = img;
StorageManager.writeImageFile(file, contents, callback);
}
};
The image is uploaded to the cloud or saved on the local storage (using nodejs functions) depending upon the live or dead internet. It works fine in uploading the image data to the cloud and I can save and see the image back. But in the case of local hard drive it is not working. The image seems to be corrupted. Here is how I am saving it.
StorageManager.writeImageFile = function(file, data, callback){
if(!UserManager.isLoggedIn()){
UserManager.login();
return;
}
var key = ProjectManager.projectName;
var dir = Settings.USER_FOLDER + "/" + key + "/" + "media";
data = window.encode64(file.contents);
fs.writeFile(dir + "/" + file.name, data, "base64", function(err){});
callback();
};
I have tried several approaches but it seems to be not working correctly. The image file is written in the hard drive but it is corrupted. Please help me to resolve this problem.
I believe this will solve your problem:
var imgData = canvas.toDataURL('image/png');
var data = imgData.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');
var whereToSave = "C:\pathToSave"; // edit this
fs.writeFile(whereToSave, buf);
I am trying to get the stdout of 7zip when it processes files and get the percentage in nodeJs, but it doesn't behave as expected. 7zip doesn't output anything to stdout until the very end of the execution. Which is not very helpful.. especially when I have large files being compressed and no feedback is shown for a very long time.
The code I am using (simplified):
// 7zip test, place the 7z.exe in the same dir, if it's not on %PATH%
var cp = require('child_process');
var inputFile = process.argv[2]; if(inputFile==null) return;
var regProgress = /(\d{1,3})%\s*$/; //get the last percentage of the string, 3 digits
var proc = cp.spawn("7z.exe",["a","-t7z" ,"-y" ,inputFile + ".7z",inputFile]);
proc.stdout.setEncoding("utf8");
proc.stdout.on("data",function(data){
if(regProgress.test(data))
console.log("Progress = " + regProgress.exec(data)[1] + "%");
});
proc.once("exit",function(exit,sig){ console.log("Complete"); });
I have used the same code to get the percentage with WinRar successfully and I am beginning to think that 7zip might be buggy? Or I am doing it wrong? Can I forcefully read the stdout of a process with a timer perhaps?
The same code above, with the exception of the following line replaced, works as expected with WinRar.
var proc = cp.spawn("Rar.exe",["a","-s","-ma5","-o+",inputFile+".rar",inputFile]);
If anyone knows why this happens and if it is fixable, I would be grateful! :-)
p.s. I have tried 7za.exe, the command line version of 7zip, also the stable, beta and alpha versions, they all have the same issue
It is no longer needed to use a terminal emulator like pty.js, you can pass the -bsp1 to 7z to force to output the progress to stdout.
7-zip only outputs progress when stdout is a terminal.
To trick 7-zip, you need to npm install pty.js (requires Visual Studio or VS Express with Windows SDK) and then use code like:
var pty = require('pty');
var inputFile = process.argv[2],
pathTo7zip = 'c:\\Program Files\\7-Zip\\7z.exe';
if (inputFile == null)
return;
var term = pty.spawn(process.env.ComSpec, [], {
name: 'ansi',
cols: 200,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
var rePrg = /(\d{1,3})%\r\n?/g,
reEsc = /\u001b\[\w{2}/g,
reCwd = new RegExp('^' + process.cwd().replace(/\\/g, '\\\\'), 'm');
prompts = 0,
buffer = '';
term.on('data', function(data) {
var m, idx;
buffer += data;
// remove terminal escape sequences
buffer = buffer.replace(reEsc, '');
// check for multiple progress indicators in the current buffer
while (m = rePrg.exec(buffer)) {
idx = m.index + m[0].length;
console.log(m[1] + ' percent done!');
}
// check for the cmd.exe prompt
if (m = reCwd.exec(buffer)) {
if (++prompts === 2) {
// command is done
return term.kill();
} else {
// first prompt is before we started the actual 7-zip process
if (idx === undefined) {
// we didn't see a progress indicator, so make sure to truncate the
// prompt from our buffer so that we don't accidentally detect the same
// prompt twice
buffer = buffer.substring(m.index + m[0].length);
return;
}
}
}
// truncate the part of our buffer that we're done processing
if (idx !== undefined)
buffer = buffer.substring(idx);
});
term.write('"'
+ pathTo7zip
+ '" a -t7z -y "'
+ inputFile
+ '.7z" "'
+ inputFile
+ '"\r');
It should be noted that 7-zip does not always output 100% at finish. If the file compresses quickly, you may just see only a single 57% for example, so you will have to handle that however you want.