2016-04-30 124 views
0

如果任何人都熟悉使用youtubeextractor,我正在尝试执行以下操作。我正在使用该图书馆网站上发布的这个示例。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

回答

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; 
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) 
+0

你有点失去了我的这种反应。 OrderByDescending()如何寻找特定的分辨率?我错过了什么吗? – sledgehammer

+0

@sledgehammer它试图找到最大分辨率,以可用为准 – Eser

+0

可以说我有分辨率存储在一个ini文件中,并且我想 – sledgehammer

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; 
} 
-1

作为替代,可以考虑使用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(); 
}