Skip to content

Commit

Permalink
必要时才调用mp4解密 (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaoda authored Nov 29, 2024
1 parent 6bd906e commit 8095c6e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/N_m3u8DL-RE.Common/Entity/MediaSegment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace N_m3u8DL_RE.Common.Entity;
using N_m3u8DL_RE.Common.Enum;

namespace N_m3u8DL_RE.Common.Entity;

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

public EncryptInfo EncryptInfo { get; set; } = new EncryptInfo();
public EncryptInfo EncryptInfo { get; set; } = new();

public bool IsEncrypted => EncryptInfo.Method != EncryptMethod.NONE;

public string Url { get; set; }

Expand Down
8 changes: 4 additions & 4 deletions src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
// 从文件读取KEY
await SearchKeyAsync(currentKID);
// 实时解密
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID) && StreamExtractor.ExtractorType != ExtractorType.MSS)
if (streamSpec.Playlist.MediaInit.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID) && StreamExtractor.ExtractorType != ExtractorType.MSS)
{
var enc = result.ActualFilePath;
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
Expand Down Expand Up @@ -225,7 +225,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
var processor = new MSSMoovProcessor(streamSpec);
var header = processor.GenHeader(File.ReadAllBytes(result.ActualFilePath));
await File.WriteAllBytesAsync(FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath, header);
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
{
// 需要重新解密init
var enc = FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath;
Expand All @@ -249,7 +249,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
// 从文件读取KEY
await SearchKeyAsync(currentKID);
// 实时解密
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
{
var enc = result.ActualFilePath;
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
Expand Down Expand Up @@ -287,7 +287,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
if (result != null && result.Success)
task.Increment(1);
// 实时解密
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID))
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID))
{
var enc = result.ActualFilePath;
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
Expand Down
8 changes: 4 additions & 4 deletions src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private async Task<bool> RecordStreamAsync(StreamSpec streamSpec, ProgressTask t
// 从文件读取KEY
await SearchKeyAsync(currentKID);
// 实时解密
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID) && StreamExtractor.ExtractorType != ExtractorType.MSS)
if (streamSpec.Playlist.MediaInit.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID) && StreamExtractor.ExtractorType != ExtractorType.MSS)
{
var enc = result.ActualFilePath;
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
Expand Down Expand Up @@ -270,7 +270,7 @@ private async Task<bool> RecordStreamAsync(StreamSpec streamSpec, ProgressTask t
var processor = new MSSMoovProcessor(streamSpec);
var header = processor.GenHeader(File.ReadAllBytes(result.ActualFilePath));
await File.WriteAllBytesAsync(FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath, header);
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
{
// 需要重新解密init
var enc = FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath;
Expand All @@ -290,7 +290,7 @@ private async Task<bool> RecordStreamAsync(StreamSpec streamSpec, ProgressTask t
// 从文件读取KEY
await SearchKeyAsync(currentKID);
// 实时解密
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
{
var enc = result.ActualFilePath;
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
Expand Down Expand Up @@ -333,7 +333,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
if (result != null && result.Success)
task.Increment(1);
// 实时解密
if (DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID))
if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && result != null && result.Success && !string.IsNullOrEmpty(currentKID))
{
var enc = result.ActualFilePath;
var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
Expand Down

0 comments on commit 8095c6e

Please sign in to comment.