我使用 Extjs 4.2 和 php 上传视频。但我想用允许用户上传的可用时间来限制视频的持续时间。我的代码只允许 .avi 和 .mov 。
最佳答案
好吧,正如我在评论中提到的,我对这个很感兴趣,所以我决定四处看看,看看有什么可能。简短的回答,不是很多。但以一种非常的方式来看,它看起来……有点可能。
我想在序言中说,这本身并不是一个真正的答案,更多的是一个概念验证。它使用一些 HTML5 功能,例如 <video>
和FileReader
- 我想也许可以读取 <video>
的持续时间标签,所以我对此做了一些谷歌搜索。
因为它们显然会在旧浏览器上严重失败......我只在 Chrome 上检查过这一点。不知道是不是FileReader
在 Firefox 中的实现方式有所不同。我也不能谈论其他视频格式等。
无论如何,我当然不会依赖它进行验证,但它可能是现代浏览器“方便”验证功能的起点?
我仍然认为唯一强大的全面验证解决方案是:
- 允许将任何视频上传到您的服务器,但不超过合理的大致大小限制。
- 在服务器上创建一个队列/异步进程,对文件本身执行第二遍验证。正如我提到的
ffmpeg
这里可能是一个不错的选择?- 向用户发送成功/失败电子邮件?
顺便说一句,这里有一个 SO 答案,它演示了 getting length of a video using ffmpeg
创建页面test.html
与 input[type=file]
元素:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Duration</title>
</head>
<body>
<input id="upload" type="file">
<div id="duration">Please choose a video</div>
<script src="path/to/duration.js"></script>
</body>
</html>
path/to/duration.js
的内容脚本...请原谅我的 JavaScript,它远非完美:
(function() {
var upload = document.getElementById('upload'), // form input
duration = document.getElementById('duration'); // output for user
// add a change event listener to the form input
upload.addEventListener('change', function(e) {
var file,
reader;
// check that a file has been selected
if (this.files.length !== 1) {
return;
}
duration.innerText = 'reading video...';
file = this.files[0];
// check the file's mime type, we want mp4 in this example
if (file.type !== 'video/mp4') {
duration.innerText = 'expected video/mp4, got ' + file.type;
return false;
}
// create a FileReader object
// and read the file as a data/url
// string so we can inline it later
reader = new FileReader();
reader.readAsDataURL(file);
// callback when the reader is complete
reader.onload = function() {
var video,
timeout;
duration.innerText = 'processing video...';
// create a html <video> element
// assign data/url as src
video = document.createElement('video');
video.src = this.result;
// poll the video readyState until it's ready
// this came from another SO answer (which I accidentally closed... sorry/thanks :s )
// we should now have our video duration, so echo to the browser!
timeout = setInterval(function(){
if (video.readyState > 0) {
duration.innerText = 'video is ' + video.duration + ' seconds';
clearInterval(timeout);
}
}, 500);
};
}, false);
})();
粗糙并准备好!
顺便说一句,HTML5 Rocks 文档确实很有帮助。
希望这有帮助:)
关于php - 有什么方法可以确定和限制上传视频的持续时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827611/