R
R
RAPOS2015-06-03 08:42:52
Video
RAPOS, 2015-06-03 08:42:52

How to get the dimensions of a video file in JavaSript?

Actually, if you use FileReader to read the selected file before uploading it to the site, then for example, finding out the width or height of the image will not help:
var img = new Image(file);
To work with audio, it will help:
var audio = new Audio(file);
What about the video? I want to check the width and height of the video before uploading to the site. What are the ways? Or not to use FileReader for this ?

<input id="file" type="file"/>
<script>
var file = document.getElementById('file').files[0];
document.getElementById('file').addEventListener('change', read_file(file));
function read_file(){
     var fr = new FileReader();
     fr.onloaded = function(e){
          var src = e.target.result;
          document.getElementByTagName('body').appendChild('<video src="'+ src +'"></video');
          var video = document.getElementByTagName('video')[0];
          console.log(video.videoHeight); // Возвращает '0', хотя видео присутствует
     }
}
</script>

I ask for help in solving this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Push Pull, 2015-06-03
@RAPOS

after - var video = document.getElementByTagName('video')[0];
here is the code to do:

var interval = setInterval(function () {
       if ( video.videoHeight > 0 ) {
           clearInterval(interval);
           console.log(video.videoHeight);
      }
}, 200);

200 ms - for example, you can do less / more.
why you get 0 - because the browser has not processed the video completely, and you are already trying to access it, the larger the video size, the longer you have to wait.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question