简体   繁体   English

我可以为 HTML5 文件上传设置视频长度限制吗?

[英]Can I set a video length limit on a HTML5 file upload?

When uploading a video through a HTML5 input such as:通过 HTML5 输入上传视频时,例如:

<input type="file" id="input">

I know it is possible to set a limit on the file size using PHP / Node.Js.我知道可以使用 PHP/Node.Js 设置文件大小限制。

Is there a way to set a limit on the video length, not the file size?有没有办法限制视频长度,而不是文件大小?

IE: Only allow a user to upload videos which are 15 seconds or less. IE:只允许用户上传 15 秒或更短的视频。

Here's what I found:这是我发现的:

Solution 1: Create a video element that plays a local video file and then runs video.duration on it.解决方案 1:创建一个播放本地视频文件的视频元素,然后在其上运行 video.duration。

 var vid = document.getElementById("myVideo"); function myFunction() { alert(vid.duration); } (function localFileVideoPlayer() { 'use strict' var URL = window.URL || window.webkitURL var displayMessage = function (message, isError) { var element = document.querySelector('#message') element.innerHTML = message element.className = isError ? 'error' : 'info' } var playSelectedFile = function (event) { var file = this.files[0] var type = file.type var videoNode = document.querySelector('video') var canPlay = videoNode.canPlayType(type) if (canPlay === '') canPlay = 'no' var message = 'Can play type "' + type + '": ' + canPlay var isError = canPlay === 'no' displayMessage(message, isError) if (isError) { return } var fileURL = URL.createObjectURL(file) videoNode.src = fileURL } var inputNode = document.querySelector('input') inputNode.addEventListener('change', playSelectedFile, false) })()
 <h3>Step 1: Upload a video</h3> <h3>Step 2: Get length</h3> <h3>Step 3: ???</h3> <h3>Step 4: Profit</h3> <button onclick="myFunction()" type="button">Get video length</button> <hr> <div id="message"></div> <input type="file" accept="video/*"/> <video controls autoplay id="myVideo"></video>

Solution 2: use mediainfo.js https://mediainfo.js.org/解决方案2:使用mediainfo.js https://mediainfo.js.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM