Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse YouTube duration ISO 8601

How can I parse the youtube duration format which I believe is ISO 8601

This request

https://www.googleapis.com/youtube/v3/videos?id=Kdgt1ZHkvnM&part=contentDetails&key={API_KEY}

Returns

{
 "kind": "youtube#videoListResponse",
 "etag": "\"QVyS2yjpsZ-tKkk4JvgYeO_YkzY/Do26Zx0a-KfdN4FPvoMAgqiFNRA\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"QVyS2yjpsZ-tKkk4JvgYeO_YkzY/yZ-09PZbpkEHSEcQeekJuGOCbJY\"",
   "id": "Kdgt1ZHkvnM",
   "contentDetails": {
    "duration": "PT20M1S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": false
   }
  }
 ]
}

Is there a library that parses this format "PT20M1S" for .Net?

like image 463
ojhawkins Avatar asked Jun 25 '14 13:06

ojhawkins


1 Answers

Yes, YouTube uses ISO 8601 duration format, for more you can check it here Wiki ISO 8601 duration.

So what you need to do is to use the following code (of course in a proper context, when you will parse the XML), but you can get the idea:

TimeSpan youTubeDuration = XmlConvert.ToTimeSpan("PT20M1S");
like image 162
Peter Stegnar Avatar answered Nov 03 '22 05:11

Peter Stegnar