Example #1
0
        public static string GetExtForVideo(VideoRecodeFormat videoRecodeFormat)
        {
            switch (videoRecodeFormat)
            {
            case VideoRecodeFormat.Avi:
                return("avi");

            case VideoRecodeFormat.Mp4:
                return("mp4");

            case VideoRecodeFormat.Ogg:
                return("ogg");

            case VideoRecodeFormat.Flv:
                return("flv");

            case VideoRecodeFormat.Webm:
                return("webm");

            case VideoRecodeFormat.Mkv:
                return("mkv");

            default:
                // Don't support 'None' because we don't know the extension in advance!
                throw new InvalidOperationException("VideoRecodeFormat.None is not supported.");
            }
        }
Example #2
0
        /// <summary>
        /// Runs a download of the specified video with an optional conversion afterwards.
        /// </summary>
        /// <param name="url">The URL of the video to be downloaded.</param>
        /// <param name="format">A format selection string in youtube-dl style.</param>
        /// <param name="mergeFormat">If a merge is required, the container format of the merged downloads.</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 path to the downloaded and converted video.</returns>
        public async Task <RunResult <string> > RunVideoDownload(string url,
                                                                 string format = "bestvideo+bestaudio/best",
                                                                 DownloadMergeFormat mergeFormat = DownloadMergeFormat.Unspecified,
                                                                 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.Format            = format;
            opts.MergeOutputFormat = mergeFormat;
            opts.RecodeVideo       = recodeFormat;
            string outputFile = String.Empty;
            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));
        }
Example #3
0
 public void Configure(
     FormatData videoFormat, FormatData audioFormat = null,
     bool extractAudio = false,
     AudioConversionFormat audioConversionFormat = AudioConversionFormat.Mp3,
     VideoRecodeFormat videoRecodeFormat         = VideoRecodeFormat.None)
 {
     if (videoFormat == null && audioFormat == null)
     {
         throw new InvalidOperationException(Resources.DownloadOption_Exception_NoFormat);
     }
     if (extractAudio && audioConversionFormat == AudioConversionFormat.Best)
     {
         throw new InvalidOperationException("Cannot use AudioConversionFormat.Best.");
     }
     if (audioFormat != null && !extractAudio && videoRecodeFormat == VideoRecodeFormat.None)
     {
         throw new InvalidOperationException(Resources.DownloadOption_Exception_RecodeFormat);
     }
     if (audioFormat == null && videoFormat.AudioCodec == "none" && extractAudio)
     {
         throw new InvalidOperationException(Resources.DownloadOption_Exception_NoAudio);
     }
     this.VideoFormat           = videoFormat;
     this.AudioFormat           = audioFormat;
     this.IsAudio               = extractAudio;
     this.AudioConversionFormat = audioConversionFormat;
     this.VideoRecodeFormat     = videoRecodeFormat;
 }
Example #4
0
 public VideoDownload(string formatSelection,
                      VideoRecodeFormat recodeFormat = VideoRecodeFormat.None,
                      string description             = null, string fileExtension = "")
     : base(description, false, !String.IsNullOrEmpty(fileExtension) || recodeFormat != VideoRecodeFormat.None)
 {
     this.FormatSelection = formatSelection;
     this.RecodeFormat    = recodeFormat;
     this.fileExtension   = fileExtension;
 }
Example #5
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()));
        }