I have a function that logs the video duration into the console before the video is uploaded. However, I cannot get the video duration outside the addEventListener function because it returns back NaN. Despite of it, inside the function it successfully logs the proper video duration but if I save it into a variable it does not get the right value.
Video duration function
var duration = 0; // Set default variable for storing video duration
if(vfile.type == "video/webm" || vfile.type == "video/mp4" || vfile.type == "video/ogg" || vfile.type == "video/mov"){
        var video = document.createElement('video'); // Create video element
        video.preload = 'metadata'; // Set preload attribute to metadata
        window.URL.revokeObjectURL(video.src);
        video.addEventListener('durationchange', function() { // Check for duration
            console.log('Duration change', video.duration); // Successfully logs video duration into console e.g.: 13.012
            duration = video.duration; // Set duration variable to video.duration
        });
        console.log("Duration: ", duration); // Returns back 0
    }
    video.src = URL.createObjectURL(vfile);
If I set variable duration to video.duration outside the addEventListener function it gives back NaN.
All in all, how could I set variable duration to the actual video duration for later usage in the script?
 
    