最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Client side Javascript get video widthheight before upload - Stack Overflow

programmeradmin1浏览0评论

I've been trying to piece together a combination of HTML5 video tag + the FileReader API but I haven't figured out how to get the dimensions of a video that a user is providing from their own computer.

Here is what I am referencing for width/ height:

HTML5 Video Dimensions

<video id="foo" src="foo.mp4"></video>

var vid = document.getElementById("foo");
vid.videoHeight; // returns the intrinsic height of the video
vid.videoWidth; // returns the intrinsic width of the video

But I want to know if it's possible to do this with a file from a user's computer (that they have selected via a normal input html tag).

Thanks!

I've been trying to piece together a combination of HTML5 video tag + the FileReader API but I haven't figured out how to get the dimensions of a video that a user is providing from their own computer.

Here is what I am referencing for width/ height:

HTML5 Video Dimensions

<video id="foo" src="foo.mp4"></video>

var vid = document.getElementById("foo");
vid.videoHeight; // returns the intrinsic height of the video
vid.videoWidth; // returns the intrinsic width of the video

But I want to know if it's possible to do this with a file from a user's computer (that they have selected via a normal input html tag).

Thanks!

Share Improve this question edited May 23, 2017 at 12:02 CommunityBot 11 silver badge asked Apr 8, 2017 at 7:26 JasonAddFourJasonAddFour 1631 gold badge1 silver badge10 bronze badges 1
  • 1 File instance does not expose contents of underlying file content, save for MIME type at .type property, .size, .lastModified, and .lastModifiedDate. You can create a <video> element with .src set to Blob URL or data URI representation of file, utilize loadedmetadata event to get dimension of video media, see stackoverflow.com/a/4129579 – guest271314 Commented Apr 8, 2017 at 8:00
Add a comment  | 

3 Answers 3

Reset to default 11

A bit unclean solution using basic FileReader + Data URL.

<html>
  <head>
<style>
div {
    margin: 20px;
}
</style>
  </head>
  <body>
    <h1>Get Dimensions</h1>
    <div>
        <label for="load-file">Load a file:</label>
          <input type="file" id="load-file">
    </div>
    <div>
        <button type="button" id="done-button">Get me dimensions</button>
    </div>

    <script src="//cdn.jsdelivr.net/jquery/2.1.4/jquery.js"></script>
    <script>
(function ($) {
  $('#done-button').on('click', function () {
    var file = $('#load-file')[0].files[0];
    var reader  = new FileReader();
    var fileType =  file.type;
    console.log("type", fileType);
    reader.addEventListener("load", function () {
      var dataUrl =  reader.result;
      var videoId = "videoMain";
      var $videoEl = $('<video id="' + videoId + '"></video>');
      $("body").append($videoEl);
      $videoEl.attr('src', dataUrl);

      var videoTagRef = $videoEl[0];
      videoTagRef.addEventListener('loadedmetadata', function(e){
        console.log(videoTagRef.videoWidth, videoTagRef.videoHeight);
      });

    }, false);

    if (file) {
      reader.readAsDataURL(file);
    }
  });

})(jQuery);
    </script>
  </body>
</html>

Here is a simple and fast solution to get a video's size before upload. It doesn't require any dependency.

const url = URL.createObjectURL(file);
const $video = document.createElement("video");
$video.src = url;
$video.addEventListener("loadedmetadata", function () {
 console.log("width:", this.videoWidth);
 console.log("height:", this.videoHeight);
});
const onSelectVideo = (files) => {
const file = files[0];
const url = URL.createObjectURL(file);
let videoId = "videoMain";
const video = document.createElement("video");
const body = document.getElementsByTagName("body");

video.setAttribute("src", url);
video.setAttribute("videoId", videoId);
body[0]?.append(video);
let videoTagRef = document.querySelector("[videoId='videoMain']");

videoTagRef.addEventListener("loadedmetadata", function (e) {
  console.log(videoTagRef.videoWidth, videoTagRef.videoHeight);
});}
发布评论

评论列表(0)

  1. 暂无评论