Skip to content

Instantly share code, notes, and snippets.

@korakot
Last active January 6, 2021 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korakot/07e76d7bab7e0f0e2d36aeb8a9a46861 to your computer and use it in GitHub Desktop.
Save korakot/07e76d7bab7e0f0e2d36aeb8a9a46861 to your computer and use it in GitHub Desktop.
Get youtube video duration from its id, using YouTube Data API.
from requests import get
!pip install isodate -q
import isodate
key = 'AZxxxx' # from https://console.cloud.google.com/apis/credentials
def get_durations(id_list):
url = 'https://www.googleapis.com/youtube/v3/videos'
kw = {
'id': ','.join(id_list),
'key': key,
'part': 'contentDetails',
}
r = get(url, kw)
dur_list = []
for it in r.json()['items']:
dur = it['contentDetails']['duration']
dur = isodate.parse_duration(dur)
dur = str(dur).lstrip('0:')
dur_list.append(dur)
return dur_list # max 50 items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment