Exemplo n.º 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));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs youtube-dl with the given option set.
        /// </summary>
        /// <param name="urls">The video URLs passed to youtube-dl.</param>
        /// <param name="options">The OptionSet of youtube-dl options.</param>
        /// <param name="ct">A CancellationToken used to cancel the process.</param>
        /// <returns>A RunResult object containing the output of youtube-dl as an array of string.</returns>
        public async Task <RunResult <string[]> > RunWithOptions(string[] urls, OptionSet options, CancellationToken ct)
        {
            var output  = new List <string>();
            var process = new YoutubeDLProcess(YoutubeDLPath);

            process.OutputReceived     += (o, e) => output.Add(e.Data);
            (int code, string[] errors) = await runner.RunThrottled(process, urls, options, ct);

            return(new RunResult <string[]>(code == 0, errors, output.ToArray()));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Runs an update of youtube-dl.
        /// </summary>
        /// <returns>The output of youtube-dl as string.</returns>
        public async Task <string> RunUpdate()
        {
            string output  = String.Empty;
            var    process = new YoutubeDLProcess(YoutubeDLPath);

            process.OutputReceived += (o, e) => output = e.Data;
            await process.RunAsync(null, new OptionSet()
            {
                Update = true
            });

            return(output);
        }
Exemplo n.º 4
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()));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Runs a fetch of information for the given video without downloading the video.
        /// </summary>
        /// <param name="url">The URL of the video to fetch information for.</param>
        /// <param name="ct">A CancellationToken used to cancel the process.</param>
        /// <param name="flat">If set to true, does not extract information for each video in a playlist.</param>
        /// <param name="overrideOptions">Override options of the default option set for this run.</param>
        /// <returns>A RunResult object containing a VideoData object with the requested video information.</returns>
        public async Task <RunResult <VideoData> > RunVideoDataFetch(string url,
                                                                     CancellationToken ct = default, bool flat = true, OptionSet overrideOptions = null)
        {
            var opts = GetDownloadOptions();

            if (overrideOptions != null)
            {
                opts = opts.OverrideOptions(overrideOptions);
            }
            opts.DumpSingleJson = true;
            opts.FlatPlaylist   = flat;
            VideoData videoData = null;
            var       process   = new YoutubeDLProcess(YoutubeDLPath);

            process.OutputReceived     += (o, e) => videoData = JsonConvert.DeserializeObject <VideoData>(e.Data);
            (int code, string[] errors) = await runner.RunThrottled(process, new[] { url }, opts, ct);

            return(new RunResult <VideoData>(code == 0, errors, videoData));
        }