Пример #1
0
        /// <summary>
        /// Runs a download of the specified video with and converts it to an audio format afterwards.
        /// </summary>
        /// <param name="url">The URL of the video to be downloaded.</param>
        /// <param name="format">The audio format the video will be converted to after downloaded.</param>
        /// <param name="ct">A CancellationToken used to cancel the download.</param>
        /// <param name="progress">A progress provider used to get download progress information.</param>
        /// <param name="output">A progress provider used to capture the standard output.</param>
        /// <param name="overrideOptions">Override options of the default option set for this run.</param>
        /// <returns>A RunResult object containing the path to the downloaded and converted video.</returns>
        public async Task <RunResult <string> > RunAudioDownload(string url, AudioConversionFormat format,
                                                                 CancellationToken ct      = default, IProgress <DownloadProgress> progress = null,
                                                                 IProgress <string> output = null, OptionSet overrideOptions                = null)
        {
            var opts = GetDownloadOptions();

            if (overrideOptions != null)
            {
                opts = opts.OverrideOptions(overrideOptions);
            }
            opts.Format       = "bestaudio/best";
            opts.ExtractAudio = true;
            opts.AudioFormat  = format;
            string outputFile = String.Empty;
            var    error      = new List <string>();
            var    process    = new YoutubeDLProcess(YoutubeDLPath);

            // Report the used ytdl args
            output?.Report($"Arguments: {process.ConvertToArgs(new[] { url }, opts)}\n");
            process.OutputReceived += (o, e) =>
            {
                var match = rgxFile.Match(e.Data);
                if (match.Success)
                {
                    outputFile = match.Groups[1].ToString().Trim('"');
                    progress?.Report(new DownloadProgress(DownloadState.Success, data: outputFile));
                }
                output?.Report(e.Data);
            };
            (int code, string[] errors) = await runner.RunThrottled(process, new[] { url }, opts, ct, progress);

            return(new RunResult <string>(code == 0, errors, outputFile));
        }
Пример #2
0
        /// <summary>
        /// Runs a download of the specified video playlist with an optional conversion afterwards.
        /// </summary>
        /// <param name="url">The URL of the playlist to be downloaded.</param>
        /// <param name="start">The index of the first playlist video to download (starting at 1).</param>
        /// <param name="end">The index of the last playlist video to dowload (if null, download to end).</param>
        /// <param name="items">An array of indices of playlist video to download.</param>
        /// <param name="format">A format selection string in youtube-dl style.</param>
        /// <param name="recodeFormat">The video format the output will be recoded to after download.</param>
        /// <param name="ct">A CancellationToken used to cancel the download.</param>
        /// <param name="progress">A progress provider used to get download progress information.</param>
        /// <param name="output">A progress provider used to capture the standard output.</param>
        /// <param name="overrideOptions">Override options of the default option set for this run.</param>
        /// <returns>A RunResult object containing the paths to the downloaded and converted videos.</returns>
        public async Task <RunResult <string[]> > RunVideoPlaylistDownload(string url,
                                                                           int?start     = 1, int?end = null,
                                                                           int[] items   = null,
                                                                           string format = "bestvideo+bestaudio/best",
                                                                           VideoRecodeFormat recodeFormat = VideoRecodeFormat.None,
                                                                           CancellationToken ct           = default, IProgress <DownloadProgress> progress = null,
                                                                           IProgress <string> output      = null, OptionSet overrideOptions                = null)
        {
            var opts = GetDownloadOptions();

            if (overrideOptions != null)
            {
                opts = opts.OverrideOptions(overrideOptions);
            }
            opts.NoPlaylist    = false;
            opts.PlaylistStart = start;
            opts.PlaylistEnd   = end;
            if (items != null)
            {
                opts.PlaylistItems = String.Join(",", items);
            }
            opts.Format      = format;
            opts.RecodeVideo = recodeFormat;
            var outputFiles = new List <string>();
            var process     = new YoutubeDLProcess(YoutubeDLPath);

            // Report the used ytdl args
            output?.Report($"Arguments: {process.ConvertToArgs(new[] { url }, opts)}\n");
            process.OutputReceived += (o, e) =>
            {
                var match = rgxFile.Match(e.Data);
                if (match.Success)
                {
                    var file = match.Groups[1].ToString().Trim('"');
                    outputFiles.Add(file);
                    progress?.Report(new DownloadProgress(DownloadState.Success, data: file));
                }
                output?.Report(e.Data);
            };
            (int code, string[] errors) = await runner.RunThrottled(process, new[] { url }, opts, ct, progress);

            return(new RunResult <string[]>(code == 0, errors, outputFiles.ToArray()));
        }