Skip to content

Commit

Permalink
add --keep-encrypted-chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Last-Order committed May 8, 2022
1 parent b462024 commit f51efc1
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 17 deletions.
4 changes: 2 additions & 2 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minyami",
"version": "4.7.1",
"version": "4.7.2",
"description": "",
"main": "dist/exports.js",
"types": "dist/exports.d.ts",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Help:
--slice <range> Download specified part of the stream
<range> Set time range in [<hh:mm:ss>-<hh:mm:ss> format]. eg. --slice "45:00-53:00"
--nomerge, keep Do not merge m3u8 chunks.
--keep-encrypted-chunks Do not delete encrypted chunks after decryption. Use with --keep.
--resume <input_path> Resume a download. (Archive) -r
<input_path> m3u8 file path
--clean Clean cache files
Expand Down
1 change: 1 addition & 0 deletions readme.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Help:
--slice <range> 下载部分内容
<range> 设置时间范围,格式为 [<hh:mm:ss>-<hh:mm:ss> format] 例如 --slice "45:00-53:00"
--nomerge, keep 不合并视频分块。
--keep-encrypted-chunks 不删除解密前分块。与--keep一起使用。
--resume <input_path> 恢复下载 (不适用于直播) -r
<input_path> m3u8 文件路径
--clean 清除缓存文件和任务信息
Expand Down
2 changes: 2 additions & 0 deletions src/core/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ArchiveDownloader extends Downloader {
cookies,
headers,
nomerge,
keepEncryptedChunks,
cliMode,
}: ArchiveDownloaderConfig = {}
) {
Expand All @@ -68,6 +69,7 @@ class ArchiveDownloader extends Downloader {
cookies,
headers,
nomerge,
keepEncryptedChunks,
cliMode,
});
if (slice) {
Expand Down
16 changes: 15 additions & 1 deletion src/core/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface DownloaderConfig {
proxy?: string;
format?: string;
nomerge?: boolean;
keepEncryptedChunks?: boolean;
cliMode?: boolean;
}

Expand Down Expand Up @@ -128,6 +129,8 @@ class Downloader extends EventEmitter {

encryptionKeys = {};

keepEncryptedChunks = false;

// Hooks
protected onChunkNaming: (chunk: M3U8Chunk | EncryptedM3U8Chunk) => string = (chunk) => {
return new URL(chunk.url).pathname
Expand All @@ -152,6 +155,7 @@ class Downloader extends EventEmitter {
headers,
nomerge,
cliMode = false,
keepEncryptedChunks,
}: DownloaderConfig = {
threads: 5,
}
Expand Down Expand Up @@ -218,6 +222,15 @@ class Downloader extends EventEmitter {

if (nomerge) {
this.noMerge = nomerge;
logger.info("Temporary files will not be deleted automatically.");
}

if (keepEncryptedChunks) {
this.keepEncryptedChunks = keepEncryptedChunks;
logger.info("Encrypted chunks will not be deleted automatically.");
if (!this.noMerge) {
logger.warning(`--keep-encrypted-chunks should be used with --keep.`);
}
}

this.m3u8Path = m3u8Path;
Expand Down Expand Up @@ -323,7 +336,8 @@ class Downloader extends EventEmitter {
path.resolve(this.tempPath, `./${task.filename}`),
path.resolve(this.tempPath, `./${task.filename}`) + ".decrypt",
this.getEncryptionKey(CommonUtils.buildFullUrl(this.m3u8.m3u8Url, task.key)),
task.iv || task.sequenceId.toString(16)
task.iv || task.sequenceId.toString(16),
this.keepEncryptedChunks
);
logger.debug(`Decrypting ${task.filename} succeed`);
}
Expand Down
15 changes: 14 additions & 1 deletion src/core/live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ export default class LiveDownloader extends Downloader {
*/
constructor(
m3u8Path: string,
{ threads, output, key, verbose, retries, proxy, cookies, headers, nomerge, cliMode }: LiveDownloaderConfig
{
threads,
output,
key,
verbose,
retries,
proxy,
cookies,
headers,
nomerge,
keepEncryptedChunks,
cliMode,
}: LiveDownloaderConfig
) {
super(m3u8Path, {
threads: threads || 5,
Expand All @@ -47,6 +59,7 @@ export default class LiveDownloader extends Downloader {
cookies,
headers,
nomerge,
keepEncryptedChunks,
cliMode,
});
if (retries) {
Expand Down
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,9 @@ Erii.bind(
logger.enableDebugMode();
}
ProxyAgent.readProxyConfigurationFromEnv();
const downloadOptions = Object.assign(options, { cliMode: true, logger });
if (options.live) {
const downloader = new LiveDownloader(path, {
...options,
cliMode: true,
logger,
});
const downloader = new LiveDownloader(path, downloadOptions);
downloader.on("finished", () => {
process.exit();
});
Expand All @@ -74,11 +71,7 @@ Erii.bind(
});
await downloader.download();
} else {
const downloader = new ArchiveDownloader(path, {
...options,
cliMode: true,
logger,
});
const downloader = new ArchiveDownloader(path, downloadOptions);
downloader.on("finished", () => {
process.exit();
});
Expand Down Expand Up @@ -278,6 +271,12 @@ Erii.addOption({
description: "Do not merge m3u8 chunks.",
});

Erii.addOption({
name: ["keep-encrypted-chunks"],
command: "download",
description: "Do not delete encrypted chunks after decryption.",
});

Erii.default(() => {
Erii.showHelp();
});
Expand Down
4 changes: 2 additions & 2 deletions src/utils/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export async function requestRaw(url: string, options: AxiosRequestConfig = {}):
* @param key in hex
* @param iv in hex
*/
export function decrypt(input: string, output: string, key: string, iv: string) {
export function decrypt(input: string, output: string, key: string, iv: string, keepEncryptedChunks = false) {
return new Promise<void>((resolve) => {
const algorithm = "aes-128-cbc";
if (key.length !== 32) {
Expand All @@ -165,7 +165,7 @@ export function decrypt(input: string, output: string, key: string, iv: string)
const o = fs.createWriteStream(output);
const pipe = i.pipe(decipher).pipe(o);
pipe.on("finish", () => {
fs.unlinkSync(input);
!keepEncryptedChunks && fs.unlinkSync(input);
resolve();
});
});
Expand Down

0 comments on commit f51efc1

Please sign in to comment.