gpt4 book ai didi

javascript - 有什么办法可以让我们在上传前获得视频时长?

转载 作者:可可西里 更新时间:2023-11-01 00:39:46 25 4
gpt4 key购买 nike

我想限制用户只能上传时长不超过 30 秒的视频。有什么办法吗?

最佳答案

试试这个

<form action="#" method="post" enctype="multipart/form-data">
File: <input type="file" name="fup" id="fup" /><br>
Duration: <input type="text" name="f_du" id="f_du" size="5" /> seconds<br>
<input type="submit" value="Upload" />
</form>
<audio id="audio"></audio>

<script>
// Code to get duration of audio /video file before upload - from: http://coursesweb.net/

//register canplaythrough event to #audio element to can get duration
var f_duration =0; //store duration
document.getElementById('audio').addEventListener('canplaythrough', function(e){
//add duration in the input field #f_du
f_duration = Math.round(e.currentTarget.duration);
document.getElementById('f_du').value = f_duration;
URL.revokeObjectURL(obUrl);
});

//when select a file, create an ObjectURL with the file and add it in the #audio element
var obUrl;
document.getElementById('fup').addEventListener('change', function(e){
var file = e.currentTarget.files[0];
//check file extension for audio/video type
if(file.name.match(/\.(avi|mp3|mp4|mpeg|ogg)$/i)){
obUrl = URL.createObjectURL(file);
document.getElementById('audio').setAttribute('src', obUrl);
}
});
</script>

关于javascript - 有什么办法可以让我们在上传前获得视频时长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47363517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com