Skip to content

Commit 32b11df

Browse files
committed
fmp4合并功能测试完成
1 parent 21b6621 commit 32b11df

File tree

15 files changed

+146
-80
lines changed

15 files changed

+146
-80
lines changed

M3u8Downloader_H.Combiners/M3uCombiners/M3uCombiner.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using M3u8Downloader_H.Abstractions.Common;
22
using M3u8Downloader_H.Abstractions.Settings;
33
using M3u8Downloader_H.Abstractions.M3u8;
4+
using System.Diagnostics;
45

56
namespace M3u8Downloader_H.Combiners.M3uCombiners
67
{
@@ -33,7 +34,7 @@ public async ValueTask StartMerging(IM3uFileInfo m3UFileInfo, CancellationToken
3334
{
3435
cancellationToken.ThrowIfCancellationRequested();
3536
await MegerVideoInternalAsync(videoFileStream, m3UFileInfo.MediaFiles[i], cancellationToken);
36-
DialogProgress.Report(i / (double)m3UFileInfo.MediaFiles.Count);
37+
DialogProgress.Report((double)i / m3UFileInfo.MediaFiles.Count);
3738
}
3839
catch (Exception) when (Settings.ForcedMerger)
3940
{

M3u8Downloader_H.Common/DownloadPrams/DownloadParamsBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace M3u8Downloader_H.Common.DownloadPrams
55
{
66
public class DownloadParamsBase : IDownloadParamBase
77
{
8-
private static readonly string _cachePath =
8+
private static readonly string _defaultCachePath =
99
#if DEBUG
1010
"E:\\desktop\\download\\Caches";
1111
#else
@@ -51,7 +51,7 @@ public DownloadParamsBase(Uri uri, string? videoName, string? cachePath, string
5151
if (!string.IsNullOrWhiteSpace(cachePath))
5252
CachePath = cachePath;
5353
else
54-
CachePath = Path.Combine(_cachePath, _cacheName);
54+
CachePath = Path.Combine(_defaultCachePath, _cacheName);
5555

5656
VideoName = videoName!;
5757
SelectFormats = selectFormat;

M3u8Downloader_H.Common/Extensions/CryptExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace M3u8Downloader_H.Common.Extensions
66
{
77
public static class CryptExtensions
88
{
9-
private static readonly Dictionary<string, (int, int)> KeyGroup = new() { { "AES-128", (16, 24) }, { "AES-192", (24, 32) }, { "AES-256", (32, 44) } };
9+
//private static readonly Dictionary<string, (int, int)> KeyGroup = new() { { "AES-128", (16, 24) }, { "AES-192", (24, 32) }, { "AES-256", (32, 44) } };
1010

1111
public static byte[] HmacSha256(this Stream memory, byte[] key)
1212
{
@@ -69,7 +69,7 @@ public static Stream AesDecrypt(this Stream memory, byte[] aesKey, byte[] aesIV)
6969
return new CryptoStream(memory, decryptor, CryptoStreamMode.Read);
7070
}
7171

72-
public static byte[] TryParseKey(this byte[] data, string method)
72+
/* public static byte[] TryParseKey(this byte[] data, string method)
7373
{
7474
string tmpMethod = string.IsNullOrWhiteSpace(method) ? "AES-128" : method.ToUpper(CultureInfo.CurrentCulture).Trim();
7575
if (KeyGroup.TryGetValue(tmpMethod, out (int, int) tmpKey))
@@ -83,6 +83,6 @@ public static byte[] TryParseKey(this byte[] data, string method)
8383
}
8484
}
8585
throw new InvalidCastException("无法解析的密钥,请确定是否为AES-128,AES-192,AES-256");
86-
}
86+
}*/
8787
}
8888
}
Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Globalization;
42
using System.Text;
5-
using System.Threading.Tasks;
63
using M3u8Downloader_H.Abstractions.M3u8;
74
using M3u8Downloader_H.Common.Extensions;
85

96
namespace M3u8Downloader_H.Common.M3u8
107
{
118
public class M3uKeyInfoHelper(string method, byte[] bytes, byte[] iv) : IM3uKeyInfo
129
{
10+
private static readonly Dictionary<string, (int, int)> KeyGroup = new() { { "AES-128", (16, 24) }, { "AES-192", (24, 32) }, { "AES-256", (32, 44) } };
11+
1312
public string Method { get; } = method;
1413

1514
public Uri Uri { get; } = default!;
@@ -20,24 +19,57 @@ public class M3uKeyInfoHelper(string method, byte[] bytes, byte[] iv) : IM3uKeyI
2019

2120
public static IM3uKeyInfo GetKeyInfoInstance(string method, byte[] bKey , byte[]? iv)
2221
{
23-
byte[] data = bKey.TryParseKey(method);
22+
byte[] data = TryParseKey(method, bKey);
2423
return new M3uKeyInfoHelper(method, data, iv!);
2524
}
2625

2726
public static IM3uKeyInfo GetKeyInfoInstance(IM3uKeyInfo m3UKeyInfo)
2827
{
29-
byte[] data = m3UKeyInfo.BKey.TryParseKey(m3UKeyInfo.Method);
28+
byte[] data = TryParseKey(m3UKeyInfo.Method, m3UKeyInfo.BKey);
3029
return new M3uKeyInfoHelper(m3UKeyInfo.Method, data, m3UKeyInfo.IV);
3130
}
3231

3332
public static IM3uKeyInfo GetKeyInfoInstance(string method, string key)
3433
{
35-
return new M3uKeyInfoHelper(method, Encoding.UTF8.GetBytes(key), null!);
34+
byte[] data = TryParseKey(method, key);
35+
return new M3uKeyInfoHelper(method, data, null!);
3636
}
3737

3838
public static IM3uKeyInfo GetKeyInfoInstance(string method, string key, string iv)
3939
{
40-
return new M3uKeyInfoHelper(method, Encoding.UTF8.GetBytes(key), iv?.ToHex()!);
40+
byte[] data = TryParseKey(method, key);
41+
return new M3uKeyInfoHelper(method, data, iv?.ToHex()!);
42+
}
43+
44+
private static byte[] TryParseKey(string method,byte[] data)
45+
{
46+
string tmpMethod = string.IsNullOrWhiteSpace(method) ? "AES-128" : method.ToUpper(CultureInfo.CurrentCulture).Trim();
47+
if (KeyGroup.TryGetValue(tmpMethod, out (int, int) tmpKey))
48+
{
49+
if (data.Length == tmpKey.Item1)
50+
return data;
51+
else if (data.Length == tmpKey.Item2)
52+
{
53+
var stringdata = Encoding.UTF8.GetString(data);
54+
return Convert.FromBase64String(stringdata);
55+
}
56+
}
57+
throw new InvalidCastException("无法解析的密钥,请确定是否为AES-128,AES-192,AES-256");
58+
}
59+
60+
private static byte[] TryParseKey(string method, string data)
61+
{
62+
string tmpMethod = string.IsNullOrWhiteSpace(method) ? "AES-128" : method.ToUpper(CultureInfo.CurrentCulture).Trim();
63+
if (KeyGroup.TryGetValue(tmpMethod, out (int, int) tmpKey))
64+
{
65+
if (data.Length == tmpKey.Item1)
66+
return Encoding.UTF8.GetBytes(data);
67+
else if (data.Length == tmpKey.Item2)
68+
{
69+
return Convert.FromBase64String(data);
70+
}
71+
}
72+
throw new InvalidCastException("无法解析的密钥,请确定是否为AES-128,AES-192,AES-256");
4173
}
4274
}
4375
}

M3u8Downloader_H.Core/Converters/M3u8Converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static M3u8Converter CreateM3u8Converter(
8888
IMergeSetting mergeSetting,
8989
ILog log)
9090
{
91-
M3u8Converter m3U8Converter = new M3u8Converter(m3UFileInfo, log, downloadParamBase, mergeSetting)
91+
M3u8Converter m3U8Converter = new(m3UFileInfo, log, downloadParamBase, mergeSetting)
9292
{
9393
m3UDownloaderClient = new(log, downloadParamBase),
9494
m3UCombinerClient = new(log, downloadParamBase, mergeSetting)

M3u8Downloader_H.Core/Downloads/M3u8Downloader.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ public async ValueTask StartDownload(Action<int> StateAction,IDialogProgress dia
4545
StateAction.Invoke((int)DownloadStatus.Parsed);
4646
await GetM3U8FileInfo(cancellationToken);
4747

48-
StateAction.Invoke((int)DownloadStatus.Enqueued);
4948
using var acquire = dialogProgress.Acquire();
50-
5149
await DownloadAsync(dialogProgress, cancellationToken);
5250

5351
await MergeAsync(dialogProgress, cancellationToken);
@@ -57,18 +55,11 @@ public async ValueTask StartDownload(Action<int> StateAction,IDialogProgress dia
5755
DirectoryEx.DeleteCache(downloadParam.CachePath);
5856
}
5957

60-
private async Task GetM3U8FileInfo(CancellationToken cancellationToken)
58+
private async ValueTask GetM3U8FileInfo(CancellationToken cancellationToken)
6159
{
6260
if (_isParsed)
6361
return;
6462

65-
if (M3U8FileInfo is not null)
66-
{
67-
Log.Info("获取视频流{0}个", M3U8FileInfo.MediaFiles.Count);
68-
_isParsed = true;
69-
return;
70-
}
71-
7263
IM3u8DownloadParam m3u8DownloadParam = (IM3u8DownloadParam)downloadParam;
7364
if (m3u8DownloadParam.RequestUrl.IsFile)
7465
{
@@ -91,7 +82,7 @@ private async Task GetM3U8FileInfo(CancellationToken cancellationToken)
9182
}
9283

9384

94-
private async Task DownloadAsync(IDialogProgress downloadProgress, CancellationToken cancellationToken)
85+
private async ValueTask DownloadAsync(IDialogProgress downloadProgress, CancellationToken cancellationToken)
9586
{
9687
if (_isDownloaded)
9788
return;
@@ -103,7 +94,7 @@ private async Task DownloadAsync(IDialogProgress downloadProgress, CancellationT
10394
_isDownloaded = true;
10495
}
10596

106-
private async Task MergeAsync(IDialogProgress progress, CancellationToken cancellationToken)
97+
private async ValueTask MergeAsync(IDialogProgress progress, CancellationToken cancellationToken)
10798
{
10899
m3UCombinerClient.DialogProgress = progress;
109100

@@ -120,6 +111,7 @@ private async Task MergeAsync(IDialogProgress progress, CancellationToken cancel
120111

121112
public partial class M3u8Downloader
122113
{
114+
//通过软件界面创建
123115
public static M3u8Downloader CreateM3u8Downloader(
124116
HttpClient httpClient,
125117
IM3u8DownloadParam m3U8DownloadParam,
@@ -139,10 +131,10 @@ public static M3u8Downloader CreateM3u8Downloader(
139131
m3U8Downloader.m3UCombinerClient = new M3uCombinerClient(logger, m3U8DownloadParam, (IMergeSetting)downloaderSetting);
140132

141133
m3U8Downloader.M3UKeyInfo = m3U8DownloadParam.M3UKeyInfo;
142-
143134
return m3U8Downloader;
144135
}
145136

137+
//通过接口创建
146138
public static M3u8Downloader CreateM3u8Downloader(
147139
HttpClient httpClient,
148140
IDownloadParamBase m3U8DownloadParam,
@@ -158,6 +150,8 @@ IM3uFileInfo m3UFileInfo
158150
m3U8Downloader.m3UCombinerClient = new M3uCombinerClient(logger, m3U8DownloadParam, (IMergeSetting)downloaderSetting);
159151

160152
m3U8Downloader.M3U8FileInfo = m3UFileInfo;
153+
logger.Info("通过接口传入m3u8文件的视频流有{0}个,将跳过获取操作开始直接下载", m3UFileInfo.MediaFiles.Count);
154+
m3U8Downloader._isParsed = true;
161155
return m3U8Downloader;
162156
}
163157
}

M3u8Downloader_H.Core/Downloads/MediaDownloader.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,20 @@ public async ValueTask StartDownload(Action<int> StateAction, IDialogProgress di
4747
DirectoryEx.DeleteCache(mediaDownloadParam.CachePath);
4848
}
4949

50-
private async Task DownloadAsync(IDialogProgress downloadProgress, CancellationToken cancellationToken)
50+
private async ValueTask DownloadAsync(IDialogProgress downloadProgress, CancellationToken cancellationToken)
5151
{
5252
if (_isDownloaded)
5353
return;
5454

5555
m3UDownloaderClient.DialogProgress = downloadProgress;
56-
5756
foreach (var media in mediaDownloadParam.Medias)
5857
{
5958
await m3UDownloaderClient.MediaDownloader.DownloadAsync(media, cancellationToken);
6059
}
6160
_isDownloaded = true;
6261
}
6362

64-
private async Task MergeAsync(IDialogProgress downloadProgress, CancellationToken cancellationToken)
63+
private async ValueTask MergeAsync(IDialogProgress downloadProgress, CancellationToken cancellationToken)
6564
{
6665
m3UCombinerClient.DialogProgress = downloadProgress;
6766

M3u8Downloader_H.Downloader/M3uDownloaders/CryptM3uDownloader.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ namespace M3u8Downloader_H.Downloader.M3uDownloaders
88
{
99
internal class CryptM3uDownloader(HttpClient httpClient, IM3uFileInfo m3UFileInfo) : M3u8Downloader(httpClient)
1010
{
11+
private bool initialized = false;
1112
private readonly HttpClient httpClient = httpClient;
1213

14+
1315
public override async ValueTask Initialization(CancellationToken cancellationToken)
1416
{
17+
if (initialized)
18+
return;
19+
1520
if (m3UFileInfo.Key is null)
1621
throw new InvalidDataException("没有可用的密钥信息");
1722

@@ -41,6 +46,7 @@ public override async ValueTask Initialization(CancellationToken cancellationTok
4146
? m3uFileinfoTmp.Key = M3uKeyInfoHelper.GetKeyInfoInstance(m3UFileInfo.Key)
4247
: throw new InvalidDataException("密钥为空");
4348
}
49+
initialized = true;
4450
}
4551

4652
protected override Stream DownloadAfter(Stream stream, string contentType, CancellationToken cancellationToken)

M3u8Downloader_H.M3U8/M3UFileReaderManangers/M3UFileReaderManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class M3UFileReaderManager
2222
internal IM3u8DownloadParam DownloadParam { get; set; } = default!;
2323
internal IDownloaderSetting DownloaderSetting { get; set; } = default!;
2424
internal ILog? Log { get; set; } = default!;
25-
internal TimeSpan TimeOuts { get; } = TimeSpan.FromSeconds(15);
25+
internal TimeSpan TimeOuts { get; set; } = TimeSpan.FromSeconds(15);
2626

2727
public M3UFileReaderManager()
2828
{
@@ -36,10 +36,10 @@ public M3UFileReaderManager(HttpClient httpClient)
3636

3737
public async Task<IM3uFileInfo> GetM3u8FileInfo(CancellationToken cancellationToken = default)
3838
{
39-
using CancellationTokenSource cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
40-
cancellationTokenSource.CancelAfter(TimeOuts);
4139
for (int i = 0; i < 5; i++)
4240
{
41+
using CancellationTokenSource cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
42+
cancellationTokenSource.CancelAfter(TimeOuts);
4343
try
4444
{
4545
var m3u8FileInfo = await GetM3u8FileInfoInternal(DownloadParam.RequestUrl, DownloadParam.Headers ?? DownloaderSetting.Headers, cancellationTokenSource.Token);
@@ -64,7 +64,7 @@ public IM3uFileInfo GetM3u8FileInfo(string ext, Uri uri)
6464
"json" => new M3UFileReaderWithJson().GetM3u8FileInfo(uri),
6565
"" => new M3UFileReaderWithDirectory().GetM3u8FileInfo(uri,(Stream)null!),
6666
"m3u8" => M3u8FileReader.GetM3u8FileInfo(uri),
67-
_ => throw new InvalidOperationException("请确认是否为.m3u8或.json或.xml或文件夹"),
67+
_ => throw new InvalidOperationException("请确认是否为.m3u8或.json或文件夹"),
6868
};
6969
return checkM3u8FileInfo(m3UFileInfo, uri);
7070
}

M3u8Downloader_H/Models/DialogProgress.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -18,6 +19,7 @@ public IDisposable Acquire()
1819

1920
public void Report(double value)
2021
{
22+
Debug.Print("Report double value :{0}", value);
2123
action.Invoke(value);
2224
}
2325

0 commit comments

Comments
 (0)