.HttpGet()
是我自己封装的HTTP请求方法
下载网络文件方法
public static void DownloadFile(this string url, string savePath)
{
WebClient wc = new WebClient();
var name = Path.GetFileName(url);
if (name.Contains("?"))
{
name = name.Split("?")[0];
}
if (savePath.Last() != '/')
{
savePath += "/";
}
Directory.CreateDirectory(savePath);
if(string.IsNullOrEmpty(name)) {
return;
}
wc.DownloadFile(url, savePath + name);
}
下载m3u8
string tspath = "C:/Users/Administrator/Desktop/ts/";
string mp4path = "C:/Users/Administrator/Desktop/mp4/";
string m3u8url = "https://v6.nfm3u8.com/20220802/dg9qcubn/hls/index.m3u8";
string m3u8_content = m3u8url.HttpGet();
List<string> m3u8_list = m3u8_content.Split("\n").Where(x => !x.StartsWith("#") && !string.IsNullOrEmpty(x.Trim())).ToList();
int s = 0;
int progress = 0;
foreach (string ts_url in m3u8_list) {
Task.Run(() =>
{
s++;
ts_url.DownloadFile(tspath);
progress++;
s--;
});
while (true)
{
Task.Delay(100).Wait();
if (s < 10)
{
break;
}
}
}
while (true)
{
if (progress < m3u8_list.Count)
{
Task.Delay(300).Wait();
}
else
{
break;
}
}
using (FileStream fs = new FileStream($"{mp4path}video.mp4", FileMode.Create))
{
foreach (var _u in m3u8_list)
{
var name = Path.GetFileName(_u);
using (FileStream fs1 = new FileStream(tspath+name, FileMode.Open))
{
fs1.CopyTo(fs);
}
}
}
Task.Delay(500).Wait();
foreach (string filePath in Directory.GetFiles(tspath))
{
File.Delete(filePath);
}
Console.WriteLine("下载完成:"+ $"{mp4path}video.mp4");
Console.ReadLine();