是否可以使用C# .NET和YouTube Data API v3 (不是JavaScript或任何其他客户端语言)来获取我的每个YouTube视频的时长?
我已经搜索了几天,我唯一想出来的就是谷歌在他们的.NET Code Samples page上有一个例子,它只展示了如何获得一个playlistItems.list。然而,这并没有给我一个列表的视频及其相关的持续时间从contentDeatils。
请帮我弄清楚这件事。谢谢你们所有人。
发布于 2017-06-28 07:14:32
我也遇到过类似的情况,我需要更新我所有上传的描述。请在此处查看隐藏的宝石:https://github.com/youtube/api-samples/tree/master/dotnet
在项目Google.Apis.YouTube.Samples.UpdateVideos
中,你会发现一个循环,你可以稍微修改一下,并使用它来获得每个视频的持续时间。
foreach (var channel in channelsListResponse.Items)
{
var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
Console.WriteLine("Videos in list {0}", uploadsListId);
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;
// Retrieve the list of videos uploaded to the authenticated user's channel.
var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
foreach (var playlistItem in playlistItemsListResponse.Items)
{
var videoRequest = youtubeService.Videos.List("snippet");
videoRequest.Id = playlistItem.Snippet.ResourceId.VideoId;
videoRequest.MaxResults = 1;
var videoItemRequestResponse = await videoRequest.ExecuteAsync();
// Get the videoID of the first video in the list
var video = videoItemRequestResponse.Items[0];
var duration = video.ContentDetails.Duration;
}
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
发布于 2018-06-04 07:08:08
好的,这是我最终如何解决这个问题的解释。
我希望使用YouTube data API实现的目标是根据用户名或频道ID从任何YouTube频道检索视频列表。
理想情况下,我们应该能够向YouTube索要来自特定YouTube频道的所有视频。然而,它似乎不是这样工作的。最后,我们需要向YouTubeService.Videos.List方法发送一个视频列表请求。这将允许我们检索视频对象列表的内容详细信息、片段和统计信息。但是,此方法需要几个参数。一个是VideoListRequest.ID参数,它是您希望检索的视频集合中的视频ID的字符串数组。另一个是VideoListRequest.MaxResults参数。这将是您希望拉回的最大视频数量。
要检索视频ID的列表,我们需要再次调用YouTube API并从YouTubeService.PlaylistItems.List方法检索播放列表项的列表。但是,此方法需要UploadsListID,必须通过另一次调用YouTube API的YouTubeService.Channels.List方法来获取该API。
因此,我们需要对YouTube应用程序接口进行3次调用,如下图所示。
第一步是根据用户名或频道ID获取频道列表。UploadsListId将来自ChannelListResponse: channelsListResponse.Items.ContentDetails.RelatedPlaylists.Uploads.
第二步是使用上一步中的UploadsListID获取播放列表项的列表。这允许我们检索上传的视频播放列表中每个视频的视频ID,并将它们放入字符串列表中。
最后,第三步是根据前面字符串列表中的视频in获取视频列表。通过此响应,我们可以检索每个视频中的持续时间,并将YouTube的HMS格式转换为“可用的”Timespan格式字符串(h:mm:ss)。
这是我用来完成上述描述的代码:
public async Task<List<Video>> GetVideoListAsync(ChannelListMethod Method, string MethodValue, int? MaxVideos)
{
// Define variables needed for this method
List<string> videoIdList = new List<string>();
List<Video> videoList = new List<Video>();
string uploadsListId = null;
// Make sure values passed into the method are not null or empty.
if (MaxVideos == null)
{
throw new ArgumentNullException(nameof(MaxVideos));
}
if (string.IsNullOrEmpty(MethodValue))
{
return videoList;
}
// Create the service.
using (YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer
{
ApiKey = _apiKey,
ApplicationName = _appName
}))
{
// Step ONE is to get a list of channels for a specified YouTube user or ChannelID.
// Create the FIRST Request object to get a list of YouTube Channels and get their contentDetails
// based on either ForUserName or ChannelID.
ChannelsResource.ListRequest channelsListRequest = youtubeService.Channels.List("contentDetails");
if (Method == ChannelListMethod.ForUserName)
{
// Build the ChannelListRequest using UserName
channelsListRequest.ForUsername = MethodValue;
}
else
{
// Build the ChannelListRequest using ChannelID
channelsListRequest.Id = MethodValue;
}
// This is the FIRST Request to the YouTube API.
// Retrieve the contentDetails part of the channel resource to get a list of channel IDs.
// We are only interested in the Uploads playlist of the first channel.
try
{
ChannelListResponse channelsListResponse = await channelsListRequest.ExecuteAsync();
uploadsListId = channelsListResponse.Items[0].ContentDetails.RelatedPlaylists.Uploads;
}
catch (Exception ex)
{
ErrorException = ex;
return videoList;
}
// Step TWO is to get a list of playlist items from the Uploads playlist.
// From the API response, use the Uploads playlist ID (uploadsListId) to be used to get list of videos
// from the videos uploaded to the user's channel.
string nextPageToken = "";
while (nextPageToken != null)
{
// Create the SECOND Request object for requestring a list of Playlist items
// from the channel's Uploads playlist.
// Limit the list to maxVideos items and continue to iterate through the pages.
PlaylistItemsResource.ListRequest playlistItemsListRequest = youtubeService.PlaylistItems.List("contentDetails");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = MaxVideos;
playlistItemsListRequest.PageToken = nextPageToken;
// This is the SECOND Request to YouTube and get a Response object containing
// the playlist items in the channel's Uploads playlist.
// Then iterate through the Response items to build a string list of the video IDs
try
{
PlaylistItemListResponse playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
foreach (PlaylistItem playlistItem in playlistItemsListResponse.Items)
{
videoIdList.Add(playlistItem.ContentDetails.VideoId.ToString());
}
}
catch (Exception ex)
{
ErrorException = ex;
return videoList;
}
// Now that we have a collection (string array) of video IDs,
// Step THREE is to retrieve the snippet, contentDetails, and statistics parts of the
// list of videos uploaded to the authenticated user's channel.
try
{
// Create the THIRD Request object for requestring a list of videos and their associated metadata.
var VideoListRequest = youtubeService.Videos.List("snippet, contentDetails, statistics");
// The next line converts the list of video Ids to a comma seperated string array of the video IDs
VideoListRequest.Id = String.Join(",", videoIdList);
VideoListRequest.MaxResults = MaxVideos;
var VideoListResponse = await VideoListRequest.ExecuteAsync();
// This is the THIRD Request to the YouTube API to get a Response object
// containing Collect each Video duration and convert to a usable time format.
foreach (var video in VideoListResponse.Items)
{
video.ContentDetails.Duration = HMSToTimeSpan(video.ContentDetails.Duration).ToString();
videoList.Add(video);
}
// request next page
nextPageToken = VideoListResponse.NextPageToken;
}
catch (Exception ex)
{
ErrorException = ex;
return videoList;
}
}
return videoList;
}
}
我知道这不是完美的解决方案,或者可能不是“最好”的解决方案,但我希望这能帮助其他人解决同样的问题。
感谢您的所有帮助@Janis S
https://stackoverflow.com/questions/44790637
复制相似问题