1313using Spectre . Console ;
1414using System . Collections . Concurrent ;
1515using System . Text ;
16+ using N_m3u8DL_RE . Enum ;
1617
1718namespace N_m3u8DL_RE . DownloadManager ;
1819
@@ -39,9 +40,9 @@ private async Task SearchKeyAsync(string? currentKID)
3940 if ( _key != null )
4041 {
4142 if ( DownloaderConfig . MyOptions . Keys == null )
42- DownloaderConfig . MyOptions . Keys = new string [ ] { _key } ;
43+ DownloaderConfig . MyOptions . Keys = [ _key ] ;
4344 else
44- DownloaderConfig . MyOptions . Keys = DownloaderConfig . MyOptions . Keys . Concat ( new string [ ] { _key } ) . ToArray ( ) ;
45+ DownloaderConfig . MyOptions . Keys = [ .. DownloaderConfig . MyOptions . Keys , _key ] ;
4546 }
4647 }
4748
@@ -109,8 +110,8 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
109110 var saveName = DownloaderConfig . MyOptions . SaveName != null ? $ "{ DownloaderConfig . MyOptions . SaveName } .{ streamSpec . Language } ". TrimEnd ( '.' ) : dirName ;
110111 var headers = DownloaderConfig . Headers ;
111112
112- // mp4decrypt
113- var mp4decrypt = DownloaderConfig . MyOptions . DecryptionBinaryPath ! ;
113+ var decryptionBinaryPath = DownloaderConfig . MyOptions . DecryptionBinaryPath ! ;
114+ var decryptEngine = DownloaderConfig . MyOptions . GetDecryptEngine ( ) ;
114115 var mp4InitFile = "" ;
115116 var currentKID = "" ;
116117 var readInfo = false ; // 是否读取过
@@ -171,7 +172,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
171172 currentKID = mp4Info . KID ;
172173 // try shaka packager, which can handle WebM
173174 if ( string . IsNullOrEmpty ( currentKID ) && DownloaderConfig . MyOptions . UseShakaPackager ) {
174- currentKID = MP4DecryptUtil . ReadInitShaka ( result . ActualFilePath , mp4decrypt ) ;
175+ currentKID = MP4DecryptUtil . ReadInitShaka ( result . ActualFilePath , decryptionBinaryPath ) ;
175176 }
176177 // 从文件读取KEY
177178 await SearchKeyAsync ( currentKID ) ;
@@ -180,7 +181,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
180181 {
181182 var enc = result . ActualFilePath ;
182183 var dec = Path . Combine ( Path . GetDirectoryName ( enc ) ! , Path . GetFileNameWithoutExtension ( enc ) + "_dec" + Path . GetExtension ( enc ) ) ;
183- var dResult = await MP4DecryptUtil . DecryptAsync ( DownloaderConfig . MyOptions . UseShakaPackager , mp4decrypt , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID , isMultiDRM : mp4Info . isMultiDRM ) ;
184+ var dResult = await MP4DecryptUtil . DecryptAsync ( decryptEngine , decryptionBinaryPath , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID , isMultiDRM : mp4Info . isMultiDRM ) ;
184185 if ( dResult )
185186 {
186187 FileDic [ streamSpec . Playlist . MediaInit ] ! . ActualFilePath = dec ;
@@ -229,7 +230,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
229230 // 需要重新解密init
230231 var enc = FileDic [ streamSpec . Playlist ! . MediaInit ! ] ! . ActualFilePath ;
231232 var dec = Path . Combine ( Path . GetDirectoryName ( enc ) ! , Path . GetFileNameWithoutExtension ( enc ) + "_dec" + Path . GetExtension ( enc ) ) ;
232- var dResult = await MP4DecryptUtil . DecryptAsync ( DownloaderConfig . MyOptions . UseShakaPackager , mp4decrypt , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID ) ;
233+ var dResult = await MP4DecryptUtil . DecryptAsync ( decryptEngine , decryptionBinaryPath , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID ) ;
233234 if ( dResult )
234235 {
235236 FileDic [ streamSpec . Playlist ! . MediaInit ! ] ! . ActualFilePath = dec ;
@@ -243,7 +244,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
243244 }
244245 // try shaka packager, which can handle WebM
245246 if ( string . IsNullOrEmpty ( currentKID ) && DownloaderConfig . MyOptions . UseShakaPackager ) {
246- currentKID = MP4DecryptUtil . ReadInitShaka ( result . ActualFilePath , mp4decrypt ) ;
247+ currentKID = MP4DecryptUtil . ReadInitShaka ( result . ActualFilePath , decryptionBinaryPath ) ;
247248 }
248249 // 从文件读取KEY
249250 await SearchKeyAsync ( currentKID ) ;
@@ -253,7 +254,7 @@ private async Task<bool> DownloadStreamAsync(StreamSpec streamSpec, ProgressTask
253254 var enc = result . ActualFilePath ;
254255 var dec = Path . Combine ( Path . GetDirectoryName ( enc ) ! , Path . GetFileNameWithoutExtension ( enc ) + "_dec" + Path . GetExtension ( enc ) ) ;
255256 mp4Info = MP4DecryptUtil . GetMP4Info ( enc ) ;
256- var dResult = await MP4DecryptUtil . DecryptAsync ( DownloaderConfig . MyOptions . UseShakaPackager , mp4decrypt , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID , mp4InitFile , isMultiDRM : mp4Info . isMultiDRM ) ;
257+ var dResult = await MP4DecryptUtil . DecryptAsync ( decryptEngine , decryptionBinaryPath , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID , mp4InitFile , isMultiDRM : mp4Info . isMultiDRM ) ;
257258 if ( dResult )
258259 {
259260 File . Delete ( enc ) ;
@@ -291,7 +292,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
291292 var enc = result . ActualFilePath ;
292293 var dec = Path . Combine ( Path . GetDirectoryName ( enc ) ! , Path . GetFileNameWithoutExtension ( enc ) + "_dec" + Path . GetExtension ( enc ) ) ;
293294 mp4Info = MP4DecryptUtil . GetMP4Info ( enc ) ;
294- var dResult = await MP4DecryptUtil . DecryptAsync ( DownloaderConfig . MyOptions . UseShakaPackager , mp4decrypt , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID , mp4InitFile , isMultiDRM : mp4Info . isMultiDRM ) ;
295+ var dResult = await MP4DecryptUtil . DecryptAsync ( decryptEngine , decryptionBinaryPath , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID , mp4InitFile , isMultiDRM : mp4Info . isMultiDRM ) ;
295296 if ( dResult )
296297 {
297298 File . Delete ( enc ) ;
@@ -322,8 +323,8 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
322323 if ( ! string . IsNullOrEmpty ( currentKID ) && DownloaderConfig . MyOptions . MP4RealTimeDecryption && DownloaderConfig . MyOptions . Keys != null && DownloaderConfig . MyOptions . Keys . Length > 0 && mp4InitFile != "" )
323324 {
324325 File . Delete ( mp4InitFile ) ;
325- // shaka实时解密不需要init文件用于合并
326- if ( DownloaderConfig . MyOptions . UseShakaPackager )
326+ // shaka/ffmpeg实时解密不需要init文件用于合并
327+ if ( decryptEngine != DecryptEngine . MP4DECRYPT )
327328 {
328329 FileDic ! . Remove ( streamSpec . Playlist ! . MediaInit , out _ ) ;
329330 }
@@ -590,7 +591,7 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
590591 currentKID = MP4DecryptUtil . GetMP4Info ( output ) . KID ;
591592 // try shaka packager, which can handle WebM
592593 if ( string . IsNullOrEmpty ( currentKID ) && DownloaderConfig . MyOptions . UseShakaPackager ) {
593- currentKID = MP4DecryptUtil . ReadInitShaka ( output , mp4decrypt ) ;
594+ currentKID = MP4DecryptUtil . ReadInitShaka ( output , decryptionBinaryPath ) ;
594595 }
595596 // 从文件读取KEY
596597 await SearchKeyAsync ( currentKID ) ;
@@ -602,8 +603,8 @@ await Parallel.ForEachAsync(segments, options, async (seg, _) =>
602603 var enc = output ;
603604 var dec = Path . Combine ( Path . GetDirectoryName ( enc ) ! , Path . GetFileNameWithoutExtension ( enc ) + "_dec" + Path . GetExtension ( enc ) ) ;
604605 mp4Info = MP4DecryptUtil . GetMP4Info ( enc ) ;
605- Logger . InfoMarkUp ( $ "[grey]Decrypting...[/]") ;
606- var result = await MP4DecryptUtil . DecryptAsync ( DownloaderConfig . MyOptions . UseShakaPackager , mp4decrypt , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID , isMultiDRM : mp4Info . isMultiDRM ) ;
606+ Logger . InfoMarkUp ( $ "[grey]Decrypting using { decryptEngine } ...[/]") ;
607+ var result = await MP4DecryptUtil . DecryptAsync ( decryptEngine , decryptionBinaryPath , DownloaderConfig . MyOptions . Keys , enc , dec , currentKID , isMultiDRM : mp4Info . isMultiDRM ) ;
607608 if ( result )
608609 {
609610 File . Delete ( enc ) ;
@@ -653,8 +654,7 @@ public async Task<bool> StartDownloadAsync()
653654 }
654655 progress . Columns ( progressColumns ) ;
655656
656- if ( DownloaderConfig . MyOptions . MP4RealTimeDecryption && ! DownloaderConfig . MyOptions . UseShakaPackager
657- && DownloaderConfig . MyOptions . Keys != null && DownloaderConfig . MyOptions . Keys . Length > 0 )
657+ if ( DownloaderConfig . MyOptions is { MP4RealTimeDecryption : true , UseShakaPackager : false , Keys . Length : > 0 } )
658658 Logger . WarnMarkUp ( $ "[darkorange3_1]{ ResString . realTimeDecMessage } [/]") ;
659659
660660 await progress . StartAsync ( async ctx =>
0 commit comments