Skip to content

Commit 8095c6e

Browse files
authored
必要时才调用mp4解密 (#519)
1 parent 6bd906e commit 8095c6e

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/N_m3u8DL-RE.Common/Entity/MediaSegment.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace N_m3u8DL_RE.Common.Entity;
1+
using N_m3u8DL_RE.Common.Enum;
2+
3+
namespace N_m3u8DL_RE.Common.Entity;
24

35
public class MediaSegment
46
{
@@ -11,7 +13,9 @@ public class MediaSegment
1113
public long? StopRange => (StartRange != null && ExpectLength != null) ? StartRange + ExpectLength - 1 : null;
1214
public long? ExpectLength { get; set; }
1315

14-
public EncryptInfo EncryptInfo { get; set; } = new EncryptInfo();
16+
public EncryptInfo EncryptInfo { get; set; } = new();
17+
18+
public bool IsEncrypted => EncryptInfo.Method != EncryptMethod.NONE;
1519

1620
public string Url { get; set; }
1721

src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
177177
// 从文件读取KEY
178178
await SearchKeyAsync(currentKID);
179179
// 实时解密
180-
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID) && StreamExtractor.ExtractorType != ExtractorType.MSS)
180+
if (streamSpec.Playlist.MediaInit.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID) && StreamExtractor.ExtractorType != ExtractorType.MSS)
181181
{
182182
var enc = result.ActualFilePath;
183183
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
@@ -225,7 +225,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
225225
var processor = new MSSMoovProcessor(streamSpec);
226226
var header = processor.GenHeader(File.ReadAllBytes(result.ActualFilePath));
227227
await File.WriteAllBytesAsync(FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath, header);
228-
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
228+
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
229229
{
230230
// 需要重新解密init
231231
var enc = FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath;
@@ -249,7 +249,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
249249
// 从文件读取KEY
250250
await SearchKeyAsync(currentKID);
251251
// 实时解密
252-
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
252+
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
253253
{
254254
var enc = result.ActualFilePath;
255255
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
@@ -287,7 +287,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
287287
if (result != null && result.Success)
288288
task.Increment(1);
289289
// 实时解密
290-
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID))
290+
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID))
291291
{
292292
var enc = result.ActualFilePath;
293293
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));

src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private async Task<bool> RecordStreamAsync(StreamSpec streamSpec, ProgressTask t
211211
// 从文件读取KEY
212212
await SearchKeyAsync(currentKID);
213213
// 实时解密
214-
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID) && StreamExtractor.ExtractorType != ExtractorType.MSS)
214+
if (streamSpec.Playlist.MediaInit.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID) && StreamExtractor.ExtractorType != ExtractorType.MSS)
215215
{
216216
var enc = result.ActualFilePath;
217217
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
@@ -270,7 +270,7 @@ private async Task<bool> RecordStreamAsync(StreamSpec streamSpec, ProgressTask t
270270
var processor = new MSSMoovProcessor(streamSpec);
271271
var header = processor.GenHeader(File.ReadAllBytes(result.ActualFilePath));
272272
await File.WriteAllBytesAsync(FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath, header);
273-
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
273+
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
274274
{
275275
// 需要重新解密init
276276
var enc = FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath;
@@ -290,7 +290,7 @@ private async Task<bool> RecordStreamAsync(StreamSpec streamSpec, ProgressTask t
290290
// 从文件读取KEY
291291
await SearchKeyAsync(currentKID);
292292
// 实时解密
293-
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
293+
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
294294
{
295295
var enc = result.ActualFilePath;
296296
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
@@ -333,7 +333,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
333333
if (result != null && result.Success)
334334
task.Increment(1);
335335
// 实时解密
336-
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID))
336+
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID))
337337
{
338338
var enc = result.ActualFilePath;
339339
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));

0 commit comments

Comments
 (0)