Skip to content

Instantly share code, notes, and snippets.

@mklasen
Created February 11, 2022 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklasen/239f148538e3cff98c01addb95f97d82 to your computer and use it in GitHub Desktop.
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 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 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..';
}
.video-sample {
min-height: 300px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment