支持本地m3u8的下载

This commit is contained in:
nilaoda 2022-09-27 00:55:08 +08:00
parent f8c1123d65
commit 408736d2db
4 changed files with 36 additions and 8 deletions

View File

@ -84,6 +84,10 @@ namespace N_m3u8DL_RE.Common.Util
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];
var webResponse = await DoGetAsync(url, headers);
bytes = await webResponse.Content.ReadAsByteArrayAsync();

View File

@ -73,10 +73,10 @@ namespace N_m3u8DL_RE.Parser.Processor.HLS
else if (!string.IsNullOrEmpty(uri))
{
var retryCount = parserConfig.KeyRetryCount;
var segUrl = PreProcessUrl(ParserUtil.CombineURL(m3u8Url, uri), parserConfig);
getHttpKey:
try
{
var segUrl = PreProcessUrl(ParserUtil.CombineURL(m3u8Url, uri), parserConfig);
var bytes = HTTPUtil.GetBytesAsync(segUrl, parserConfig.Headers).Result;
encryptInfo.Key = bytes;
}

View File

@ -431,7 +431,7 @@ namespace N_m3u8DL_RE.CommandLine
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,
BinaryMerge, DelAfterDone, WriteMetaJson, AppendUrlParams, ConcurrentDownload, Headers, /**SavePattern,**/ SubOnly, SubtitleFormat, AutoSubtitleFix,

View File

@ -2,14 +2,8 @@
using N_m3u8DL_RE.Common.Resource;
using N_m3u8DL_RE.Common.Util;
using N_m3u8DL_RE.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace N_m3u8DL_RE.Util
{
@ -17,9 +11,39 @@ namespace N_m3u8DL_RE.Util
{
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)
{
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));
if (fromPosition != null || toPosition != null)
request.Headers.Range = new(fromPosition, toPosition);