For jQuery, try this code. What it does it get the data from the input
tag and instantiates a FileReader
instance. We add an event listener to this instance and we append a <source>
tag as child to the <video>
tag. We are able to get the duration once the data has been loaded. Adding a type="video/mp4"
attribute to the source tag is a small enhancement that would work on most .mov files, but not all (untested). This is an enhancement to another SO answer located here.
$(document).ready(function() {
var data = $('#file-input')[0]; // File from <input type="file"> tag
var reader = new FileReader();
reader.addEventListener('load', function() {
var video = $('<video class="uploaded_video_src" controls></video>');
if (reader.result) {
$('.uploaded_video_src').append(`<source src="${reader.result}" type="video/mp4">`); // Add type attr for support for some .mov files
video.on('loadedmetadata', function() {
console.log('duration:', $(this)[0].duration);
});
}
});
});