Example #1
0
        /// <summary>
        /// Downloads video and saves it to the provided videoPath folder
        /// </summary>
        /// <param name="id">ID of the video to download</param>
        /// <param name="quality">Choosen quality of the video</param>
        /// <param name="videoPath">Path where file should be saved and what file should be named with extension at the end (.mp4/.mp3).</param>
        /// <returns></returns>
        public async Task DownloadVideoAsync(string id, string quality, string videoPath)
        {
            MediaStreamInfoSet streamInfoSet = await client.GetVideoMediaStreamInfosAsync(id);

            var audioStreamInfo  = streamInfoSet.Audio.WithHighestBitrate();
            var videoStreamInfo  = streamInfoSet.Video.FirstOrDefault(c => c.VideoQualityLabel == quality);
            var mediaStreamInfos = new MediaStreamInfo[] { audioStreamInfo, videoStreamInfo };
            await converter.DownloadAndProcessMediaStreamsAsync(mediaStreamInfos, videoPath, "mp4");
        }
Example #2
0
        public async Task DownloadMedia(string id, string quality, string videoPath, string mediaType)
        {
            MediaStreamInfoSet streamInfoSet;

            streamInfoSet = await client.GetVideoMediaStreamInfosAsync(id);

            var audioStreamInfo = streamInfoSet.Audio.WithHighestBitrate();
            var videoStreamInfo = streamInfoSet.Video.FirstOrDefault(c => c.VideoQualityLabel == quality);

            if (mediaType == "mp4")
            {
                var mediaStreamInfos = new MediaStreamInfo[] { audioStreamInfo, videoStreamInfo };
                await converter.DownloadAndProcessMediaStreamsAsync(mediaStreamInfos, videoPath, "mp4");
            }
            else if (mediaType == "mp3")
            {
                var mediaStreamInfos = new MediaStreamInfo[] { audioStreamInfo };
                await converter.DownloadAndProcessMediaStreamsAsync(mediaStreamInfos, videoPath, "mp3");
            }
            else
            {
                throw new ArgumentException("mediaType not supported");
            }
        }
Example #3
0
        public async Task DownloadVideoAsync(DownloadOption downloadOption, string filePath,
                                             IProgress <double> progress, CancellationToken cancellationToken)
        {
            await EnsureThrottlingAsync(cancellationToken);

            try
            {
                await _youtubeConverter.DownloadAndProcessMediaStreamsAsync(downloadOption.StreamInfos,
                                                                            filePath, downloadOption.Format, ConversionPreset.Medium,
                                                                            progress, cancellationToken);
            }
            finally
            {
                Interlocked.Decrement(ref _concurrentDownloadCount);
            }
        }
        public async Task DownloadVideoAsync(string videoId, string filePath, DownloadOption downloadOption,
                                             IProgress <double> progress, CancellationToken cancellationToken)
        {
            // Ensure throttling and increment concurrent download count
            await EnsureThrottlingAsync(cancellationToken);

            try
            {
                // Download the video
                await _youtubeConverter.DownloadAndProcessMediaStreamsAsync(downloadOption.MediaStreamInfos,
                                                                            filePath, downloadOption.Format,
                                                                            progress, cancellationToken);
            }
            finally
            {
                // Decrement concurrent download count
                Interlocked.Decrement(ref _concurrentDownloadCount);
            }
        }
Example #5
0
 /// <summary>
 /// Downloads given media streams and processes them into a file using specified format.
 /// </summary>
 public static Task DownloadAndProcessMediaStreamsAsync(this IYoutubeConverter converter,
                                                        IReadOnlyList <IStreamInfo> streamInfos,
                                                        string filePath, string format,
                                                        IProgress <double>?progress = null, CancellationToken cancellationToken = default) =>
 converter.DownloadAndProcessMediaStreamsAsync(streamInfos, filePath, format, ConversionPreset.Medium, progress, cancellationToken);