如果有人熟悉使用youtubeextractor,我正在尝试执行以下操作。我正在使用该网站上发布的这个示例库。
string link = "youtube link";
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
VideoInfo video = videoInfos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 360);
此代码仅查找特定分辨率,如果该分辨率不存在该怎么办?
如果有人有一个寻找1080分辨率的例子,如果1080不存在那么我们寻找720,否则按照这个优先级顺序获得420分辨率:1080 -> 720 -> 480
答案 0 :(得分:1)
VideoInfo video = videoInfos.OrderByDescending(info=>info.Resolution)
.First(info => info.VideoType == VideoType.Mp4)
这将返回具有最佳分辨率的.MP4视频。
如果您只对某些分辨率感兴趣,那么您可以将其作为
var allowedResolutions = new List<int>() { 1080, 720, 480, 360 };
VideoInfo video = videoInfos.OrderByDescending(info=>info.Resolution)
.Where(info => allowedResolutions.Contains(info.Resolution))
.First(info => info.VideoType == VideoType.Mp4)
答案 1 :(得分:1)
IEnumerable<VideoInfo> videos = DownloadUrlResolver.GetDownloadUrls(txtUrl.Text);
foreach (var vid in videos) {
if (vid.Resolution > maxRez)
maxRez = vid.Resolution;
}
cboRezolution.Text = maxRez.ToString();
VideoInfo video = videos.First(p => p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(maxRez));
lblStatus.Text = video.Title;
答案 2 :(得分:0)
public static IEnumerable<VideoInfo> GetVideoInfos(YoutubeModel model)
{
int xx;
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(model.Link);
int[] arr = new int[3] { 360, 720, 1080 };
for (xx = 0; xx < 3; xx++)
{
try
{
VideoInfo video = videoInfos
.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == arr[xx]);
}
catch (Exception st)
{
}
}
return videoInfos;
}
//Returns VideoInfo object (Only for video model)
public static VideoInfo GetVideoInfo(YoutubeVideoModel videoModel)
{
//Select the first .mp4 video with 360p resolution
VideoInfo video = videoModel.VideoInfo.First();
return video;
}
答案 3 :(得分:0)
作为替代方案,考虑使用YoutubeExplode,它具有用于存储视频质量的强类型枚举,以及许多其他优点。
您可以使用它来执行以下操作:
using (var client = new YoutubeClient())
{
// Get the video info
var videoInfo = await client.GetVideoInfoAsync("video_id");
// Get streams above 480p and sort them by quality descending (higher first)
var hqStreams = videoInfo.Streams.Where(s => s.Quality >= VideoStreamQuality.Medium480).OrderByDescending(s => s.Quality);
// Select the first stream (highest quality)
var bestStream = hqStreams.First();
}