Created
June 18, 2016 12:50
-
-
Save n1ru4l/c884606ab0091fd9d627d1d2073a34ed to your computer and use it in GitHub Desktop.
Convert mediainfo duration to seconds
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
function parseDuration(duration) { | |
let durationInSeconds = 0 | |
const hourIndex = duration.indexOf('h') | |
if(hourIndex > -1) { | |
let hours = parseInt(duration.substr(0, hourIndex), 10) | |
duration = duration.substr(hourIndex + 1) | |
durationInSeconds += hours * 60 * 60 | |
} | |
const minuteIndex = duration.indexOf('mn') | |
if(minuteIndex > -1) { | |
let minutes = parseInt(duration.substr(0, minuteIndex), 10) | |
duration = duration.substr(minuteIndex + 2) | |
durationInSeconds += minutes * 60 | |
} | |
const secondIndex = duration.indexOf('s') | |
if(secondIndex > -1) { | |
let seconds = parseInt(duration.substr(0, secondIndex), 10) | |
durationInSeconds += seconds | |
} | |
return durationInSeconds | |
} | |
parseDuration('1mn 16s') // 76 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this function. On osx they already convert to seconds. If you add this on line 4 it will work for all platforms.