Beispiel #1
0
        public static string GetExtForAudio(AudioConversionFormat audioConversionFormat)
        {
            switch (audioConversionFormat)
            {
            case AudioConversionFormat.Mp3:
                return("mp3");

            case AudioConversionFormat.M4a:
                return("m4a");

            case AudioConversionFormat.Vorbis:
                return("ogg");

            case AudioConversionFormat.Wav:
                return("wav");

            case AudioConversionFormat.Opus:
                return("opus");

            case AudioConversionFormat.Aac:
                return("aac");

            case AudioConversionFormat.Flac:
                return("flac");

            default:
                // Don't support 'best' because we don't know the extension in advance!
                throw new InvalidOperationException("AudioConversionFormat.Best is not supported.");
            }
        }
        /// <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));
        }
Beispiel #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;
 }
Beispiel #4
0
 public AudioConversionDownload(AudioConversionFormat format,
                                string description = null)
     : base(description, true, true)
 {
     this.ConversionFormat = format;
 }
        /// <summary>
        /// Runs a download of the specified video playlist and converts all videos to an audio format 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">The audio format the videos 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 paths to the downloaded and converted videos.</returns>
        public async Task <RunResult <string[]> > RunAudioPlaylistDownload(string url,
                                                                           int?start                 = 1, int?end = null,
                                                                           int[] items               = null, AudioConversionFormat format = AudioConversionFormat.Best,
                                                                           CancellationToken ct      = default, IProgress <DownloadProgress> progress = null,
                                                                           IProgress <string> output = null, OptionSet overrideOptions                = null)
        {
            var outputFiles = new List <string>();
            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       = "bestaudio/best";
            opts.ExtractAudio = true;
            opts.AudioFormat  = format;
            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()));
        }