Home > Software engineering >  Saving video file from HttpResponse takes too long to finish - is there better way to make it quicke
Saving video file from HttpResponse takes too long to finish - is there better way to make it quicke

Time:09-29

I'm currently trying to save video file from a Http response and my method seems to work fine except the processing time for large video (1 GB) takes too long to finish.

using (HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse())
using (Stream mystream = wresp.GetResponseStream())
{
    using (var f = new FileStream(Path.Combine(@"D:/Games/", "Test1.mp4"), FileMode.Create, FileAccess.Write))
    {
        f.Position = 0;
        await mystream.CopyToAsync(f);
    }
}
Console.WriteLine("Finished");

I think maybe I can just read all the response before processing to copy it to my local storage.

Or is there any better way that help me to process the video more quickly?

CodePudding user response:

Using a larger stream buffer size should help increase the performance of the CopyToAsync() operation. However, you will likely need to test a few different buffer sizes in order to find the ideal value.

In order to do this, you will need to use the following override of the Stream.CopyToAsync() method: CopyToAsync(Stream, Int32).

Additionally, the following similar question may help in your research/troubleshooting: How can I improve the performance of this CopyTo method?.

  • Related