从 HttpResponse 保存视频文件需要很长时间才能完成 - 有没有更好的方法可以让它更快?

Saving video file from HttpResponse takes too long to finish - is there better way to make it quicker?

我目前正在尝试从 Http 响应中保存视频文件,我的方法似乎工作正常,但大型视频 (1 GB) 的处理时间太长无法完成。

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");

我想也许我可以在处理之前读取所有响应以将其复制到我的本地存储。

或者有什么更好的方法可以帮助我更快地处理视频?

使用更大的流缓冲区大小应该有助于提高 CopyToAsync() 操作的性能。但是,您可能需要测试几个不同的缓冲区大小才能找到理想值。

为此,您需要使用以下覆盖 Stream.CopyToAsync() 方法:CopyToAsync(Stream, Int32).

此外,以下类似问题可能对您的research/troubleshooting有所帮助:How can I improve the performance of this CopyTo method?