Business.DownloadBusiness.SelectBestFormat C# (CSharp) Метод

SelectBestFormat() публичный статический Метод

Returns the best format from the list in this order of availability: WebM, Mp4 or Flash. Mp4 will be chosen if WebM is over 35% smaller.
public static SelectBestFormat ( IEnumerable list ) : BestFormatInfo
list IEnumerable The list of videos to chose from.
Результат BestFormatInfo
        public static BestFormatInfo SelectBestFormat(IEnumerable<VideoInfo> list) {
            var MaxResolutionList = (from v in list
                                     where (Settings.SavedFile.MaxDownloadQuality == 0 || v.Resolution <= Settings.SavedFile.MaxDownloadQuality)
                                        && v.AdaptiveType != AdaptiveType.Audio
                                     orderby v.Resolution descending
                                     select v).ToList();
            MaxResolutionList = MaxResolutionList.Where(v => v.Resolution == MaxResolutionList.First().Resolution).ToList();

            // If for the maximum resolution, only some formats have a file size, the other ones must be queried.
            if (MaxResolutionList.Any(v => v.FileSize == 0) && MaxResolutionList.Any(v => v.FileSize > 0)) {
                foreach (VideoInfo item in MaxResolutionList.Where(v => v.FileSize == 0)) {
                    DownloadUrlResolver.QueryStreamSize(item);
                }
            }

            VideoInfo BestVideo = (from v in MaxResolutionList
                                   // WebM VP9 encodes ~35% better. non-DASH is VP8 and isn't better than MP4.
                                   let Preference = (int)((v.VideoType == VideoType.WebM && v.AdaptiveType == AdaptiveType.Video) ? v.FileSize * 1.35 : v.FileSize)
                                   where v.Resolution == MaxResolutionList.First().Resolution
                                   orderby Preference descending
                                   select v).FirstOrDefault();

            if (BestVideo != null) {
                BestFormatInfo Result = new BestFormatInfo();
                Result.BestVideo = BestVideo;
                // Even for non-DASH videos, we still want to know what audio is available even though it may not be downloaded.
                Result.BestAudio = SelectBestAudio(from v in list
                                                   where (v.CanExtractAudio || v.AdaptiveType == AdaptiveType.Audio)
                                                   orderby v.AudioBitrate descending
                                                   select v);
                return Result;
            } else
                return null;
        }

Usage Example

Пример #1
0
 public async Task StartScan(List <VideoListItem> selection, CancellationToken cancel)
 {
     await selection.ForEachAsync(5, cancel, async item => {
         Media VideoData = PlayerAccess.GetVideoById(item.MediaId.Value);
         if (VideoData != null && !item.IsBusy)
         {
             try {
                 // Query the server for media info.
                 SetStatus(item, VideoListItemStatusEnum.DownloadingInfo);
                 VideoInfo VInfo = await DownloadManager.GetDownloadInfoAsync(VideoData.DownloadUrl);
                 if (VInfo != null)
                 {
                     // Get the highest resolution format.
                     BestFormatInfo VideoFormat = DownloadBusiness.SelectBestFormat(VInfo.Streams);
                     if (VideoFormat == null || VideoFormat.BestVideo == null)
                     {
                         SetStatus(item, VideoListItemStatusEnum.Failed);
                     }
                     else
                     {
                         SetStatus(item, await IsHigherQualityAvailable(VideoFormat, Settings.NaturalGroundingFolder + VideoData.FileName));
                     }
                     if (VideoFormat != null && !string.IsNullOrEmpty(VideoFormat.StatusText))
                     {
                         SetStatus(item, item.Status, item.StatusText + string.Format(" ({0})", VideoFormat.StatusText));
                     }
                 }
                 else
                 {
                     SetStatus(item, VideoListItemStatusEnum.InvalidUrl);
                 }
             }
             catch {
                 SetStatus(item, VideoListItemStatusEnum.Failed);
             }
         }
     });
 }