mirror of
https://github.com/NohamR/N_m3u8DL-RE.git
synced 2025-07-09 13:37:03 +00:00
支持本地m3u8的下载
This commit is contained in:
parent
f8c1123d65
commit
408736d2db
@ -84,6 +84,10 @@ namespace N_m3u8DL_RE.Common.Util
|
|||||||
|
|
||||||
public static async Task<byte[]> GetBytesAsync(string url, Dictionary<string, string>? headers = null)
|
public static async Task<byte[]> GetBytesAsync(string url, Dictionary<string, string>? headers = null)
|
||||||
{
|
{
|
||||||
|
if (url.StartsWith("file:"))
|
||||||
|
{
|
||||||
|
return await File.ReadAllBytesAsync(new Uri(url).LocalPath);
|
||||||
|
}
|
||||||
byte[] bytes = new byte[0];
|
byte[] bytes = new byte[0];
|
||||||
var webResponse = await DoGetAsync(url, headers);
|
var webResponse = await DoGetAsync(url, headers);
|
||||||
bytes = await webResponse.Content.ReadAsByteArrayAsync();
|
bytes = await webResponse.Content.ReadAsByteArrayAsync();
|
||||||
|
@ -73,10 +73,10 @@ namespace N_m3u8DL_RE.Parser.Processor.HLS
|
|||||||
else if (!string.IsNullOrEmpty(uri))
|
else if (!string.IsNullOrEmpty(uri))
|
||||||
{
|
{
|
||||||
var retryCount = parserConfig.KeyRetryCount;
|
var retryCount = parserConfig.KeyRetryCount;
|
||||||
|
var segUrl = PreProcessUrl(ParserUtil.CombineURL(m3u8Url, uri), parserConfig);
|
||||||
getHttpKey:
|
getHttpKey:
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var segUrl = PreProcessUrl(ParserUtil.CombineURL(m3u8Url, uri), parserConfig);
|
|
||||||
var bytes = HTTPUtil.GetBytesAsync(segUrl, parserConfig.Headers).Result;
|
var bytes = HTTPUtil.GetBytesAsync(segUrl, parserConfig.Headers).Result;
|
||||||
encryptInfo.Key = bytes;
|
encryptInfo.Key = bytes;
|
||||||
}
|
}
|
||||||
|
@ -431,7 +431,7 @@ namespace N_m3u8DL_RE.CommandLine
|
|||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var rootCommand = new RootCommand("N_m3u8DL-RE (Beta version) 20220925")
|
var rootCommand = new RootCommand("N_m3u8DL-RE (Beta version) 20220927")
|
||||||
{
|
{
|
||||||
Input, TmpDir, SaveDir, SaveName, BaseUrl, ThreadCount, DownloadRetryCount, AutoSelect, SkipMerge, SkipDownload, CheckSegmentsCount,
|
Input, TmpDir, SaveDir, SaveName, BaseUrl, ThreadCount, DownloadRetryCount, AutoSelect, SkipMerge, SkipDownload, CheckSegmentsCount,
|
||||||
BinaryMerge, DelAfterDone, WriteMetaJson, AppendUrlParams, ConcurrentDownload, Headers, /**SavePattern,**/ SubOnly, SubtitleFormat, AutoSubtitleFix,
|
BinaryMerge, DelAfterDone, WriteMetaJson, AppendUrlParams, ConcurrentDownload, Headers, /**SavePattern,**/ SubOnly, SubtitleFormat, AutoSubtitleFix,
|
||||||
|
@ -2,14 +2,8 @@
|
|||||||
using N_m3u8DL_RE.Common.Resource;
|
using N_m3u8DL_RE.Common.Resource;
|
||||||
using N_m3u8DL_RE.Common.Util;
|
using N_m3u8DL_RE.Common.Util;
|
||||||
using N_m3u8DL_RE.Entity;
|
using N_m3u8DL_RE.Entity;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace N_m3u8DL_RE.Util
|
namespace N_m3u8DL_RE.Util
|
||||||
{
|
{
|
||||||
@ -17,9 +11,39 @@ namespace N_m3u8DL_RE.Util
|
|||||||
{
|
{
|
||||||
private static readonly HttpClient AppHttpClient = HTTPUtil.AppHttpClient;
|
private static readonly HttpClient AppHttpClient = HTTPUtil.AppHttpClient;
|
||||||
|
|
||||||
|
private static async Task<DownloadResult> CopyFileAsync(string sourceFile, string path, SpeedContainer speedContainer, long? fromPosition = null, long? toPosition = null)
|
||||||
|
{
|
||||||
|
using var inputStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
|
using var outputStream = new FileStream(path, FileMode.OpenOrCreate);
|
||||||
|
inputStream.Seek(fromPosition ?? 0L, SeekOrigin.Begin);
|
||||||
|
var expect = (toPosition ?? inputStream.Length) - inputStream.Position + 1;
|
||||||
|
if (expect == inputStream.Length + 1)
|
||||||
|
{
|
||||||
|
await inputStream.CopyToAsync(outputStream);
|
||||||
|
speedContainer.Add(inputStream.Length);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var buffer = new byte[expect];
|
||||||
|
await inputStream.ReadAsync(buffer);
|
||||||
|
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||||
|
speedContainer.Add(buffer.Length);
|
||||||
|
}
|
||||||
|
return new DownloadResult()
|
||||||
|
{
|
||||||
|
ActualContentLength = outputStream.Length,
|
||||||
|
ActualFilePath = path
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public static async Task<DownloadResult> DownloadToFileAsync(string url, string path, SpeedContainer speedContainer, Dictionary<string, string>? headers = null, long? fromPosition = null, long? toPosition = null)
|
public static async Task<DownloadResult> DownloadToFileAsync(string url, string path, SpeedContainer speedContainer, Dictionary<string, string>? headers = null, long? fromPosition = null, long? toPosition = null)
|
||||||
{
|
{
|
||||||
Logger.Debug(ResString.fetch + url);
|
Logger.Debug(ResString.fetch + url);
|
||||||
|
if (url.StartsWith("file:"))
|
||||||
|
{
|
||||||
|
var file = new Uri(url).LocalPath;
|
||||||
|
return await CopyFileAsync(file, path, speedContainer, fromPosition, toPosition);
|
||||||
|
}
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
|
using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
|
||||||
if (fromPosition != null || toPosition != null)
|
if (fromPosition != null || toPosition != null)
|
||||||
request.Headers.Range = new(fromPosition, toPosition);
|
request.Headers.Range = new(fromPosition, toPosition);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user