omansak / libvideo

A lightweight .NET library to download YouTube videos.
BSD 2-Clause "Simplified" License
553 stars 164 forks source link

Corrupted File after download video #246

Closed ArataKaito closed 2 years ago

ArataKaito commented 2 years ago

Hello,

i have a problem with the sample chunk download. it downloades the file but after it finished its corrptued and isn't running.

` public async void OnlyVideo_Cunks() {

        try
        {

            var client = new HttpClient();

            foreach (var link in youtubeURLS)
            {
                try
                {
                    var youTube = YouTube.Default;
                    var videoInfo = await youTube.GetVideoAsync(link);
                    var maxRes = videoInfo.Resolution == 720;

                    //var videoInfos = youTube.GetAllVideosAsync(link).GetAwaiter().GetResult(); //Holt sich die Informationen über das Video.
                    //var maxResolution = videoInfos.First(i => i.Resolution == 720); //Holt

                    using (Stream output = File.OpenWrite(destinationPath + @"\" + videoInfo.FullName))
                    {
                        //byte[] bytesInStream = new byte[16 * 1024];
                        //int read2;

                        //do
                        //{
                        //    read2 = output.Read(bytesInStream, 0, (int)bytesInStream.Length);
                        //    if (read2 > 0)
                        //        output.Write(bytesInStream, 0, read2);  
                        //}

                        //while (read2 > 0);

                        using (var input = await client.GetStreamAsync(videoInfo.Uri))
                        {
                            byte[] buffer = new byte[64 * 1024];
                            int read;
                            int totalRead = 0;

                            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                output.Write(buffer, 0, read);
                                totalRead += read;
                            }
                        }

                    }

                    MessageBox.Show("Fertig!");

                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }

            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }`
omansak commented 2 years ago

A sample code :

https://github.com/omansak/libvideo/blob/e9439680271d1d92398d90132757d6c95e578f31/src/libvideo.debug/CustomYoutubeClient.cs#L39