user4861457
user4861457

Reputation:

How to detect if MP4 file is audio or video with fineuploader or javascript?

I am using fineuploader and I allow user to upload audio and video files but I'm facing an issue with MP4 files, I've different video players for both audio and video so I need to track if file is uploading was audio or video so I can save them in their particular folder and play with their specific player.

I would prefer if someone can give solution with javascript or fineuploader core functions but php solution will also works.

PS : I just need a way to do this, rest I'll take care

Thanks

Upvotes: 2

Views: 3888

Answers (3)

mido
mido

Reputation: 25034

Tested the below code in latest firefox and chrome, checking using Video element of HTML5, not sure if it is a hack, I am checking if the height / width of the video is zero, if yes then audio, else video:

function isVideo(url){
  return new Promise(function(res, rej){
    var video = document.createElement('video');
    video.preload='metadata';
    video.onloadedmetadata = function(evt){
      res(!!(video.videoHeight && video.videoWidth));
      video.src = null;      
    };
    video.src = url;
  });
}

usage:

isVideo('http://example.com/aa.mp4').then(function(bool){...});
isVideo(window.URL.createObjectURL(blob)).then... // for file objects and blobs

same with a callback:

function isVideo(url, callback){
  return new Promise(function(res, rej){
    var video = document.createElement('video');
    video.preload='metadata';
    video.onloadedmetadata = function(evt){
      callback(!!(video.videoHeight && video.videoWidth));
      video.src = null;      
    };
    video.src = url;
  });
}

Upvotes: 4

Ray Nicholus
Ray Nicholus

Reputation: 19890

The best or at least the easiest way to handle this may be to use ffmpeg. For example, ffmpeg -i file.mp4 will provide a bit of metadata about the file, including streams/tracks. If there are any video streams, or if there are only audio streams, then the file is not a video.

Upvotes: 2

Rajesh kannan
Rajesh kannan

Reputation: 634

If you can get the uploaded file name in one variable assume if it is a variable called filename then the following check will give you the solution

if(filename.split('.').pop().toUpperCase()=="MP4")

Upvotes: -2

Related Questions