微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过在C#中调用YouTube API获得所有YouTube视频的时长

如何解决如何通过在C#中调用YouTube API获得所有YouTube视频的时长

我想在致电youtubeService.Search.List("snippet");之后获得列表中每个视频的时长 以下是我的代码

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = "API Key",ApplicationName = this.GetType().ToString()
            });

            var searchListRequest = youtubeService.Search.List("snippet");
            searchListRequest.MaxResults = 30;
            var searchListResponse = searchListRequest.Execute();

我看到持续时间在视频的内容详细信息中,但是每个视频的内容详细信息为空。

解决方法

查看search.list的文档后,您会看到它返回了search#resource的列表

{
  "kind": "youtube#searchResult","etag": etag,"id": {
    "kind": string,"videoId": string,"channelId": string,"playlistId": string
  },"snippet": {
    "publishedAt": datetime,"title": string,"description": string,"thumbnails": {
      (key): {
        "url": string,"width": unsigned integer,"height": unsigned integer
      }
    },"channelTitle": string,"liveBroadcastContent": string
  }
}

其中不包含有关视频时长的任何信息。该方法不会返回您要查找的信息。

视频列表

只有包含videos.listvideo.resource会为您提供视频的时长

 {
      "kind": "youtube#video","etag": "U-t7IORpXf52WZSn6hBzfgm6WLo","id": "XOIZZEdfrfA","contentDetails": {
        "duration": "PT8M51S","dimension": "2d","definition": "hd","caption": "false","licensedContent": true,"contentRating": {},"projection": "rectangular"
      }

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。