/// <summary>
        /// Gets an instance asynchronously of the <see cref="YoutubeSongInfo"/> class.
        /// </summary>
        /// <param name="Source">The URL of the video</param>
        /// <returns>A <see cref="YoutubeSongInfo"/> instance.</returns>
        public static async Task <YoutubeSongInfo> GetVideoInfoAsync(string Source)
        {
            YoutubeDl d = new YoutubeDl("youtube-dl.exe", App.Path)
            {
                VideoID = YoutubeUri.GetVideoID(Source)
            };

            YoutubeMediaInfo r = await d.GetVideoInfo();

            var Result = new YoutubeSongInfo(
                Source: Source,
                Title: r.RecognizedMedia?.Title ?? r.Title,
                Artist: r.RecognizedMedia?.Artist,
                DurationInSeconds: r.Duration.TotalSeconds
                )
            {
                Formats = r.MediaFormats
            };

            if (App.Config.YoutubeDownloadThumbnail)
            {
                Result.AlbumImage = await r.Thumbnails.FirstOrDefault()?.DownloadAsImageAsync();
            }

            return(Result);
        }
Example #2
0
        /// <summary>
        /// Downloads informations about the video asynchronously.
        /// </summary>
        /// <returns>An <see cref="ExpandoObject"/> instance that holds the video information.</returns>
        public async Task <YoutubeMediaInfo> GetVideoInfo()
        {
            this.RequiresUpdateAndRetry = false;

            Process DownloaderProcess = new Process()
            {
                StartInfo = new ProcessStartInfo {
                    FileName               = this.ExecutablePath,
                    Arguments              = $"--dump-json {this.YoutubeUri}",
                    UseShellExecute        = false,
                    WindowStyle            = ProcessWindowStyle.Hidden,
                    CreateNoWindow         = true,
                    WorkingDirectory       = this.WorkingDirectory,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true
                }
            };

            StringBuilder StdOut = new StringBuilder();

            DownloaderProcess.OutputDataReceived += (o, e) => StdOut.Append(e.Data);
            DownloaderProcess.ErrorDataReceived  += (o, e) => this.CheckIfUpdateRequired(e.Data);

            DownloaderProcess.Start();
            DownloaderProcess.BeginOutputReadLine();

            await DownloaderProcess.WaitForExitAsync();

            string  json   = StdOut.ToString();
            dynamic Result = JsonConvert.DeserializeObject <ExpandoObject>(json, new ExpandoObjectConverter());

            if (RequiresUpdateAndRetry && this.AutoUpdateAndRetry)
            {
                await this.UpdateAsync();

                return(await GetVideoInfo());
            }

            return(YoutubeMediaInfo.FromDynamicJson(Result));
        }