【问题标题】:Is it possible to get the duration (time) video through Youtube data API v3.0是否可以通过 Youtube 数据 API v3.0 获取时长(时间)视频
【发布时间】:2015-10-17 16:56:51
【问题描述】:

是否可以获取持续时间 (时间)视频通过Youtube数据API v3.0。 如果有,怎么做?

【问题讨论】:

标签: youtube-api youtube-data-api


【解决方案1】:

使用 javascript

function converTime(d) {
  //ignore the "PT" part
  d = d.search(/PT/i) > -1? d.slice(2) : d;
  let h, m, s;
  //indexes of the letters h, m, s in the duration
  let hIndex = d.search(/h/i),
      mIndex = d.search(/m/i),
      sIndex = d.search(/s/i);
  //is h, m, s inside the duration
  let dContainsH = hIndex > -1,
      dContainsM = mIndex > -1,
      dContainsS = sIndex > -1;
  //setting h, m, s
  h = dContainsH? d.slice(0, hIndex) + ":" : "";
  m = dContainsM? d.slice(dContainsH ? hIndex + 1 : 0, mIndex) : dContainsH? "0" : "0";
  s = dContainsS? d.slice(dContainsM ? mIndex + 1 : hIndex + 1, sIndex) : "0";
  //adding 0 before m or s
  s = (dContainsM || dContainsS) && s < 10? "0" + s: s;
  m = (dContainsH || dContainsM) && m < 10? "0" + m + ":" : m + ":";
  return d !== "0S" ? h + m + s : "LIVE"
}
console.log(converTime("PT6M7S"));

【讨论】:

    【解决方案2】:

    这是我在 .NET 和 C# 中的做法。
    首先包括“contentDetails”部分

    var searchListRequest = youtubeService.Videos.List("snippet,contentDetails");
    

    第二次将持续时间转换为更易于编程管理的内容,如下所示:

    TimeSpan YouTubeDuration = System.Xml.XmlConvert.ToTimeSpan(searchResult.ContentDetails.Duration);
    

    希望对你有帮助

    【讨论】:

    • 你能解释一下吗?有 YoutubeService 的程序集文件是什么?
    【解决方案3】:

    在进行搜索调用后,您必须调用 Youtube Data API 的视频资源。您最多可以在搜索中添加 50 个视频 ID,因此您不必为每个元素调用它。

    https://developers.google.com/youtube/v3/docs/videos/list

    您需要设置 part=contentDetails,因为有持续时间。

    例如下面的调用:

    https://www.googleapis.com/youtube/v3/videos?id=9bZkp7q19f0&part=contentDetails&key={YOUR_API_KEY}
    

    给出这个结果:

    {
     "kind": "youtube#videoListResponse",
     "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/ny1S4th-ku477VARrY_U4tIqcTw\"",
     "items": [
      {
    
       "id": "9bZkp7q19f0",
       "kind": "youtube#video",
       "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/HN8ILnw-DBXyCcTsc7JG0z51BGg\"",
       "contentDetails": {
        "duration": "PT4M13S",
        "dimension": "2d",
        "definition": "hd",
        "caption": "false",
        "licensedContent": true,
        "regionRestriction": {
         "blocked": [
          "DE"
         ]
        }
       }
      }
     ]
    }
    

    时间格式为 ISO 8601 字符串。 PT代表Time Duration,4M是4分钟,13S是13秒。

    详情请参考this问题

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 2014-07-29
      • 2015-11-30
      • 2011-11-14
      • 2018-06-28
      • 2014-08-30
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      相关资源
      最近更新 更多