コード例 #1
0
        public async Task <VideoDetails> GetVideoMetadataAsync(string videoID)
        {
            var video = await client.GetVideoAsync(videoID);

            MediaStreamInfoSet streamInfoSet = await client.GetVideoMediaStreamInfosAsync(videoID);

            IEnumerable <string> qualities = SortQualities(streamInfoSet.GetAllVideoQualityLabels());

            return(new VideoDetails()
            {
                id = videoID, ChannelName = video.Author, Title = video.Title, qualities = qualities, thumbnails = video.Thumbnails
            });
        }
コード例 #2
0
        public static async Task DownloadMusic(string url, FileInfo desinationFile)
        {
            if (desinationFile.Exists)
            {
                Console.WriteLine($"\t{desinationFile.Name} already downloaded. Skipping...");
                return;
            }
            Console.Write($"\t-Downloading {desinationFile.Name}...");
            //Get video
            var vidId      = YoutubeClient.ParseVideoId(url);
            var streamInfo = await _client.GetVideoMediaStreamInfosAsync(vidId);

            var stream            = streamInfo.Audio.WithHighestBitrate();
            var ext               = stream.Container.GetFileExtension();
            var videoDownloadFile = Path.ChangeExtension(Path.GetTempFileName(), ext);
            await _client.DownloadMediaStreamAsync(stream, videoDownloadFile);

            //Write video to file
            Console.Write($"Done. Converting to mp3...");
            //Extract mp3 from video
            var result = await Conversion.Convert(videoDownloadFile, desinationFile.FullName)
                         .Start();

            Console.WriteLine($"Done");
        }
コード例 #3
0
        public async Task <string> Download(string url, Action <int> progressCallback, string newPath, bool onlyMusic)
        {
            string result        = null;
            var    streamInfoSet = await client.GetVideoMediaStreamInfosAsync(url);

            MediaStreamInfo stream = null;

            if (onlyMusic)
            {
                stream = streamInfoSet.Audio.WithHighestBitrate();
            }
            else
            {
                stream = streamInfoSet.Muxed.WithHighestVideoQuality();
            }

            var fileExtension = stream.Container.GetFileExtension();

            result = newPath + "." + fileExtension;
            var fileStream = File.Create(result);

            var progress = new DownladProgress(progressCallback);

            await client.DownloadMediaStreamAsync(stream, fileStream, progress);

            return(result);
        }
コード例 #4
0
        /// <inheritdoc />
        public async Task DownloadVideoAsync(string videoId, string filePath, string format,
                                             IProgress <double>?progress = null, CancellationToken cancellationToken = default)
        {
            // Get stream info set
            var mediaStreamInfoSet = await _youtubeClient.GetVideoMediaStreamInfosAsync(videoId);

            // Download video with known stream info set
            await DownloadVideoAsync(mediaStreamInfoSet, filePath, format, progress, cancellationToken);
        }
コード例 #5
0
        public async Task <string> GetAudioFileUrl(string id)
        {
            var mediaStreams = await _youtubeClient.GetVideoMediaStreamInfosAsync(id);

            var audioStream = mediaStreams.Audio
                              .Where(s => s.Container == Container.Mp4)
                              .MaxBy(s => s.Bitrate)
                              .First();

            return(audioStream.Url);
        }
コード例 #6
0
        /// <inheritdoc />
        public async Task DownloadVideoAsync(string videoId, string filePath, string format,
                                             TimeSpan startTs                    = default(TimeSpan), TimeSpan takeTs = default(TimeSpan),
                                             IProgress <double>?progress         = null,
                                             CancellationToken cancellationToken = default)
        {
            // Get stream info set
            var mediaStreamInfoSet = await _youtubeClient.GetVideoMediaStreamInfosAsync(videoId)
                                     .ConfigureAwait(false);

            // Download video with known stream info set
            await DownloadVideoAsync(mediaStreamInfoSet, filePath, format, startTs, takeTs, progress, cancellationToken);
        }
コード例 #7
0
        /// <inheritdoc />
        public async Task DownloadVideoAsync(string videoId, string filePath, string format,
                                             IProgress <double> progress         = null,
                                             CancellationToken cancellationToken = default(CancellationToken))
        {
            videoId.GuardNotNull(nameof(videoId));
            filePath.GuardNotNull(nameof(filePath));
            format.GuardNotNull(nameof(format));

            // Get stream info set
            var mediaStreamInfoSet = await _youtubeClient.GetVideoMediaStreamInfosAsync(videoId)
                                     .ConfigureAwait(false);

            // Download video with known stream info set
            await DownloadVideoAsync(mediaStreamInfoSet, filePath, format, progress, cancellationToken)
            .ConfigureAwait(false);
        }
コード例 #8
0
        private static void DownloadVideo(IYoutubeClient client, Video video, string downloadFolder, string videoFileNameBase, IProgress <double> progress)
        {
            var converter = new YoutubeConverter(client, Ffmpeg.DefaultFilePath);

            var mediaStreamInfoSet = client.GetVideoMediaStreamInfosAsync(video.Id).Result;
            var videoStreamInfo    = mediaStreamInfoSet.Video.OrderByDescending(info => info.VideoQuality).ThenByDescending(info => info.Framerate).First();
            var audioStreamInfo    = mediaStreamInfoSet.Audio.OrderByDescending(info => info.Bitrate).First();

            var extension = videoStreamInfo.Container.GetFileExtension();

            converter.DownloadAndProcessMediaStreamsAsync(
                new MediaStreamInfo[] { videoStreamInfo, audioStreamInfo },
                Path.Combine(downloadFolder, videoFileNameBase + $".{extension}"),
                extension,
                progress)
            .Wait();
        }
コード例 #9
0
        public async Task <IReadOnlyList <DownloadOption> > GetDownloadOptionsAsync(string videoId)
        {
            var result = new List <DownloadOption>();

            // Get media stream info set
            var mediaStreamInfoSet = await _youtubeClient.GetVideoMediaStreamInfosAsync(videoId);

            // Sort video streams
            var videoStreamInfos = mediaStreamInfoSet.Video
                                   .OrderByDescending(s => s.VideoQuality)
                                   .ThenByDescending(s => s.Framerate)
                                   .ToArray();

            // Add video download options
            foreach (var videoStreamInfo in videoStreamInfos)
            {
                // Get format
                var format = videoStreamInfo.Container.GetFileExtension();

                // Get best audio stream, preferably with the same container
                var audioStreamInfo = mediaStreamInfoSet.Audio
                                      .OrderByDescending(s => s.Container == videoStreamInfo.Container)
                                      .ThenByDescending(s => s.Bitrate)
                                      .FirstOrDefault();

                // Add to list
                result.Add(new DownloadOption(format, audioStreamInfo, videoStreamInfo));
            }

            // Add audio-only download options
            {
                // Get best audio stream, preferably with webm container
                var audioStreamInfo = mediaStreamInfoSet.Audio
                                      .OrderByDescending(s => s.Container == Container.WebM)
                                      .ThenByDescending(s => s.Bitrate)
                                      .FirstOrDefault();

                // Add to list
                result.Add(new DownloadOption("mp3", audioStreamInfo));
                result.Add(new DownloadOption("ogg", audioStreamInfo));
            }

            return(result);
        }