1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
YouTubeScraper/YouTube.API/Resources/VideoPlaybackResource.cs
T
2020-05-10 15:11:42 +03:00

73 lines
1.9 KiB
C#

using Google.Apis.Services;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Threading.Tasks;
using YouTube.Models;
using YoutubeExplode;
using YoutubeExplode.Videos.ClosedCaptions;
using YoutubeExplode.Videos.Streams;
namespace YouTube.Resources
{
public class VideoPlaybackResource
{
IClientService ClientService { get; }
public VideoPlaybackResource(IClientService clientService) =>
ClientService = clientService;
public ListRequest List(string videoId) =>
new ListRequest(ClientService, videoId);
public class ListRequest
{
IClientService Service { get; }
public string Id { get; set; }
public async Task<VideoPlayback> ExecuteAsync()
{
VideoPlayback item = new VideoPlayback();
YoutubeClient client = new YoutubeClient(Service.HttpClient);
item.Id = Id;
item.PlaybackUrls.ValidUntil = DateTime.Now + TimeSpan.FromHours(1);
item.PlaybackUrls.LiveStreamUrl = await client.Videos.Streams.GetHttpLiveStreamUrlAsync(Id);
if (!string.IsNullOrWhiteSpace(item.PlaybackUrls.LiveStreamUrl))
return item;
item.PlaybackUrls.VideoFilesManifest = await client.Videos.Streams.GetManifestAsync(Id);
ClosedCaptionManifest ccSet = await client.Videos.ClosedCaptions.GetManifestAsync(Id);
List<ClosedCaptionInfo> captions = new List<ClosedCaptionInfo>();
foreach (ClosedCaptionTrackInfo i in ccSet.Tracks)
captions.Add(new ClosedCaptionInfo
{
AutoGenerated = i.IsAutoGenerated,
Url = i.Url,
Language = new CultureInfo(i.Language.Code),
TrackInfo = i
});
item.ClosedCaptions = captions.AsReadOnly();
return item;
}
public VideoPlayback Execute()
{
Task<VideoPlayback> task = ExecuteAsync();
task.Wait();
return task.Result;
}
public ListRequest(IClientService service, string id)
{
Id = id;
Service = service;
}
}
}
}