Last active
          April 15, 2019 08:39 
        
      - 
      
 - 
        
Save sebastiankade/efa447c0c476201562cc9087e73e3f85 to your computer and use it in GitHub Desktop.  
    Get duration of video or audio in react browser
  
        
  
    
      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
    
  
  
    
  | export const getVideoDuration = async (file) => | |
| new Promise((resolve, reject) => { | |
| const mediaType = file.type.split("/")[0]; | |
| if (mediaType === "video" || mediaType === "audio") { | |
| var video = document.createElement(mediaType); | |
| video.preload = "metadata"; | |
| video.onloadedmetadata = function() { | |
| window.URL.revokeObjectURL(video.src); | |
| resolve(video.duration); | |
| }; | |
| video.onerror = reject; | |
| video.src = URL.createObjectURL(file); | |
| } else { | |
| resolve(); | |
| } | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment