You've already forked N_m3u8DL-CLI
mirror of
https://github.com/nilaoda/N_m3u8DL-CLI
synced 2025-10-03 07:40:49 +02:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0bd23ab641 | ||
![]() |
a38f27ccd7 | ||
![]() |
3acec5efd3 | ||
![]() |
90874e4bfe | ||
![]() |
b94768e3e8 |
@@ -30,8 +30,8 @@ namespace N_m3u8DL_CLI
|
||||
|
||||
|
||||
/*===============================================================================*/
|
||||
static string nowVer = "2.8.1";
|
||||
static string nowDate = "20201121";
|
||||
static string nowVer = "2.8.2";
|
||||
static string nowDate = "20201122";
|
||||
public static void WriteInit()
|
||||
{
|
||||
Console.Clear();
|
||||
@@ -101,18 +101,20 @@ namespace N_m3u8DL_CLI
|
||||
public static string GetWebSource(String url, string headers = "", int TimeOut = 60000)
|
||||
{
|
||||
string htmlCode = string.Empty;
|
||||
for(int i = 0; i < 10; i++)
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
reProcess:
|
||||
HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
|
||||
webRequest.Method = "GET";
|
||||
if (NoProxy) webRequest.Proxy = null;
|
||||
webRequest.UserAgent = "Mozilla/5.0 (Linux; U; Android 7.0; zh-cn; 15 Plus Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/9.4 Mobile Safari/537.36";
|
||||
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
|
||||
webRequest.Accept = "*/*";
|
||||
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate, br");
|
||||
webRequest.Timeout = TimeOut; //设置超时
|
||||
webRequest.KeepAlive = false;
|
||||
webRequest.AllowAutoRedirect = true; //自动跳转
|
||||
webRequest.AllowAutoRedirect = false; //手动处理重定向,否则会丢失Referer
|
||||
if (url.Contains("pcvideo") && url.Contains(".titan.mgtv.com"))
|
||||
{
|
||||
webRequest.UserAgent = "";
|
||||
@@ -146,6 +148,14 @@ namespace N_m3u8DL_CLI
|
||||
}
|
||||
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
|
||||
|
||||
//302
|
||||
if (webResponse.Headers.Get("Location") != null)
|
||||
{
|
||||
url = webResponse.Headers.Get("Location");
|
||||
webResponse.Close();
|
||||
goto reProcess;
|
||||
}
|
||||
|
||||
//文件过大则认为不是m3u8
|
||||
if (webResponse.ContentLength != -1 && webRequest.ContentLength > 50 * 1024 * 1024) return "";
|
||||
|
||||
@@ -406,14 +416,17 @@ namespace N_m3u8DL_CLI
|
||||
}
|
||||
}
|
||||
|
||||
reProcess:
|
||||
byte[] arraryByte;
|
||||
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
||||
req.Method = "GET";
|
||||
req.Timeout = timeOut;
|
||||
req.ReadWriteTimeout = timeOut; //重要
|
||||
req.AllowAutoRedirect = false; //手动处理重定向,否则会丢失Referer
|
||||
if (NoProxy) req.Proxy = null;
|
||||
req.Headers.Add("Accept-Encoding", "gzip, deflate");
|
||||
req.Accept = "*/*";
|
||||
req.UserAgent = "Mozilla/5.0 (Linux; U; Android 7.0; zh-cn; 15 Plus Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 MQQBrowser/9.4 Mobile Safari/537.36";
|
||||
//添加headers
|
||||
if (headers != "")
|
||||
{
|
||||
@@ -441,6 +454,13 @@ namespace N_m3u8DL_CLI
|
||||
|
||||
using (HttpWebResponse wr = (HttpWebResponse)req.GetResponse())
|
||||
{
|
||||
//302
|
||||
if (wr.Headers.Get("Location") != null)
|
||||
{
|
||||
url = wr.Headers.Get("Location");
|
||||
wr.Close();
|
||||
goto reProcess;
|
||||
}
|
||||
if (wr.ContentEncoding != null && wr.ContentEncoding.ToLower() == "gzip") //如果使用了GZip则先解压
|
||||
{
|
||||
using (Stream streamReceive = wr.GetResponseStream())
|
||||
@@ -501,10 +521,11 @@ namespace N_m3u8DL_CLI
|
||||
if (shouldStop)
|
||||
return;
|
||||
|
||||
reProcess:
|
||||
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
|
||||
request.Timeout = timeOut;
|
||||
request.ReadWriteTimeout = timeOut; //重要
|
||||
request.AllowAutoRedirect = true;
|
||||
request.AllowAutoRedirect = false; //手动处理重定向,否则会丢失Referer
|
||||
request.KeepAlive = false;
|
||||
request.Method = "GET";
|
||||
if (NoProxy) request.Proxy = null;
|
||||
@@ -552,6 +573,13 @@ namespace N_m3u8DL_CLI
|
||||
bool pngHeader = false; //PNG HEADER检测
|
||||
using (var response = (HttpWebResponse)request.GetResponse())
|
||||
{
|
||||
//302
|
||||
if (response.Headers.Get("Location") != null)
|
||||
{
|
||||
url = response.Headers.Get("Location");
|
||||
response.Close();
|
||||
goto reProcess;
|
||||
}
|
||||
using (var responseStream = response.GetResponseStream())
|
||||
{
|
||||
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
|
||||
@@ -560,7 +588,7 @@ namespace N_m3u8DL_CLI
|
||||
totalLen = response.ContentLength;
|
||||
byte[] bArr = new byte[1024];
|
||||
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
|
||||
if (!pngHeader && size > 3 && 137 == bArr[0] && 80 == bArr[1] && 78 == bArr[2] && 71 == bArr[3])
|
||||
if (!pngHeader && size > 3 && 137 == bArr[0] && 80 == bArr[1] && 78 == bArr[2] && 71 == bArr[3])
|
||||
{
|
||||
pngHeader = true;
|
||||
}
|
||||
@@ -593,10 +621,11 @@ namespace N_m3u8DL_CLI
|
||||
}
|
||||
if (shouldStop)
|
||||
try { File.Delete(path); } catch (Exception) { }
|
||||
if (totalLen != -1 && downLen != totalLen)
|
||||
if (totalLen != -1 && downLen != totalLen)
|
||||
try { File.Delete(path); } catch (Exception) { }
|
||||
if (pngHeader)
|
||||
TrySkipPngHeader(path);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@@ -112,8 +112,9 @@ namespace N_m3u8DL_CLI
|
||||
/// <param name="downDir">文件存储目录</param>
|
||||
/// <param name="mpdUrl">MPD链接</param>
|
||||
/// <param name="mpdContent">MPD内容</param>
|
||||
/// <param name="customBase">自定义BaseUrl</param>
|
||||
/// <returns></returns>
|
||||
public static string Parse(string downDir, string mpdUrl, string mpdContent)
|
||||
public static string Parse(string downDir, string mpdUrl, string mpdContent, string customBase = "")
|
||||
{
|
||||
XmlDocument mpdDoc = new XmlDocument();
|
||||
mpdDoc.LoadXml(mpdContent);
|
||||
@@ -193,7 +194,7 @@ namespace N_m3u8DL_CLI
|
||||
return url;
|
||||
}
|
||||
|
||||
var mpdBaseUrl = GetBaseUrl(mpdUrl);
|
||||
var mpdBaseUrl = string.IsNullOrEmpty(customBase) ? GetBaseUrl(mpdUrl) : customBase;
|
||||
if (!string.IsNullOrEmpty(mpdBaseUrl) && !CheckBaseUrl())
|
||||
{
|
||||
if (!mpdBaseUrl.EndsWith("/") && !baseUrl.StartsWith("/"))
|
||||
|
@@ -125,7 +125,7 @@ namespace N_m3u8DL_CLI
|
||||
File.WriteAllText(mpdSavePath, m3u8Content);
|
||||
//分析mpd文件
|
||||
M3u8Url = Global.Get302(M3u8Url, Headers);
|
||||
var newUri = MPDParser.Parse(DownDir, M3u8Url, m3u8Content);
|
||||
var newUri = MPDParser.Parse(DownDir, M3u8Url, m3u8Content, BaseUrl);
|
||||
M3u8Url = newUri;
|
||||
m3u8Content = File.ReadAllText(new Uri(M3u8Url).LocalPath);
|
||||
}
|
||||
@@ -332,7 +332,7 @@ namespace N_m3u8DL_CLI
|
||||
else if (line.StartsWith(HLSTags.ext_x_i_frame_stream_inf)) ;
|
||||
else if (line.StartsWith(HLSTags.ext_x_media))
|
||||
{
|
||||
if (Global.GetTagAttribute(line, "TYPE") == "AUDIO")
|
||||
if (Global.GetTagAttribute(line, "TYPE") == "AUDIO" && !MEDIA_AUDIO.ContainsKey(Global.GetTagAttribute(line, "GROUP-ID")))
|
||||
MEDIA_AUDIO.Add(Global.GetTagAttribute(line, "GROUP-ID"), CombineURL(BaseUrl, Global.GetTagAttribute(line, "URI")));
|
||||
if (Global.GetTagAttribute(line, "TYPE") == "SUBTITLES")
|
||||
{
|
||||
|
@@ -284,4 +284,6 @@
|
||||
2020年11月20日
|
||||
- 识别大部分mpd地址,自动转换为m3u8并下载
|
||||
- GIF HEADER检测
|
||||
- 修复BUG
|
||||
- 修复BUG
|
||||
2020年11月22日
|
||||
- 解决HTTPS协议自动重定向后,Referer丢失问题
|
Reference in New Issue
Block a user