jquery - Get content type of 'File' object using javascript -
i have 2 files same extensions follows:
1) test_audio_file.mp4 (consist of audio content)
2) test_video_file.mp4 (consist of audio , video content)
after uploading file, creating file
object of uploaded file.
i want check content type of file
object. i.e. audio/mp4
first file , video/mp4
second file.
when print file type using file_object.type
method, getting video/mp4
in both cases.
my assumption audio/mp4
first file , video/mp4
second file.
i putting line of code here:
loadfile: function(file) { console.log(file.type); };
is there method or way content type audio
first file , video
second file.
any ideas great. thanks!
the browser assume mime-type file extension in both cases here mp4.
to sure, can check binary content of file.
example
assuming have loaded file in arraybuffer
first create flexible view (uint32array cannot used length of buffer must 4-bytes aligned not case file, , dataview big-endian little-endian swapping you):
var view = new dataview(buffer); // buffer = arraybuffer
(update: removed "unneeded" first check/used box size recommend use in case. more details added.)
then, check atom (the mp4 file-format operates "atoms" , "boxes" instead of "chunks" in many other similar formats) "ftyp
" (0x66747970) in bytes 4-8 (big-endian):
if (view.getuint32(4) === 0x66747970) { // = "ftyp" // ok, far good.. }
now check what type of mp4 is:
if (view.getuint32(8) === 0x64617368) { // = "dash" // audio } else if (view.getuint32(8) === 0x6d703432) { // = "mp42" // audio + video }
we can create object-url proper mime-type set, audio:
var blob = new blob([buffer], {type: "audio/mp4"}); var url = url.createobjecturl(blob); // src video/audio element
note there many other types need consider (use hex editor inspect actual values of files expect) , want use arrays indexof()
check multiple possible values:
var videotypes = [0x6d703432, 0x69736f6d, ...]; // mp42, isom, ... ... var type = view.getuint32(8); if (videotypes.indexof(type) > -1) { /* ok! */ }
as fallback can assume video/mp4
unknown headers , types, create blob video/mp4
mime-type , let browser deal file there.
also see link above details on offsets , box lengths.
working demo
the following demo limited check types of given example files. of course need extend other mp4 types (type field) check in real-world application using example 1 array audio types, 1 video etc.
load 1 of files have analyzed.
var inp = document.queryselector("input"); inp.onchange = function(e) { var reader = new filereader(); reader.onload = analyze; reader.readasarraybuffer(e.target.files[0]); }; function analyze(e) { var buffer = e.target.result, view = new dataview(buffer), blob, url; // check file type if (view.getuint32(4) !== 0x66747970) { // = "ftyp" alert("not mp4 file!"); return } // check if audio or audio+video if (view.getuint32(8) === 0x64617368) { // = "dash" alert("audio\n(see console example url)"); blob = new blob([buffer], {type: "audio/mp4"}); } else if (view.getuint32(8) === 0x6d703432 || // = "mp42" view.getuint32(8) === 0x69736f6d) { // = "isom" alert("video+audio\n(see console example url)"); blob = new blob([buffer], {type: "video/mp4"}); } else { // assume video/mp4 alert("unsupported:\n0x" + (view.getuint32(8)).tostring(16)); blob = new blob([buffer], {type: "video/mp4"}); } // convert blob url can used video/audio element url = (url || webkiturl).createobjecturl(blob); console.log("copy , paste tab, wo/quotes:", url); }
pick mp4 file: <input type="file">
Comments
Post a Comment