How do i get any file signature (or call it file header) in NodeJS - node.js

I am working on a module which uploads images, pdfs, audio and video files. I mainly covered all the validations in it but i am stuck at one point where i cannot detect the header of the video/audio file. It is when i change the type of any file, like changed the .JS to mp3 or mp4. Is there any packages in nodeJS which can detect the file signature or header?
Thanks in advance.

If you can not/do not, want to check by the filename extension you will have to parse the file using an appropriate media inspector.
To list a few:
file-type
mux.js

Related

How to write a webm (or other) audio/video block of data from MediaRecorder to a properly formatted .webm (or other) container file?

I am using javascript to capture audio data from MediaRecorder, and base64 encode it so I can send it back to the web server where it can be saved for later playback.
data:audio/webm;codecs=opus;base64,GkXfo59ChoEBQveBA...(too much data to post, but you get the idea)
I can put that data into an HTML5 audio element's .src field, and play it back on a Chrome browser just fine. But Safari can't handle the data in that format, I guess it doesn't support the opus codec.
One solution for me would be to figure out how to write the audio data into a properly formatted .webm container file, and then use ffmpeg.exe to convert it to some other Safari friendly format.
But I don't know the file format for .webm file - I'm looking for tips or guidance how to write such a .webm file.
Anybody have any suggestions, libraries, or tips to write data like above to a .webm file? I prefer a C# .net answer, but javascript will also do, or any examples are appreciated.
Well, I got a tip from smart developer (earnabler) that if I stripped off the header portion of the content:
"data:audio/webm;codecs=opus;base64,"
and decoded just the base64 portion:
"GkXfo59ChoEBQveBA...(too much data to post, but you get the idea)"
...back to binary (example in C#):
byte[] decodedBinaryData;
decodedBinaryData = Convert.FromBase64String(encodedBase64String);
...and wrote that binary to a file with a matching file extension (.webm in this example), that the file would be a properly formatted file of that type understandable by other media software.
Lo and behold, it was! I could play the file in MediaPlayer, or QuickTime, or whatever, and could use FFMPEG to convert it to other types.
So that gives me a pathway to save/use/convert the media in many ways. Problem solved.

Easily differentiate video files from image files in Node

I'm building a project where people can upload files, I would like to then display those files in a browser where people can interact with them (vote, comment etc)
However, this means I need to programatically build the html depending on the format of the video or image. Is there a way to feed a file (or filename) into a library, and determine whether I need to display it in a video element or an image element? Even a list of video formats vs image formats would help but I haven't seen anything in regards to that.
No module can reliably determine the file type. The user could either change the extension or even the magic number of the file to obfuscate it. The only reliable way it to try to pass file to some image / video transcoder to let it decide or error out if the format is invalid. This way you know you are working with known formats since all files are transcoded to your specific extensions. That could be mp4 or png. I recommend using handbrake for videos and sharp for images. Leaving the NPM links down below:
https://www.npmjs.com/package/handbrake-js
https://www.npmjs.com/package/sharp

Azure Logic Apps: Check for file type

I setup an Azure Logic App that checks for newly created files in a OneDrive folder and then sends these (images) to the MS Vision API for tagging. This flow works fine.
How can I setup a condition to only react on a specific file type (images) or even better only when the file has a certain file ending, like ".jpg", ".png" etc.?
I tried to setup a condition on the "File content type" but couldn't figure out the appropriate value for the condition ("image" doesn't work).
I couldn't find any hints on the webs and neither on SO. Any help is very much appreciated.
When reading file attachments using the GMail action, I had to use starts with because the Content-Type property contained the MIME type followed by the file name.
The following example is for checking if the file is an Excel file (.xlsx, not .xls):
I also used http://mime.ritey.com/ to upload my files and ensure I had the MIME type correct.
File name is part of the metadata provided by the OneDrive Connector.
Using that, you can apply conditions/filters based on the extension. File content type is probably pretty reliable but in practice, the extension might be better.
I think I found a solution. I was able to kind of reverse engineer the file types by setting up an app that is triggered by new files and writes the file content type to a text file in a different folder.
image/jpg and image/png are image files
application/x-zip-compressed is a zipped file
So it seems that Azure uses standard MIME types to identify the file type (which very much makes sense... :0)

TCPDF - generating a PDF within browser but also playing an audio

I'm writing in raw PHP on Linux using the tcpdf library.
The core question is that while creating a PDF with tcpdf I'd also like to kick off an audio file for the user...but tcpdf considers that TCPDF ERROR: Some data has already been output, can't send PDF file so I'm looking for a way to play an audio file at PDF generation without tcpdf considering that 'interfering output'.
I can successfully generate my wanted PDF within my browser using the tcpdf library and calls. But I would also like to play an audio when this happens - that might say "This is your generated PDF. You can print it now...etc"
So I'm wanting to put the HTML audio syntax within my php script.
However if I try to include an audio clause before the tcpdf->output, tcpdf will play the audio but say that output has happened before the tcpdf output and will not generate the PDF image. Flow stops after the tcpdf->output command - it successfully generates the PDF but it does not return for additional lines so I can't place the audio line after the output call.
So I guess I'm looking for a "hidden" audio although I've tried hidden attributes within the HTML audio syntax but still tcpdf flags it as "output has happened". Thank you.
Well, obviously, you can do that simply by
generate the PDF into file (not to the client directly -- 'F' switch).
output your audio file to the client and let it platy
output a link/Javascript redirection to the generated PDF file.
Because you didn't provide any code in your question I do not provide any either, but I believe it is already understandable.

Need a way to write headers on a wav file generated by sox

I'm using sox to convert some mp3 files to wav for a project. The problem is that the software that plays the files does not have the media name for the element it is playing. I can't seem to find a win32 cli tool to read the header of the wav file and write what I need to it.
Sox will read the header but it's not showing the title of the media element that was inserted with the old software I used. I just couldnt automate it or I would have used it instead.
I have determined the info is written in either CART CHUNK section or just to the file headers. I can't figure out to write my own data there.
The way I was able to do this was with this project: https://github.com/JamesHeinrich/getID3

Resources