@napuketto/media 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +117 -3
- package/dist/index.d.cts +44 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +44 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +116 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -6428,6 +6428,55 @@ async function decodeSilkToPcm(input, sampleRate = SILK_SAMPLE_RATE) {
|
|
|
6428
6428
|
};
|
|
6429
6429
|
}
|
|
6430
6430
|
/**
|
|
6431
|
+
* 解码 SILK → WAV 文件(pcm_s16le 单声道 + 44 字节头,通用可播放格式)。
|
|
6432
|
+
* 收方向语音接线用(koishi h.audio / 外部客户端):QQ 语音是 silk v3,
|
|
6433
|
+
* 多数播放器无法直接解码,落 WAV 后以本地路径消费。
|
|
6434
|
+
*/
|
|
6435
|
+
async function decodeSilkToWav(input, options = {}) {
|
|
6436
|
+
const sampleRate = options.sampleRate ?? 24e3;
|
|
6437
|
+
const buffer = await (0, node_fs_promises.readFile)(input);
|
|
6438
|
+
if (!(0, silk_wasm.isSilk)(buffer)) throw new MediaError(`不是有效的 SILK 文件: ${input}`);
|
|
6439
|
+
const result = await (0, silk_wasm.decode)(buffer, sampleRate);
|
|
6440
|
+
const { dir, name } = outputTarget(input, ".wav", options.outDir);
|
|
6441
|
+
const wav = wrapPcmInWav(result.data, sampleRate);
|
|
6442
|
+
await (0, node_fs_promises.writeFile)((0, node_path.join)(dir, name), wav);
|
|
6443
|
+
return {
|
|
6444
|
+
wavPath: (0, node_path.join)(dir, name),
|
|
6445
|
+
durationMs: result.duration
|
|
6446
|
+
};
|
|
6447
|
+
}
|
|
6448
|
+
/** 输出目标(目录 + 文件名;outDir 优先,否则输入同目录替换扩展名)。 */
|
|
6449
|
+
function outputTarget(input, ext, outDir) {
|
|
6450
|
+
const base = input.replaceAll("\\", "/").split("/").pop() ?? "audio";
|
|
6451
|
+
const dot = base.lastIndexOf(".");
|
|
6452
|
+
const name = `${dot > 0 ? base.slice(0, dot) : base}${ext}`;
|
|
6453
|
+
const dir = outDir !== void 0 ? outDir : (0, node_path.dirname)(input);
|
|
6454
|
+
(0, node_fs.mkdirSync)(dir, { recursive: true });
|
|
6455
|
+
return {
|
|
6456
|
+
dir,
|
|
6457
|
+
name
|
|
6458
|
+
};
|
|
6459
|
+
}
|
|
6460
|
+
/** PCM(s16le 单声道)→ WAV 文件字节(44 字节头 + 数据)。 */
|
|
6461
|
+
function wrapPcmInWav(pcm, sampleRate) {
|
|
6462
|
+
const blockAlign = 2;
|
|
6463
|
+
const header = Buffer.alloc(44);
|
|
6464
|
+
header.write("RIFF", 0, "ascii");
|
|
6465
|
+
header.writeUInt32LE(36 + pcm.byteLength, 4);
|
|
6466
|
+
header.write("WAVE", 8, "ascii");
|
|
6467
|
+
header.write("fmt ", 12, "ascii");
|
|
6468
|
+
header.writeUInt32LE(16, 16);
|
|
6469
|
+
header.writeUInt16LE(1, 20);
|
|
6470
|
+
header.writeUInt16LE(1, 22);
|
|
6471
|
+
header.writeUInt32LE(sampleRate, 24);
|
|
6472
|
+
header.writeUInt32LE(sampleRate * blockAlign | 0, 28);
|
|
6473
|
+
header.writeUInt16LE(blockAlign, 32);
|
|
6474
|
+
header.writeUInt16LE(16, 34);
|
|
6475
|
+
header.write("data", 36, "ascii");
|
|
6476
|
+
header.writeUInt32LE(pcm.byteLength, 40);
|
|
6477
|
+
return Buffer.concat([header, Buffer.from(pcm)]);
|
|
6478
|
+
}
|
|
6479
|
+
/**
|
|
6431
6480
|
* 编码任意音频为 SILK 文件(返回输出路径,输出紧随输入文件)。
|
|
6432
6481
|
*
|
|
6433
6482
|
* - 已是 SILK → 原样返回
|
|
@@ -6486,6 +6535,66 @@ async function getSilkInfo(input, sampleRate = SILK_SAMPLE_RATE) {
|
|
|
6486
6535
|
};
|
|
6487
6536
|
}
|
|
6488
6537
|
//#endregion
|
|
6538
|
+
//#region src/downloader.ts
|
|
6539
|
+
/** 默认大小上限(30MB)。 */
|
|
6540
|
+
const DEFAULT_MAX_BYTES = 31457280;
|
|
6541
|
+
/** 默认超时(15s)。 */
|
|
6542
|
+
const DEFAULT_TIMEOUT_MS = 15e3;
|
|
6543
|
+
/** 扩展名字符合法性(URL pathname 提取校验)。 */
|
|
6544
|
+
const EXT_RE = /^\.[a-z0-9]+$/i;
|
|
6545
|
+
/**
|
|
6546
|
+
* 下载 URL 到目标路径(覆盖写)。返回写入字节数。
|
|
6547
|
+
* 失败抛错(非 2xx / 超限 / 超时 / 空 body / IO 错误),不产生半成品语义
|
|
6548
|
+
* (失败时目标文件可能残留,调用方负责清理目录)。
|
|
6549
|
+
*/
|
|
6550
|
+
async function downloadUrl(url, dest, options = {}) {
|
|
6551
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
6552
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
6553
|
+
const response = await fetch(url, {
|
|
6554
|
+
redirect: "follow",
|
|
6555
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
6556
|
+
});
|
|
6557
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
6558
|
+
const declared = Number(response.headers.get("content-length") ?? "0");
|
|
6559
|
+
if (declared > maxBytes) throw new Error(`文件超过大小上限(${declared} > ${maxBytes} 字节)`);
|
|
6560
|
+
const body = response.body;
|
|
6561
|
+
if (body === null) throw new Error("响应无 body");
|
|
6562
|
+
const reader = body.getReader();
|
|
6563
|
+
const chunks = [];
|
|
6564
|
+
let total = 0;
|
|
6565
|
+
for (;;) {
|
|
6566
|
+
const { done, value } = await reader.read();
|
|
6567
|
+
if (done) break;
|
|
6568
|
+
if (value === void 0) continue;
|
|
6569
|
+
total += value.byteLength;
|
|
6570
|
+
if (total > maxBytes) {
|
|
6571
|
+
await reader.cancel().catch(() => void 0);
|
|
6572
|
+
throw new Error(`文件超过大小上限(>${maxBytes} 字节)`);
|
|
6573
|
+
}
|
|
6574
|
+
chunks.push(value);
|
|
6575
|
+
}
|
|
6576
|
+
if (total === 0) throw new Error("下载内容为空");
|
|
6577
|
+
const { writeFile } = await import("node:fs/promises");
|
|
6578
|
+
await writeFile(dest, Buffer.concat(chunks));
|
|
6579
|
+
return total;
|
|
6580
|
+
}
|
|
6581
|
+
/** 从 Content-Type / URL 路径推断扩展名(缺省 .bin;下载命名用)。 */
|
|
6582
|
+
function inferExtension(contentType, url) {
|
|
6583
|
+
if (contentType !== null) {
|
|
6584
|
+
const base = contentType.split(";")[0]?.trim().toLowerCase() ?? "";
|
|
6585
|
+
const sub = base.split("/")[1]?.split("+")[0];
|
|
6586
|
+
if (base.startsWith("image/") || base.startsWith("audio/") || base.startsWith("video/")) {
|
|
6587
|
+
if (sub !== void 0 && sub !== "") return `.${sub}`;
|
|
6588
|
+
}
|
|
6589
|
+
}
|
|
6590
|
+
try {
|
|
6591
|
+
const pathname = new URL(url).pathname;
|
|
6592
|
+
const ext = pathname.substring(pathname.lastIndexOf("."));
|
|
6593
|
+
if (ext.length > 1 && ext.length <= 6 && EXT_RE.test(ext)) return ext.toLowerCase();
|
|
6594
|
+
} catch {}
|
|
6595
|
+
return ".bin";
|
|
6596
|
+
}
|
|
6597
|
+
//#endregion
|
|
6489
6598
|
//#region ../../node_modules/.pnpm/@borewit+text-codec@0.2.2/node_modules/@borewit/text-codec/lib/index.js
|
|
6490
6599
|
const WINDOWS_1252_EXTRA = {
|
|
6491
6600
|
128: "€",
|
|
@@ -11388,7 +11497,7 @@ function replaceExt(input, ext) {
|
|
|
11388
11497
|
else stem = base;
|
|
11389
11498
|
return `${dir}${stem}${ext}`;
|
|
11390
11499
|
}
|
|
11391
|
-
/** 转码:按选项输出到替换扩展名后的路径(.mp4)。 */
|
|
11500
|
+
/** 转码:按选项输出到替换扩展名后的路径(.mp4)。ffmpeg 缺失/失败抛 MediaError。 */
|
|
11392
11501
|
async function transcodeVideo(input, opts = {}) {
|
|
11393
11502
|
const args = [
|
|
11394
11503
|
"-y",
|
|
@@ -11401,11 +11510,13 @@ async function transcodeVideo(input, opts = {}) {
|
|
|
11401
11510
|
if (opts.fps !== void 0) args.push("-r", String(opts.fps));
|
|
11402
11511
|
const output = replaceExt(input, ".mp4");
|
|
11403
11512
|
args.push("-c:v", "libx264", "-preset", "fast", output);
|
|
11513
|
+
let exitCode;
|
|
11404
11514
|
try {
|
|
11405
|
-
await execa("ffmpeg", args, { reject: false });
|
|
11515
|
+
exitCode = (await execa("ffmpeg", args, { reject: false })).exitCode;
|
|
11406
11516
|
} catch (err) {
|
|
11407
|
-
throw new MediaError(`ffmpeg
|
|
11517
|
+
throw new MediaError(`ffmpeg 不可用或转码失败: ${input}`, { cause: err });
|
|
11408
11518
|
}
|
|
11519
|
+
if (exitCode !== 0 || !(0, node_fs.existsSync)(output)) throw new MediaError(`ffmpeg 转码失败: ${input}(exit ${exitCode ?? "?"})`);
|
|
11409
11520
|
return output;
|
|
11410
11521
|
}
|
|
11411
11522
|
/** 读取视频宽高与时长(ffprobe JSON 输出解析)。 */
|
|
@@ -11442,9 +11553,12 @@ async function getVideoInfo(input) {
|
|
|
11442
11553
|
exports.MediaError = MediaError;
|
|
11443
11554
|
exports.SILK_SAMPLE_RATE = SILK_SAMPLE_RATE;
|
|
11444
11555
|
exports.decodeSilkToPcm = decodeSilkToPcm;
|
|
11556
|
+
exports.decodeSilkToWav = decodeSilkToWav;
|
|
11445
11557
|
exports.detectFileType = detectFileType;
|
|
11558
|
+
exports.downloadUrl = downloadUrl;
|
|
11446
11559
|
exports.encodePcmToSilk = encodePcmToSilk;
|
|
11447
11560
|
exports.getImageSize = getImageSize;
|
|
11448
11561
|
exports.getSilkInfo = getSilkInfo;
|
|
11449
11562
|
exports.getVideoInfo = getVideoInfo;
|
|
11563
|
+
exports.inferExtension = inferExtension;
|
|
11450
11564
|
exports.transcodeVideo = transcodeVideo;
|
package/dist/index.d.cts
CHANGED
|
@@ -53,6 +53,24 @@ declare class MediaError extends Error {
|
|
|
53
53
|
declare const SILK_SAMPLE_RATE = 24000;
|
|
54
54
|
/** 解码 SILK 音频为 PCM(pcm_s16le)。 */
|
|
55
55
|
declare function decodeSilkToPcm(input: string, sampleRate?: number): Promise<DecodedAudio>;
|
|
56
|
+
/** 解码 SILK → WAV 输出选项。 */
|
|
57
|
+
interface DecodeSilkToWavOptions {
|
|
58
|
+
/** 采样率(缺省 24000,QQ 语音协议值)。 */
|
|
59
|
+
sampleRate?: number;
|
|
60
|
+
/** 输出目录(缺省输入同目录;收方向解码建议 tmpdir,避免污染 NT 数据目录)。 */
|
|
61
|
+
outDir?: string;
|
|
62
|
+
}
|
|
63
|
+
/** 解码结果(WAV)。 */
|
|
64
|
+
interface DecodedWav {
|
|
65
|
+
wavPath: string;
|
|
66
|
+
durationMs: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 解码 SILK → WAV 文件(pcm_s16le 单声道 + 44 字节头,通用可播放格式)。
|
|
70
|
+
* 收方向语音接线用(koishi h.audio / 外部客户端):QQ 语音是 silk v3,
|
|
71
|
+
* 多数播放器无法直接解码,落 WAV 后以本地路径消费。
|
|
72
|
+
*/
|
|
73
|
+
declare function decodeSilkToWav(input: string, options?: DecodeSilkToWavOptions): Promise<DecodedWav>;
|
|
56
74
|
/**
|
|
57
75
|
* 编码任意音频为 SILK 文件(返回输出路径,输出紧随输入文件)。
|
|
58
76
|
*
|
|
@@ -65,6 +83,30 @@ declare function encodePcmToSilk(input: string, sampleRate?: number): Promise<st
|
|
|
65
83
|
/** 获取 SILK 音频信息(时长 / 采样率)。 */
|
|
66
84
|
declare function getSilkInfo(input: string, sampleRate?: number): Promise<AudioInfo>;
|
|
67
85
|
//#endregion
|
|
86
|
+
//#region src/downloader.d.ts
|
|
87
|
+
/**
|
|
88
|
+
* downloader.ts:HTTP(S) 媒体下载(2026-09-08 T5)
|
|
89
|
+
*
|
|
90
|
+
* 协议层(adapter get_image 主动下载 / koishi 插件发送侧下沉)共用的
|
|
91
|
+
* 最小下载原语:fetch + 大小上限 + 超时,写目标路径(调用方管理目录与清理)。
|
|
92
|
+
* 零额外依赖(Node 18+ 内建 fetch)。
|
|
93
|
+
*/
|
|
94
|
+
/** 下载选项。 */
|
|
95
|
+
interface DownloadUrlOptions {
|
|
96
|
+
/** 超时毫秒(缺省 15s)。 */
|
|
97
|
+
timeoutMs?: number;
|
|
98
|
+
/** 大小上限字节(缺省 30MB)。 */
|
|
99
|
+
maxBytes?: number;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 下载 URL 到目标路径(覆盖写)。返回写入字节数。
|
|
103
|
+
* 失败抛错(非 2xx / 超限 / 超时 / 空 body / IO 错误),不产生半成品语义
|
|
104
|
+
* (失败时目标文件可能残留,调用方负责清理目录)。
|
|
105
|
+
*/
|
|
106
|
+
declare function downloadUrl(url: string, dest: string, options?: DownloadUrlOptions): Promise<number>;
|
|
107
|
+
/** 从 Content-Type / URL 路径推断扩展名(缺省 .bin;下载命名用)。 */
|
|
108
|
+
declare function inferExtension(contentType: string | null, url: string): string;
|
|
109
|
+
//#endregion
|
|
68
110
|
//#region src/image.d.ts
|
|
69
111
|
/** 读取图片尺寸(image-size 同步 API,内部读文件 + 计算)。 */
|
|
70
112
|
declare function getImageSize(input: string): Promise<ImageSize>;
|
|
@@ -72,10 +114,10 @@ declare function getImageSize(input: string): Promise<ImageSize>;
|
|
|
72
114
|
declare function detectFileType(input: string): Promise<FileTypeResult>;
|
|
73
115
|
//#endregion
|
|
74
116
|
//#region src/video.d.ts
|
|
75
|
-
/** 转码:按选项输出到替换扩展名后的路径(.mp4)。 */
|
|
117
|
+
/** 转码:按选项输出到替换扩展名后的路径(.mp4)。ffmpeg 缺失/失败抛 MediaError。 */
|
|
76
118
|
declare function transcodeVideo(input: string, opts?: TranscodeOptions): Promise<string>;
|
|
77
119
|
/** 读取视频宽高与时长(ffprobe JSON 输出解析)。 */
|
|
78
120
|
declare function getVideoInfo(input: string): Promise<VideoInfo>;
|
|
79
121
|
//#endregion
|
|
80
|
-
export { type AudioInfo, type DecodedAudio, type FileTypeResult, type ImageSize, MediaError, SILK_SAMPLE_RATE, type TranscodeOptions, type VideoInfo, decodeSilkToPcm, detectFileType, encodePcmToSilk, getImageSize, getSilkInfo, getVideoInfo, transcodeVideo };
|
|
122
|
+
export { type AudioInfo, type DecodeSilkToWavOptions, type DecodedAudio, type DecodedWav, type DownloadUrlOptions, type FileTypeResult, type ImageSize, MediaError, SILK_SAMPLE_RATE, type TranscodeOptions, type VideoInfo, decodeSilkToPcm, decodeSilkToWav, detectFileType, downloadUrl, encodePcmToSilk, getImageSize, getSilkInfo, getVideoInfo, inferExtension, transcodeVideo };
|
|
81
123
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/audio.ts","../src/image.ts","../src/video.ts"],"mappings":";;;;;;;UAOiB;EACb;EACA;;;UAIa;;EAEb;;EAEA;;;UAIa;;EAEb;;EAEA;;;UAIa;EACb;EACA;;EAEA;;;UAIa;EACb;EACA;EACA;;;UAIa;;EAEb;;EAEA;;;cAIS,mBAAmB;EAC5B,YAAY,iBAAiB,UAAU;;;;;cChC9B;;iBAmBS,gBAClB,eACA,sBACD,QAAQ;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/audio.ts","../src/downloader.ts","../src/image.ts","../src/video.ts"],"mappings":";;;;;;;UAOiB;EACb;EACA;;;UAIa;;EAEb;;EAEA;;;UAIa;;EAEb;;EAEA;;;UAIa;EACb;EACA;;EAEA;;;UAIa;EACb;EACA;EACA;;;UAIa;;EAEb;;EAEA;;;cAIS,mBAAmB;EAC5B,YAAY,iBAAiB,UAAU;;;;;cChC9B;;iBAmBS,gBAClB,eACA,sBACD,QAAQ;;UAYM;;EAEb;;EAEA;;;UAIa;EACb;EACA;;;;;;;iBAQkB,gBAClB,eACA,UAAS,yBACV,QAAQ;;;;;;;;;iBAqDW,gBAAgB,eAAe,sBAAiB;;iBAuEhD,YAClB,eACA,sBACD,QAAQ;;;;;;;;;;;UClMM;;EAEb;;EAEA;;;;;;;iBAiBkB,YAClB,aACA,cACA,UAAS,qBACV;;iBA6Ca,eAAe,4BAA4B;;;;iBClErC,aAAa,gBAAgB,QAAQ;;iBAOrC,eAAe,gBAAgB,QAAQ;;;;iBCyBvC,eAAe,eAAe,OAAM,mBAAwB;;iBA+B5D,aAAa,gBAAgB,QAAQ"}
|
package/dist/index.d.mts
CHANGED
|
@@ -53,6 +53,24 @@ declare class MediaError extends Error {
|
|
|
53
53
|
declare const SILK_SAMPLE_RATE = 24000;
|
|
54
54
|
/** 解码 SILK 音频为 PCM(pcm_s16le)。 */
|
|
55
55
|
declare function decodeSilkToPcm(input: string, sampleRate?: number): Promise<DecodedAudio>;
|
|
56
|
+
/** 解码 SILK → WAV 输出选项。 */
|
|
57
|
+
interface DecodeSilkToWavOptions {
|
|
58
|
+
/** 采样率(缺省 24000,QQ 语音协议值)。 */
|
|
59
|
+
sampleRate?: number;
|
|
60
|
+
/** 输出目录(缺省输入同目录;收方向解码建议 tmpdir,避免污染 NT 数据目录)。 */
|
|
61
|
+
outDir?: string;
|
|
62
|
+
}
|
|
63
|
+
/** 解码结果(WAV)。 */
|
|
64
|
+
interface DecodedWav {
|
|
65
|
+
wavPath: string;
|
|
66
|
+
durationMs: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 解码 SILK → WAV 文件(pcm_s16le 单声道 + 44 字节头,通用可播放格式)。
|
|
70
|
+
* 收方向语音接线用(koishi h.audio / 外部客户端):QQ 语音是 silk v3,
|
|
71
|
+
* 多数播放器无法直接解码,落 WAV 后以本地路径消费。
|
|
72
|
+
*/
|
|
73
|
+
declare function decodeSilkToWav(input: string, options?: DecodeSilkToWavOptions): Promise<DecodedWav>;
|
|
56
74
|
/**
|
|
57
75
|
* 编码任意音频为 SILK 文件(返回输出路径,输出紧随输入文件)。
|
|
58
76
|
*
|
|
@@ -65,6 +83,30 @@ declare function encodePcmToSilk(input: string, sampleRate?: number): Promise<st
|
|
|
65
83
|
/** 获取 SILK 音频信息(时长 / 采样率)。 */
|
|
66
84
|
declare function getSilkInfo(input: string, sampleRate?: number): Promise<AudioInfo>;
|
|
67
85
|
//#endregion
|
|
86
|
+
//#region src/downloader.d.ts
|
|
87
|
+
/**
|
|
88
|
+
* downloader.ts:HTTP(S) 媒体下载(2026-09-08 T5)
|
|
89
|
+
*
|
|
90
|
+
* 协议层(adapter get_image 主动下载 / koishi 插件发送侧下沉)共用的
|
|
91
|
+
* 最小下载原语:fetch + 大小上限 + 超时,写目标路径(调用方管理目录与清理)。
|
|
92
|
+
* 零额外依赖(Node 18+ 内建 fetch)。
|
|
93
|
+
*/
|
|
94
|
+
/** 下载选项。 */
|
|
95
|
+
interface DownloadUrlOptions {
|
|
96
|
+
/** 超时毫秒(缺省 15s)。 */
|
|
97
|
+
timeoutMs?: number;
|
|
98
|
+
/** 大小上限字节(缺省 30MB)。 */
|
|
99
|
+
maxBytes?: number;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 下载 URL 到目标路径(覆盖写)。返回写入字节数。
|
|
103
|
+
* 失败抛错(非 2xx / 超限 / 超时 / 空 body / IO 错误),不产生半成品语义
|
|
104
|
+
* (失败时目标文件可能残留,调用方负责清理目录)。
|
|
105
|
+
*/
|
|
106
|
+
declare function downloadUrl(url: string, dest: string, options?: DownloadUrlOptions): Promise<number>;
|
|
107
|
+
/** 从 Content-Type / URL 路径推断扩展名(缺省 .bin;下载命名用)。 */
|
|
108
|
+
declare function inferExtension(contentType: string | null, url: string): string;
|
|
109
|
+
//#endregion
|
|
68
110
|
//#region src/image.d.ts
|
|
69
111
|
/** 读取图片尺寸(image-size 同步 API,内部读文件 + 计算)。 */
|
|
70
112
|
declare function getImageSize(input: string): Promise<ImageSize>;
|
|
@@ -72,10 +114,10 @@ declare function getImageSize(input: string): Promise<ImageSize>;
|
|
|
72
114
|
declare function detectFileType(input: string): Promise<FileTypeResult>;
|
|
73
115
|
//#endregion
|
|
74
116
|
//#region src/video.d.ts
|
|
75
|
-
/** 转码:按选项输出到替换扩展名后的路径(.mp4)。 */
|
|
117
|
+
/** 转码:按选项输出到替换扩展名后的路径(.mp4)。ffmpeg 缺失/失败抛 MediaError。 */
|
|
76
118
|
declare function transcodeVideo(input: string, opts?: TranscodeOptions): Promise<string>;
|
|
77
119
|
/** 读取视频宽高与时长(ffprobe JSON 输出解析)。 */
|
|
78
120
|
declare function getVideoInfo(input: string): Promise<VideoInfo>;
|
|
79
121
|
//#endregion
|
|
80
|
-
export { type AudioInfo, type DecodedAudio, type FileTypeResult, type ImageSize, MediaError, SILK_SAMPLE_RATE, type TranscodeOptions, type VideoInfo, decodeSilkToPcm, detectFileType, encodePcmToSilk, getImageSize, getSilkInfo, getVideoInfo, transcodeVideo };
|
|
122
|
+
export { type AudioInfo, type DecodeSilkToWavOptions, type DecodedAudio, type DecodedWav, type DownloadUrlOptions, type FileTypeResult, type ImageSize, MediaError, SILK_SAMPLE_RATE, type TranscodeOptions, type VideoInfo, decodeSilkToPcm, decodeSilkToWav, detectFileType, downloadUrl, encodePcmToSilk, getImageSize, getSilkInfo, getVideoInfo, inferExtension, transcodeVideo };
|
|
81
123
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/audio.ts","../src/image.ts","../src/video.ts"],"mappings":";;;;;;;UAOiB;EACb;EACA;;;UAIa;;EAEb;;EAEA;;;UAIa;;EAEb;;EAEA;;;UAIa;EACb;EACA;;EAEA;;;UAIa;EACb;EACA;EACA;;;UAIa;;EAEb;;EAEA;;;cAIS,mBAAmB;EAC5B,YAAY,iBAAiB,UAAU;;;;;cChC9B;;iBAmBS,gBAClB,eACA,sBACD,QAAQ;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/audio.ts","../src/downloader.ts","../src/image.ts","../src/video.ts"],"mappings":";;;;;;;UAOiB;EACb;EACA;;;UAIa;;EAEb;;EAEA;;;UAIa;;EAEb;;EAEA;;;UAIa;EACb;EACA;;EAEA;;;UAIa;EACb;EACA;EACA;;;UAIa;;EAEb;;EAEA;;;cAIS,mBAAmB;EAC5B,YAAY,iBAAiB,UAAU;;;;;cChC9B;;iBAmBS,gBAClB,eACA,sBACD,QAAQ;;UAYM;;EAEb;;EAEA;;;UAIa;EACb;EACA;;;;;;;iBAQkB,gBAClB,eACA,UAAS,yBACV,QAAQ;;;;;;;;;iBAqDW,gBAAgB,eAAe,sBAAiB;;iBAuEhD,YAClB,eACA,sBACD,QAAQ;;;;;;;;;;;UClMM;;EAEb;;EAEA;;;;;;;iBAiBkB,YAClB,aACA,cACA,UAAS,qBACV;;iBA6Ca,eAAe,4BAA4B;;;;iBClErC,aAAa,gBAAgB,QAAQ;;iBAOrC,eAAe,gBAAgB,QAAQ;;;;iBCyBvC,eAAe,eAAe,OAAM,mBAAwB;;iBA+B5D,aAAa,gBAAgB,QAAQ"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { randomBytes } from "node:crypto";
|
|
3
|
-
import fs, { appendFileSync, closeSync, createReadStream, createWriteStream, mkdirSync, openSync, readFileSync, readSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import fs, { appendFileSync, closeSync, createReadStream, createWriteStream, existsSync, mkdirSync, openSync, readFileSync, readSync, statSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { readFile, rm, writeFile } from "node:fs/promises";
|
|
5
5
|
import path, { dirname, join } from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
@@ -6423,6 +6423,55 @@ async function decodeSilkToPcm(input, sampleRate = SILK_SAMPLE_RATE) {
|
|
|
6423
6423
|
};
|
|
6424
6424
|
}
|
|
6425
6425
|
/**
|
|
6426
|
+
* 解码 SILK → WAV 文件(pcm_s16le 单声道 + 44 字节头,通用可播放格式)。
|
|
6427
|
+
* 收方向语音接线用(koishi h.audio / 外部客户端):QQ 语音是 silk v3,
|
|
6428
|
+
* 多数播放器无法直接解码,落 WAV 后以本地路径消费。
|
|
6429
|
+
*/
|
|
6430
|
+
async function decodeSilkToWav(input, options = {}) {
|
|
6431
|
+
const sampleRate = options.sampleRate ?? 24e3;
|
|
6432
|
+
const buffer = await readFile(input);
|
|
6433
|
+
if (!isSilk(buffer)) throw new MediaError(`不是有效的 SILK 文件: ${input}`);
|
|
6434
|
+
const result = await decode(buffer, sampleRate);
|
|
6435
|
+
const { dir, name } = outputTarget(input, ".wav", options.outDir);
|
|
6436
|
+
const wav = wrapPcmInWav(result.data, sampleRate);
|
|
6437
|
+
await writeFile(join(dir, name), wav);
|
|
6438
|
+
return {
|
|
6439
|
+
wavPath: join(dir, name),
|
|
6440
|
+
durationMs: result.duration
|
|
6441
|
+
};
|
|
6442
|
+
}
|
|
6443
|
+
/** 输出目标(目录 + 文件名;outDir 优先,否则输入同目录替换扩展名)。 */
|
|
6444
|
+
function outputTarget(input, ext, outDir) {
|
|
6445
|
+
const base = input.replaceAll("\\", "/").split("/").pop() ?? "audio";
|
|
6446
|
+
const dot = base.lastIndexOf(".");
|
|
6447
|
+
const name = `${dot > 0 ? base.slice(0, dot) : base}${ext}`;
|
|
6448
|
+
const dir = outDir !== void 0 ? outDir : dirname(input);
|
|
6449
|
+
mkdirSync(dir, { recursive: true });
|
|
6450
|
+
return {
|
|
6451
|
+
dir,
|
|
6452
|
+
name
|
|
6453
|
+
};
|
|
6454
|
+
}
|
|
6455
|
+
/** PCM(s16le 单声道)→ WAV 文件字节(44 字节头 + 数据)。 */
|
|
6456
|
+
function wrapPcmInWav(pcm, sampleRate) {
|
|
6457
|
+
const blockAlign = 2;
|
|
6458
|
+
const header = Buffer.alloc(44);
|
|
6459
|
+
header.write("RIFF", 0, "ascii");
|
|
6460
|
+
header.writeUInt32LE(36 + pcm.byteLength, 4);
|
|
6461
|
+
header.write("WAVE", 8, "ascii");
|
|
6462
|
+
header.write("fmt ", 12, "ascii");
|
|
6463
|
+
header.writeUInt32LE(16, 16);
|
|
6464
|
+
header.writeUInt16LE(1, 20);
|
|
6465
|
+
header.writeUInt16LE(1, 22);
|
|
6466
|
+
header.writeUInt32LE(sampleRate, 24);
|
|
6467
|
+
header.writeUInt32LE(sampleRate * blockAlign | 0, 28);
|
|
6468
|
+
header.writeUInt16LE(blockAlign, 32);
|
|
6469
|
+
header.writeUInt16LE(16, 34);
|
|
6470
|
+
header.write("data", 36, "ascii");
|
|
6471
|
+
header.writeUInt32LE(pcm.byteLength, 40);
|
|
6472
|
+
return Buffer.concat([header, Buffer.from(pcm)]);
|
|
6473
|
+
}
|
|
6474
|
+
/**
|
|
6426
6475
|
* 编码任意音频为 SILK 文件(返回输出路径,输出紧随输入文件)。
|
|
6427
6476
|
*
|
|
6428
6477
|
* - 已是 SILK → 原样返回
|
|
@@ -6481,6 +6530,66 @@ async function getSilkInfo(input, sampleRate = SILK_SAMPLE_RATE) {
|
|
|
6481
6530
|
};
|
|
6482
6531
|
}
|
|
6483
6532
|
//#endregion
|
|
6533
|
+
//#region src/downloader.ts
|
|
6534
|
+
/** 默认大小上限(30MB)。 */
|
|
6535
|
+
const DEFAULT_MAX_BYTES = 31457280;
|
|
6536
|
+
/** 默认超时(15s)。 */
|
|
6537
|
+
const DEFAULT_TIMEOUT_MS = 15e3;
|
|
6538
|
+
/** 扩展名字符合法性(URL pathname 提取校验)。 */
|
|
6539
|
+
const EXT_RE = /^\.[a-z0-9]+$/i;
|
|
6540
|
+
/**
|
|
6541
|
+
* 下载 URL 到目标路径(覆盖写)。返回写入字节数。
|
|
6542
|
+
* 失败抛错(非 2xx / 超限 / 超时 / 空 body / IO 错误),不产生半成品语义
|
|
6543
|
+
* (失败时目标文件可能残留,调用方负责清理目录)。
|
|
6544
|
+
*/
|
|
6545
|
+
async function downloadUrl(url, dest, options = {}) {
|
|
6546
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
6547
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
6548
|
+
const response = await fetch(url, {
|
|
6549
|
+
redirect: "follow",
|
|
6550
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
6551
|
+
});
|
|
6552
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
6553
|
+
const declared = Number(response.headers.get("content-length") ?? "0");
|
|
6554
|
+
if (declared > maxBytes) throw new Error(`文件超过大小上限(${declared} > ${maxBytes} 字节)`);
|
|
6555
|
+
const body = response.body;
|
|
6556
|
+
if (body === null) throw new Error("响应无 body");
|
|
6557
|
+
const reader = body.getReader();
|
|
6558
|
+
const chunks = [];
|
|
6559
|
+
let total = 0;
|
|
6560
|
+
for (;;) {
|
|
6561
|
+
const { done, value } = await reader.read();
|
|
6562
|
+
if (done) break;
|
|
6563
|
+
if (value === void 0) continue;
|
|
6564
|
+
total += value.byteLength;
|
|
6565
|
+
if (total > maxBytes) {
|
|
6566
|
+
await reader.cancel().catch(() => void 0);
|
|
6567
|
+
throw new Error(`文件超过大小上限(>${maxBytes} 字节)`);
|
|
6568
|
+
}
|
|
6569
|
+
chunks.push(value);
|
|
6570
|
+
}
|
|
6571
|
+
if (total === 0) throw new Error("下载内容为空");
|
|
6572
|
+
const { writeFile } = await import("node:fs/promises");
|
|
6573
|
+
await writeFile(dest, Buffer.concat(chunks));
|
|
6574
|
+
return total;
|
|
6575
|
+
}
|
|
6576
|
+
/** 从 Content-Type / URL 路径推断扩展名(缺省 .bin;下载命名用)。 */
|
|
6577
|
+
function inferExtension(contentType, url) {
|
|
6578
|
+
if (contentType !== null) {
|
|
6579
|
+
const base = contentType.split(";")[0]?.trim().toLowerCase() ?? "";
|
|
6580
|
+
const sub = base.split("/")[1]?.split("+")[0];
|
|
6581
|
+
if (base.startsWith("image/") || base.startsWith("audio/") || base.startsWith("video/")) {
|
|
6582
|
+
if (sub !== void 0 && sub !== "") return `.${sub}`;
|
|
6583
|
+
}
|
|
6584
|
+
}
|
|
6585
|
+
try {
|
|
6586
|
+
const pathname = new URL(url).pathname;
|
|
6587
|
+
const ext = pathname.substring(pathname.lastIndexOf("."));
|
|
6588
|
+
if (ext.length > 1 && ext.length <= 6 && EXT_RE.test(ext)) return ext.toLowerCase();
|
|
6589
|
+
} catch {}
|
|
6590
|
+
return ".bin";
|
|
6591
|
+
}
|
|
6592
|
+
//#endregion
|
|
6484
6593
|
//#region ../../node_modules/.pnpm/@borewit+text-codec@0.2.2/node_modules/@borewit/text-codec/lib/index.js
|
|
6485
6594
|
const WINDOWS_1252_EXTRA = {
|
|
6486
6595
|
128: "€",
|
|
@@ -11383,7 +11492,7 @@ function replaceExt(input, ext) {
|
|
|
11383
11492
|
else stem = base;
|
|
11384
11493
|
return `${dir}${stem}${ext}`;
|
|
11385
11494
|
}
|
|
11386
|
-
/** 转码:按选项输出到替换扩展名后的路径(.mp4)。 */
|
|
11495
|
+
/** 转码:按选项输出到替换扩展名后的路径(.mp4)。ffmpeg 缺失/失败抛 MediaError。 */
|
|
11387
11496
|
async function transcodeVideo(input, opts = {}) {
|
|
11388
11497
|
const args = [
|
|
11389
11498
|
"-y",
|
|
@@ -11396,11 +11505,13 @@ async function transcodeVideo(input, opts = {}) {
|
|
|
11396
11505
|
if (opts.fps !== void 0) args.push("-r", String(opts.fps));
|
|
11397
11506
|
const output = replaceExt(input, ".mp4");
|
|
11398
11507
|
args.push("-c:v", "libx264", "-preset", "fast", output);
|
|
11508
|
+
let exitCode;
|
|
11399
11509
|
try {
|
|
11400
|
-
await execa("ffmpeg", args, { reject: false });
|
|
11510
|
+
exitCode = (await execa("ffmpeg", args, { reject: false })).exitCode;
|
|
11401
11511
|
} catch (err) {
|
|
11402
|
-
throw new MediaError(`ffmpeg
|
|
11512
|
+
throw new MediaError(`ffmpeg 不可用或转码失败: ${input}`, { cause: err });
|
|
11403
11513
|
}
|
|
11514
|
+
if (exitCode !== 0 || !existsSync(output)) throw new MediaError(`ffmpeg 转码失败: ${input}(exit ${exitCode ?? "?"})`);
|
|
11404
11515
|
return output;
|
|
11405
11516
|
}
|
|
11406
11517
|
/** 读取视频宽高与时长(ffprobe JSON 输出解析)。 */
|
|
@@ -11434,6 +11545,6 @@ async function getVideoInfo(input) {
|
|
|
11434
11545
|
};
|
|
11435
11546
|
}
|
|
11436
11547
|
//#endregion
|
|
11437
|
-
export { MediaError, SILK_SAMPLE_RATE, decodeSilkToPcm, detectFileType, encodePcmToSilk, getImageSize, getSilkInfo, getVideoInfo, transcodeVideo };
|
|
11548
|
+
export { MediaError, SILK_SAMPLE_RATE, decodeSilkToPcm, decodeSilkToWav, detectFileType, downloadUrl, encodePcmToSilk, getImageSize, getSilkInfo, getVideoInfo, inferExtension, transcodeVideo };
|
|
11438
11549
|
|
|
11439
11550
|
//# sourceMappingURL=index.mjs.map
|