Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hridaydutta123/a673db5e0145bc571d28c71035cb9405 to your computer and use it in GitHub Desktop.
Save hridaydutta123/a673db5e0145bc571d28c71035cb9405 to your computer and use it in GitHub Desktop.
Convert YouTube API duration to seconds
import re
def YTDurationToSeconds(duration):
match = re.match('PT(\d+H)?(\d+M)?(\d+S)?', duration).groups()
hours = _js_parseInt(match[0]) if match[0] else 0
minutes = _js_parseInt(match[1]) if match[1] else 0
seconds = _js_parseInt(match[2]) if match[2] else 0
return hours * 3600 + minutes * 60 + seconds
# js-like parseInt
# https://gist.github.com/douglasmiranda/2174255
def _js_parseInt(string):
return int(''.join([x for x in string if x.isdigit()]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment