Пример #1
0
 async Task DownloadAsync()
 {
     byte[] file = Array.Empty <byte>();
     // Generate a text file
     if (ToDownload is not null)
     {
         file = System.Text.Encoding.UTF8.GetBytes(JsonSerializer.Serialize <T>(ToDownload));
     }
     if (BlazorDownloadFileService is not null)
     {
         await BlazorDownloadFileService.DownloadFileAsync(Filename, file);
     }
 }
Пример #2
0
        async Task DownloadFileAsync()
        {
            if (CheckedAudioOnlyStreamInfoHashCode == NoSelection)
            {
                return;
            }

            Progress = 0;
            DownloadButtonEnabled = false;

            try
            {
                var streamInfo = AudioOnlyStreamInfos.Single(x => x.GetHashCode() == CheckedAudioOnlyStreamInfoHashCode);

                var x = new Stopwatch();

                x.Reset();
                x.Start();

                // bool oggOpusIsExtractedByServer = false;
                Stream stream;
                if (streamInfo.IsOpus() && ExtractOpus)
                {
                    stream = await YouTubeDownloadApi.GetOggOpusAudioStreamAsync(streamInfo);

                    // oggOpusIsExtractedByServer = true;
                    x.Stop();
                    Console.WriteLine("GetOggOpusAudioStreamAsync = " + x.Elapsed);
                }
                else
                {
                    stream = await YouTubeDownloadApi.GetAudioStreamAsync(streamInfo);

                    x.Stop();
                    Console.WriteLine("GetAudioStreamAsync = " + x.Elapsed);
                }

                var filename = GetFileNameWithExtension(streamInfo);

                x.Reset();
                x.Start();
                using var memoryStream = new MemoryStream();

                long lastValue = 0;
                await stream.CopyToAsync(memoryStream, async (e) =>
                {
                    Progress = 100 * e.TotalBytesRead / e.SourceLength;

                    if (Progress != lastValue)
                    {
                        lastValue = Progress;
                        StateHasChanged();
                        await Task.Delay(5); // give the UI some time to catch up
                    }
                });

                x.Stop();
                Console.WriteLine("CopyToAsync = " + x.Elapsed);

                //byte[] array;
                //if (OpusAudioStreamPresent && ExtractOpus && !oggOpusIsExtractedByServer)
                //{
                //    using var opusStream = new MemoryStream();

                //    x.Start();
                //    memoryStream.Position = 0;
                //    MatroskaDemuxer.ExtractOggOpusAudio(memoryStream, opusStream);

                //    Console.WriteLine("ExtractOggOpusAudio and ToArray = " + x.Elapsed);

                //    array = opusStream.ToArray();
                //    x.Stop();
                //}
                //else
                //{
                //    x.Start();
                //    array = memoryStream.ToArray();
                //    x.Stop();
                //    Console.WriteLine("ToArray = " + x.Elapsed);
                //}

                x.Reset();
                x.Start();
                byte[] array = memoryStream.ToArray();
                x.Stop();
                Console.WriteLine("ToArray = " + x.Elapsed);

                x.Reset();
                x.Start();
                await BlazorDownloadFileService.DownloadFileAsync(filename, array);

                x.Stop();

                Console.WriteLine("Download = " + x.Elapsed);
            }
            finally
            {
                DownloadButtonEnabled = true;
            }
        }