private async Task <SongInfo> GetSongInfoViaYtExplodeAsync(string url) { if (!YoutubeClient.TryParseVideoId(url, out string id)) { return(null); } YoutubeExplode.Models.Video video = await this.ytExplode.GetVideoAsync(id).ConfigureAwait(false); if (video is null) { return(null); } YoutubeExplode.Models.MediaStreams.MediaStreamInfoSet streamInfo = await this.ytExplode.GetVideoMediaStreamInfosAsync(video.Id).ConfigureAwait(false); YoutubeExplode.Models.MediaStreams.AudioStreamInfo stream = streamInfo.Audio .OrderByDescending(x => x.Bitrate) .FirstOrDefault(); if (stream is null) { return(null); } return(new SongInfo { Provider = "YouTube", Query = $"{_ytUrl}/watch?v={video.Id}", Thumbnail = video.Thumbnails.MediumResUrl, TotalTime = video.Duration, Uri = stream.Url, VideoId = video.Id, Title = video.Title, }); }
public override async Task SetURLAsync(string url) { try { id = YoutubeClient.ParseVideoId(url); var client = new YoutubeClient(); Controller?.UpdateMessage(this, KEY_YOUTUBE, new PlainTextMessage(AppResources.GetString("YouTubeLinkDetectedButWaiting"))); video = await client.GetVideoAsync(id); infos = await client.GetVideoMediaStreamInfosAsync(id); if (infos.Muxed.Count > 0) { Controller?.SetComboBoxLayoutVisibility(this, true); } else { throw new Exception(); } Controller?.UpdateMessage(this, KEY_YOUTUBE, new PlainTextMessage( AppResources.GetString("YouTubeLinkDetectedButWaiting") + " - " + video.Title )); foreach (var info in infos.Muxed) { PlainTextComboBoxData data = new PlainTextComboBoxData(); data.Text = info.VideoQuality.ToString() + " - " + info.VideoEncoding.ToString(); Controller?.AddComboBoxItem(this, data); } Controller?.SetComboBoxSelectionChangedListener(this, async(item) => { innerAnalyser?.Dispose(); string target = GetURLFromInfos(item.Text); if (target != null) { innerAnalyser = Converters.UrlConverter.GetAnalyser(target); if (innerAnalyser != null) { URL = target; innerAnalyser.BindVisualController(Controller); Controller?.RegistAnalyser(this, innerAnalyser); await innerAnalyser.SetURLAsync(target); } } } ); } catch (Exception) { Controller?.UpdateMessage(this, KEY_YOUTUBE, new PlainTextMessage( AppResources.GetString("YouTubeLinkDetectedButFailed") )); Controller?.SetComboBoxLayoutVisibility(this, false); } }