admin管理员组文章数量:1357307
I'm creating a webpage that has a user upload a file. When they upload the file I need to be able to get the width and height of what they uploaded. The site only accepts media. I got the width and height of images working as you can just grab it from the metadata when it is loaded, but I am unable to get this for videos.
var video = document.createElement('video');
video.onloadedmetadata = () => {
console.log("Video loaded!");
alert("width: " + this.width + "\n" + "height: " + this.height);
};
video.onerror = () => {
alert ("Error!");
};
video.src = _URL.createObjectURL(files[0]);
This is what I currently have but always get the width and height to be 0. I have seen some solutions to this using Jquery but none with react.
Any ideas as to how this can be done? Thank you
I'm creating a webpage that has a user upload a file. When they upload the file I need to be able to get the width and height of what they uploaded. The site only accepts media. I got the width and height of images working as you can just grab it from the metadata when it is loaded, but I am unable to get this for videos.
var video = document.createElement('video');
video.onloadedmetadata = () => {
console.log("Video loaded!");
alert("width: " + this.width + "\n" + "height: " + this.height);
};
video.onerror = () => {
alert ("Error!");
};
video.src = _URL.createObjectURL(files[0]);
This is what I currently have but always get the width and height to be 0. I have seen some solutions to this using Jquery but none with react.
Any ideas as to how this can be done? Thank you
Share Improve this question asked Sep 19, 2018 at 3:49 Grant KochmannGrant Kochmann 1791 gold badge3 silver badges15 bronze badges1 Answer
Reset to default 8If I understand your question correctly, then you'd be wanting to access the videoWidth
and videoHeight
properties on the video
element.
So for instance, try updating your onloadedmetadata
handler like so:
video.onloadedmetadata = () => {
console.log("Video loaded!");
alert("width: " + video.videoWidth + "\n" + "height: " + video.videoHeight);
};
Hope that helps!
本文标签: javascriptGet dimensions of mp4 on user upload reactjsStack Overflow
版权声明:本文标题:javascript - Get dimensions of mp4 on user upload reactjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744075297a2586632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论