public async Task DownloadVideoAsync_UnsignedUnrestricted_Test()
 {
     var videoInfo = await _client.GetVideoInfoAsync("LsNPjFXIPT8");
     var streamInfo = videoInfo.Streams.OrderBy(s => s.FileSize).First();
     using (var stream = await _client.DownloadVideoAsync(streamInfo))
     {
         // Read some bytes
         var buffer = new byte[5];
         await stream.ReadAsync(buffer, 0, 5);
     }
 }
        public async Task DownloadVideoAsync_UnsignedUnrestricted_Test()
        {
            var videoInfo = await _client.GetVideoInfoAsync("LsNPjFXIPT8");

            foreach (var streamInfo in videoInfo.Streams)
            {
                using (var stream = await _client.DownloadVideoAsync(streamInfo))
                {
                    var buffer = new byte[100];
                    await stream.ReadAsync(buffer, 0, buffer.Length);
                }
            }
        }
Example #3
0
        private async void DownloadVideoAsync(VideoStreamInfo videoStreamInfo)
        {
            // Check params
            if (videoStreamInfo == null)
            {
                return;
            }
            if (VideoInfo == null)
            {
                return;
            }

            // Copy values
            string title = VideoInfo.Title;
            string ext   = videoStreamInfo.FileExtension;

            // Select destination
            var sfd = new SaveFileDialog
            {
                AddExtension = true,
                DefaultExt   = ext,
                FileName     = $"{title}.{ext}".Without(Path.GetInvalidFileNameChars()),
                Filter       = $"{ext.ToUpperInvariant()} Video Files|*.{ext}|All files|*.*"
            };

            if (sfd.ShowDialog() == false)
            {
                return;
            }
            string filePath = sfd.FileName;

            // Try download
            IsBusy   = true;
            Progress = 0;
            using (var output = File.Create(filePath))
                using (var input = await _client.DownloadVideoAsync(videoStreamInfo))
                {
                    // Read the response and copy it to output stream
                    var buffer = new byte[1024];
                    int bytesRead;
                    do
                    {
                        bytesRead = await input.ReadAsync(buffer, 0, buffer.Length);

                        await output.WriteAsync(buffer, 0, bytesRead);

                        if (videoStreamInfo.FileSize > 0)
                        {
                            Progress += 1.0 * bytesRead / videoStreamInfo.FileSize;
                        }
                    } while (bytesRead > 0);
                }

            Progress = 0;
            IsBusy   = false;
        }
Example #4
0
        public static void Main(string[] args)
        {
            Console.Title        = "YoutubeExplode Demo";
            Console.WindowWidth  = 86;
            Console.WindowHeight = 40;

            // Client
            var client = new YoutubeClient();

            // Get the video ID
            Console.WriteLine("Enter Youtube video ID or URL:");
            string id = Console.ReadLine();

            id = NormalizeId(id);

            Console.WriteLine("Loading . . .");
            Console.WriteLine();

            // Get the video info
            var videoInfo = client.GetVideoInfoAsync(id).Result;

            // Output some meta data
            Console.WriteLine($"{videoInfo.Title} | {videoInfo.ViewCount:N0} views | {videoInfo.AverageRating:0.##}* rating");
            Console.WriteLine("Streams:");
            for (int i = 0; i < videoInfo.Streams.Length; i++)
            {
                var    streamInfo   = videoInfo.Streams[i];
                string normFileSize = NormalizeFileSize(streamInfo.FileSize);

                // Video+audio streams (non-adaptive)
                if (streamInfo.AdaptiveMode == VideoStreamAdaptiveMode.None)
                {
                    Console.WriteLine($"\t[{i}] Mixed | {streamInfo.Type} | {streamInfo.QualityLabel} | {normFileSize}");
                }
                // Video only streams
                else if (streamInfo.AdaptiveMode == VideoStreamAdaptiveMode.Video)
                {
                    Console.WriteLine($"\t[{i}] Video | {streamInfo.Type} | {streamInfo.QualityLabel} | {streamInfo.Fps} FPS | {normFileSize}");
                }
                // Audio only streams
                else if (streamInfo.AdaptiveMode == VideoStreamAdaptiveMode.Audio)
                {
                    Console.WriteLine($"\t[{i}] Audio | {streamInfo.Type} | {normFileSize}");
                }
                // This should not happen
                else
                {
                    throw new IndexOutOfRangeException();
                }
            }

            // Get the stream index to download
            Console.WriteLine();
            Console.WriteLine("Enter corresponding number to download:");
            int streamIndex    = Console.ReadLine().ParseInt();
            var selectedStream = videoInfo.Streams[streamIndex];

            Console.WriteLine("Loading . . .");
            Console.WriteLine();

            // Compose file name, based on meta data
            string fileName = $"{videoInfo.Title}.{selectedStream.FileExtension}".Without(Path.GetInvalidFileNameChars());

            // Download video
            using (var input = client.DownloadVideoAsync(selectedStream).Result)
                using (var output = File.Create(fileName))
                    input.CopyTo(output);

            Console.WriteLine("Done!");
            Console.ReadKey();
            client.Dispose();
        }