private async Task StartDownloadAsync(DownloadItem downloadInfo) { downloadInfo.Status = DownloadStatus.Initializing; // Query the download URL for the right file. string VideoId = null; Video VInfo = null; MediaStreamInfoSet VStream = null; if (YoutubeClient.TryParseVideoId(downloadInfo.Url, out VideoId)) { try { var T1 = youtube.GetVideoAsync(VideoId); var T2 = youtube.GetVideoMediaStreamInfosAsync(VideoId); await Task.WhenAll(new Task[] { T1, T2 }); VInfo = T1.Result; VStream = T2.Result; } catch { } } if (VStream == null) { downloadInfo.Status = DownloadStatus.Failed; RaiseCallback(downloadInfo); return; } // Get the highest resolution format. BestFormatInfo BestFile = DownloadManager.SelectBestFormat(VStream, downloadInfo.Options ?? Options); BestFile.Duration = VInfo.Duration; if (BestFile.BestVideo != null && (downloadInfo.Action == DownloadAction.Download || downloadInfo.Action == DownloadAction.DownloadVideo)) { downloadInfo.Files.Add(new FileProgress() { Type = StreamType.Video, Url = BestFile.BestVideo.Url, Destination = downloadInfo.DestinationNoExt + GetVideoExtension(DownloadManager.GetVideoEncoding(BestFile.BestVideo)), Stream = BestFile.BestVideo, BytesTotal = BestFile.BestVideo.Size }); } if (BestFile.BestAudio != null && (downloadInfo.Action == DownloadAction.Download || downloadInfo.Action == DownloadAction.DownloadAudio)) { downloadInfo.Files.Add(new FileProgress() { Type = StreamType.Audio, Url = BestFile.BestAudio.Url, Destination = downloadInfo.DestinationNoExt + GetAudioExtension(BestFile.BestAudio.AudioEncoding), Stream = BestFile.BestAudio, BytesTotal = BestFile.BestAudio.Size }); } // Add extension if Destination doesn't already include it. string Ext = DownloadManager.GetFinalExtension(BestFile.BestVideo, BestFile.BestAudio); if (!downloadInfo.Destination.ToLower().EndsWith(Ext)) { downloadInfo.Destination += Ext; } await DownloadFilesAsync(downloadInfo).ConfigureAwait(false); }