Created
February 11, 2022 11:32
-
-
Save mklasen/239f148538e3cff98c01addb95f97d82 to your computer and use it in GitHub Desktop.
Video upload sample with duration and video sample after selecting a file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- This sample uses TailwindCSS, it works without but will not have any styling. --> | |
<div class="container mx-auto py-12 font-sans"> | |
<input type="file" class="bg-gray-100 p-5 block border mb-2" /> | |
<div class="duration py-2">Duration:<span class="font-bold px-2"></span></div> | |
<div class="progress py-2"></div> | |
<div class="video-sample w-4/12 bg-gray-200 shadow-md rounded border"> </div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This sample uses Babel. | |
let file = document.querySelector('input'); | |
let durationElem = document.querySelector('.duration span'); | |
let progress = document.querySelector('.progress'); | |
let videoSample = document.querySelector('.video-sample'); | |
window.addEventListener('DOMContentLoaded', (event) => { | |
file.addEventListener('change', runAfterChange); | |
}); | |
async function runAfterChange(e) { | |
const video = await getVideo(e.target.files[0]); | |
videoSample.appendChild(video); | |
durationElem.innerHTML = Math.round(video.duration) + ' minutes'; | |
startUpload(); | |
} | |
const getVideo = (file) => | |
new Promise((resolve) => { | |
let video = document.createElement('video'); | |
video.src = URL.createObjectURL(file); | |
video.onloadedmetadata = () => resolve(video); | |
}); | |
function startUpload() { | |
progress.innerHTML = 'Starting upload..'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.video-sample { | |
min-height: 300px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment