Skip to content

Instantly share code, notes, and snippets.

@n1ru4l
Created June 18, 2016 12:50
Show Gist options
  • Save n1ru4l/c884606ab0091fd9d627d1d2073a34ed to your computer and use it in GitHub Desktop.
Save n1ru4l/c884606ab0091fd9d627d1d2073a34ed to your computer and use it in GitHub Desktop.
Convert mediainfo duration to seconds
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
@burtontanner
Copy link

Thanks for this function. On osx they already convert to seconds. If you add this on line 4 it will work for all platforms.

if(Number(duration) == duration){ //is number
	return parseInt(duration);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment