增加tts初步支持,大文件存储改用lfs
This commit is contained in:
1 parent
cd305af1fb
commit
e03614a674
12 files changed
+121
-274
No files matched your search
+2
-1
@@ -1,2 +1,3 @@
|
||||
./outputs/
|
||||
|
||||
outputs/*
|
||||
private_key.asc
|
||||
@@ -5,6 +5,7 @@
|
||||
"imports": {
|
||||
"@std/assert": "jsr:@std/assert@1",
|
||||
"@std/toml": "jsr:@std/toml@^1.0.11",
|
||||
"fs": "npm:fs@^0.0.1-security",
|
||||
"markdown-it": "npm:markdown-it@^14.1.1",
|
||||
"openpgp": "npm:openpgp@^6.3.0",
|
||||
"pdf-lib": "npm:pdf-lib@^1.17.1",
|
||||
|
||||
Generated
+5
@@ -5,6 +5,7 @@
|
||||
"jsr:@std/collections@^1.1.3": "1.1.6",
|
||||
"jsr:@std/internal@^1.0.12": "1.0.12",
|
||||
"jsr:@std/toml@^1.0.11": "1.0.11",
|
||||
"npm:fs@^0.0.1-security": "0.0.1-security",
|
||||
"npm:markdown-it@^14.1.1": "14.1.1",
|
||||
"npm:openpgp@^6.3.0": "6.3.0",
|
||||
"npm:pdf-lib@^1.17.1": "1.17.1",
|
||||
@@ -119,6 +120,9 @@
|
||||
"[email protected]": {
|
||||
"integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw=="
|
||||
},
|
||||
"[email protected]": {
|
||||
"integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w=="
|
||||
},
|
||||
"[email protected]": {
|
||||
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
|
||||
"os": ["darwin"],
|
||||
@@ -244,6 +248,7 @@
|
||||
"dependencies": [
|
||||
"jsr:@std/assert@1",
|
||||
"jsr:@std/toml@^1.0.11",
|
||||
"npm:fs@^0.0.1-security",
|
||||
"npm:markdown-it@^14.1.1",
|
||||
"npm:openpgp@^6.3.0",
|
||||
"npm:pdf-lib@^1.17.1",
|
||||
|
||||
+31
-13
@@ -32,17 +32,35 @@ import { PDFParse } from "pdf-parse";
|
||||
// },
|
||||
// });
|
||||
|
||||
Deno.test({
|
||||
name: "OpenPGPSigner test",
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
async fn() {
|
||||
const { OpenPGPSigner } = await import("./pdf_builder/builder.ts");
|
||||
const signer = new OpenPGPSigner(
|
||||
"./outputs/mobile.pdf",
|
||||
"./private_key.asc",
|
||||
"./outputs/mobile.pdf.sig",
|
||||
);
|
||||
await signer.sign();
|
||||
},
|
||||
// Deno.test({
|
||||
// name: "OpenPGPSigner test",
|
||||
// sanitizeOps: false,
|
||||
// sanitizeResources: false,
|
||||
// async fn() {
|
||||
// const { OpenPGPSigner } = await import("./pdf_builder/builder.ts");
|
||||
// const signer = new OpenPGPSigner(
|
||||
// "./outputs/mobile.pdf",
|
||||
// "./private_key.asc",
|
||||
// "./outputs/mobile.pdf.sig",
|
||||
// );
|
||||
// await signer.sign();
|
||||
// },
|
||||
// });
|
||||
import { writeFileSync } from "node:fs";
|
||||
Deno.test("TTS test", async () => {
|
||||
const { PiperCloudTTS } = await import("./pdf_builder/tts.ts");
|
||||
|
||||
const tts = new PiperCloudTTS("https://piper.cmlmuf.org/");
|
||||
const contents = Deno.readTextFileSync(
|
||||
"./outputs/工农解放报RSF续刊第一期-适合手机阅读.md",
|
||||
);
|
||||
|
||||
await tts.stream_to_file(
|
||||
contents,
|
||||
"zh_CN-xiao_ya-medium",
|
||||
"./outputs/test_audio.wav",
|
||||
1000,
|
||||
);
|
||||
|
||||
console.log("Audio streaming completed to ./outputs/test_audio.wav");
|
||||
});
|
||||
@@ -6,7 +6,7 @@ RSF续刊第一期
|
||||
|
||||
工农解放社-原革命社会主义阵地(RSF)- 2026.5.16发布
|
||||
|
||||
/\*基础格式部分\*/ .global { font-family: "AlimamaFangYuanTiVF-Thin", sans-serif; font-size: var(--font-size); margin: 1rem 2rem 2rem 2rem; line-height: 2.5rem; /\* letter-spacing: -0.15rem; \*/ } .global h1 { line-height: 4rem; font-family: "HongLei", sans-serif; } html, body { margin: 0; padding: 0; --font-size: 2rem; } /\*控件美化部分\*/ .c\_title { font-size: 3.5rem; color: white; background-color: red; border: 0.18rem solid red; display: inline-flex; width: fit-content; line-height: 1.5; padding: 0.08em 0.24em; white-space: normal; word-break: break-all; font-family: "ZCOOL QingKe HuangYou", sans-serif; margin: 2rem 1rem 0rem 2rem; border-radius: 0.5rem; overflow-wrap: break-word; max-width: 21cm; } blockquote { background-color: #f0f0f0; border-left: 0.5rem solid #ccc; padding: 0.5rem 1rem; margin: 1rem 0; color: #666; } .global img { max-width: 100%; height: auto; border-radius: 12px; border: 4px solid linear-gradient(45deg, #e799b1, #44b144, #2f90ba); box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1); display: block; margin: 1rem auto; /\* color: transparent; \*/ overflow: hidden; } .global a\[href\] { color: rgb(0, 180, 120); text-decoration: none; } /\*目录美化部分\*/ index h1 { font-size: 3.5rem; color: white; background-color: red; border: 0.18rem solid red; display: inline-flex; width: fit-content; line-height: 1.5; padding: 0.08em 0.24em; white-space: normal; word-break: break-all; font-family: "ZCOOL QingKe HuangYou", sans-serif; margin: 2rem 1rem 0rem 2rem; border-radius: 0.5rem; overflow-wrap: break-word; max-width: 21cm; } index a { text-decoration: none; font-size: var(--font-size); display: flex; width: 100%; justify-content: space-between; align-items: flex-start; gap: 0.75rem; } index ul { margin: 1rem 2rem 3rem 2rem; /\* padding: 0.5rem 1rem; \*/ } index li { list-style: none; display: flex; width: 100%; justify-content: space-between; align-items: flex-start; word-break: break-all; word-wrap: normal; line-height: 1.5; } index li page\_title { color: black; font-size: var(--font-size); font-weight: thin; position: static; flex: 1 1 auto; min-width: 0; white-space: normal; word-break: break-word; overflow-wrap: anywhere; } index li page\_num { color: black; font-size: var(--font-size); position: static; flex: 0 0 auto; min-width: 4ch; margin-left: 1rem; white-space: nowrap; text-align: right; font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; } @font-face { font-family: "Liu Jian Mao Cao"; src: url("http://127.0.0.1:8963/fonts/LiuJianMaoCao-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "YangRenDongZhuShiTi"; src: url("http://127.0.0.1:8963/fonts/YangRenDongZhuShiTi-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "ZCOOL QingKe HuangYou"; src: url("http://127.0.0.1:8963/fonts/ZCOOLQingKeHuangYou-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "ZCOOLKuaiLe"; src: url("http://127.0.0.1:8963/fonts/ZCOOLKuaiLe-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "MaShanZheng"; src: url("http://127.0.0.1:8963/fonts/MaShanZheng-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "HongLei"; src: url("http://127.0.0.1:8963/fonts/HongLei.otf") format("opentype"); font-display: swap; } @font-face { font-family: "Kai"; src: url("http://127.0.0.1:8963/fonts/Kai.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "AlimamaFangYuanTiVF-Thin"; src: url("http://127.0.0.1:8963/fonts/AlimamaFangYuanTiVF-Thin.woff2") format("woff2"); font-display: swap; } .zcool-qingke-huangyou-regular { font-family: "ZCOOL QingKe HuangYou", sans-serif; font-weight: 400; font-style: normal; } .liu-jian-mao-cao-regular { font-family: "Liu Jian Mao Cao", cursive; font-weight: 400; font-style: normal; } .zcool-kuai-le-regular { font-family: "ZCOOLKuaiLe", cursive; font-weight: 400; font-style: normal; } .ma-shan-zheng-regular { font-family: "MaShanZheng", cursive; font-weight: 400; font-style: normal; } .hong-lei-regular { font-family: "HongLei", regular; font-weight: 400; font-style: normal; } .yang-ren-dong-zhu-shi-ti-regular { font-family: "YangRenDongZhuShiTi", regular; font-weight: 400; font-style: normal; } .kai-regular { font-family: "Kai", regular; font-weight: 400; font-style: normal; } .alimama-fang-yuan-ti-vf-thin-regular { font-family: "AlimamaFangYuanTiVF-Thin", regular; font-weight: 400; font-style: normal; } 关于我们
|
||||
关于我们
|
||||
|
||||
关于我们
|
||||
====
|
||||
|
||||
@@ -6,7 +6,7 @@ RSF续刊第一期
|
||||
|
||||
工农解放社-原革命社会主义阵地(RSF)- 2026.5.16发布
|
||||
|
||||
/\*基础格式部分\*/ .global { font-family: "AlimamaFangYuanTiVF-Thin", sans-serif; font-size: var(--font-size); margin: 1rem 2rem 2rem 2rem; line-height: 1.5rem; /\* letter-spacing: -0.15rem; \*/ } .global h1 { line-height: 4rem; font-family: "HongLei", sans-serif; } html, body { margin: 0; padding: 0; --font-size: 1.25rem; } /\*控件美化部分\*/ .c\_title { font-size: 3.5rem; color: white; background-color: red; border: 0.18rem solid red; display: inline-flex; width: fit-content; line-height: 1.5; padding: 0.08em 0.24em; white-space: normal; word-break: break-all; font-family: "ZCOOL QingKe HuangYou", sans-serif; margin: 2rem 1rem 0rem 2rem; border-radius: 0.5rem; overflow-wrap: break-word; max-width: 21cm; } blockquote { background-color: #f0f0f0; border-left: 0.5rem solid #ccc; padding: 0.5rem 1rem; margin: 1rem 0; color: #666; } .global img { height: 100%; object-fit: scale-down; max-width: 100%; max-height: 25cm; border-radius: 12px; border: 4px solid linear-gradient(45deg, #e799b1, #44b144, #2f90ba); box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1); display: block; margin: 1rem auto; /\* color: transparent; \*/ /\* overflow: hidden; \*/ } .global a\[href\] { color: rgb(0, 180, 120); text-decoration: none; } /\*目录美化部分\*/ index h1 { font-size: 3.5rem; color: white; background-color: red; border: 0.18rem solid red; display: inline-flex; width: fit-content; line-height: 1.5; padding: 0.08em 0.24em; white-space: normal; word-break: break-all; font-family: "ZCOOL QingKe HuangYou", sans-serif; margin: 2rem 1rem 0rem 2rem; border-radius: 0.5rem; overflow-wrap: break-word; max-width: 21cm; } index a { text-decoration: none; font-size: var(--font-size); display: flex; width: 100%; justify-content: space-between; align-items: flex-start; gap: 0.75rem; } index ul { margin: 1rem 2rem 3rem 2rem; /\* padding: 0.5rem 1rem; \*/ } index li { list-style: none; display: flex; width: 100%; justify-content: space-between; align-items: flex-start; word-break: break-all; word-wrap: normal; line-height: 1.5; } index li page\_title { color: black; font-size: var(--font-size); font-weight: thin; position: static; flex: 1 1 auto; min-width: 0; white-space: normal; word-break: break-word; overflow-wrap: anywhere; } index li page\_num { color: black; font-size: var(--font-size); position: static; flex: 0 0 auto; min-width: 4ch; margin-left: 1rem; white-space: nowrap; text-align: right; font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; } @font-face { font-family: "Liu Jian Mao Cao"; src: url("http://127.0.0.1:8963/fonts/LiuJianMaoCao-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "YangRenDongZhuShiTi"; src: url("http://127.0.0.1:8963/fonts/YangRenDongZhuShiTi-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "ZCOOL QingKe HuangYou"; src: url("http://127.0.0.1:8963/fonts/ZCOOLQingKeHuangYou-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "ZCOOLKuaiLe"; src: url("http://127.0.0.1:8963/fonts/ZCOOLKuaiLe-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "MaShanZheng"; src: url("http://127.0.0.1:8963/fonts/MaShanZheng-Regular.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "HongLei"; src: url("http://127.0.0.1:8963/fonts/HongLei.otf") format("opentype"); font-display: swap; } @font-face { font-family: "Kai"; src: url("http://127.0.0.1:8963/fonts/Kai.ttf") format("truetype"); font-display: swap; } @font-face { font-family: "AlimamaFangYuanTiVF-Thin"; src: url("http://127.0.0.1:8963/fonts/AlimamaFangYuanTiVF-Thin.woff2") format("woff2"); font-display: swap; } .zcool-qingke-huangyou-regular { font-family: "ZCOOL QingKe HuangYou", sans-serif; font-weight: 400; font-style: normal; } .liu-jian-mao-cao-regular { font-family: "Liu Jian Mao Cao", cursive; font-weight: 400; font-style: normal; } .zcool-kuai-le-regular { font-family: "ZCOOLKuaiLe", cursive; font-weight: 400; font-style: normal; } .ma-shan-zheng-regular { font-family: "MaShanZheng", cursive; font-weight: 400; font-style: normal; } .hong-lei-regular { font-family: "HongLei", regular; font-weight: 400; font-style: normal; } .yang-ren-dong-zhu-shi-ti-regular { font-family: "YangRenDongZhuShiTi", regular; font-weight: 400; font-style: normal; } .kai-regular { font-family: "Kai", regular; font-weight: 400; font-style: normal; } .alimama-fang-yuan-ti-vf-thin-regular { font-family: "AlimamaFangYuanTiVF-Thin", regular; font-weight: 400; font-style: normal; } 关于我们
|
||||
关于我们
|
||||
|
||||
关于我们
|
||||
====
|
||||
|
||||
@@ -4,7 +4,7 @@ RSF续刊第一期
|
||||
|
||||
工农解放社-原革命社会主义阵地(RSF)- 2026.5.16发布
|
||||
|
||||
/\*基础格式部分\*/ .global { font-family: "R-PMingLiU", sans-serif; font-size: var(--font-size); margin: 1rem 4rem 2rem 4rem; line-height: 20px; /\* letter-spacing: -0.15rem; \*/ } .global h1 { line-height: 2rem; font-family: "R-PMingLiU", sans-serif; } .global p { margin: 15px 0; } html, body { margin: 0; padding: 0; --font-size: 16px; } /\*控件美化部分\*/ .c\_title { font-size: 2rem; color: red; display: inline-flex; width: fit-content; line-height: 1.5; padding: 0.08em 0.24em; white-space: normal; word-break: break-all; font-family: "R-PMingLiU", sans-serif; margin: 2rem 1rem 0rem 2rem; overflow-wrap: break-word; max-width: 21cm; } blockquote { border-left: 0.5rem solid #ccc; padding: 0.5rem 1rem; margin: 1rem 0; border-radius: 22px; border-color: #666; } .global img { height: 100%; object-fit: scale-down; max-width: 45%; max-height: 10cm; border-radius: 12px; border: 4px solid linear-gradient(45deg, #e799b1, #44b144, #2f90ba); box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1); display: block; margin: 1rem auto; /\* color: transparent; \*/ /\* overflow: hidden; \*/ } .global a\[href\] { color: rgb(0, 180, 120); text-decoration: none; } /\*目录美化部分\*/ index h1 { font-size: 1rem; color: black; border: 0.18rem solid red; display: inline-flex; width: fit-content; line-height: 1.5; padding: 0.08em 0.24em; white-space: normal; word-break: break-all; font-family: "R-PMingLiU", sans-serif; margin: 2rem 1rem 0rem 2rem; border-radius: 0.5rem; overflow-wrap: break-word; max-width: 21cm; } index a { text-decoration: none; font-size: var(--font-size); display: flex; width: 100%; justify-content: space-between; align-items: flex-start; gap: 0.75rem; } index ul { margin: 1rem 2rem 3rem 2rem; /\* padding: 0.5rem 1rem; \*/ } index li { list-style: none; display: flex; width: 100%; justify-content: space-between; align-items: flex-start; word-break: break-all; word-wrap: normal; line-height: 1.5; } index li page\_title { color: black; font-size: var(--font-size); font-weight: thin; position: static; flex: 1 1 auto; min-width: 0; white-space: normal; word-break: break-word; overflow-wrap: anywhere; } index li page\_num { color: black; font-size: var(--font-size); position: static; flex: 0 0 auto; min-width: 4ch; margin-left: 1rem; white-space: nowrap; text-align: right; font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; } @font-face { font-family: "R-PMingLiU"; src: url("http://127.0.0.1:8963/fonts/R-PMingLiU.ttf") format("truetype"); font-display: swap; } .r-pmingliu-regular { font-family: "R-PMingLiU", serif; } 关于我们
|
||||
关于我们
|
||||
|
||||
关于我们
|
||||
====
|
||||
|
||||
@@ -52,8 +52,15 @@ export class DocuBuilder {
|
||||
|
||||
async as_markdown() {
|
||||
const turndownService = new TurndownService();
|
||||
const md_page = turndownService.turndown(await this.as_html_page());
|
||||
|
||||
const html_page = await this.as_html_page();
|
||||
const cleaned_html = html_page.replace(
|
||||
`/<[^>]class="[^"]\bc_title\b[^"]"[^>]>([\s\S]*?)</[^>]+>/gi`,
|
||||
"# $1",
|
||||
).replace(
|
||||
/<script\b[^>]*>[\s\S]*?<\/script>|<style\b[^>]*>[\s\S]*?<\/style>/gi,
|
||||
"",
|
||||
);
|
||||
const md_page = turndownService.turndown(cleaned_html);
|
||||
this.pages = [md_page];
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
export interface TTS {
|
||||
say_all(content: string, voice: string): Promise<ArrayBuffer>;
|
||||
}
|
||||
|
||||
export class PiperCloudTTS {
|
||||
api_url: string;
|
||||
|
||||
constructor(api_url: string) {
|
||||
this.api_url = api_url;
|
||||
}
|
||||
|
||||
async say_all(content: string, voice: string): Promise<ArrayBuffer> {
|
||||
const response = await fetch(this.api_url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "Mozilla/5.0",
|
||||
},
|
||||
body: JSON.stringify({ text: content, voice: voice }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(
|
||||
`Failed to fetch ${this.api_url}: ${response.status} ${response.statusText} - ${errorText}`,
|
||||
);
|
||||
}
|
||||
|
||||
return await response.arrayBuffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将长文本分段并直接流式写入文件
|
||||
*/
|
||||
async stream_to_file(
|
||||
content: string,
|
||||
voice: string,
|
||||
outputPath: string,
|
||||
chunkSize = 1000,
|
||||
): Promise<void> {
|
||||
const file = await Deno.open(outputPath, {
|
||||
write: true,
|
||||
create: true,
|
||||
truncate: true,
|
||||
});
|
||||
|
||||
try {
|
||||
for (let i = 0; i < content.length; i += chunkSize) {
|
||||
const chunk = content.slice(i, i + chunkSize);
|
||||
console.log(`Processing chunk ${i / chunkSize + 1}...`);
|
||||
|
||||
const audioData = await this.say_all(chunk, voice);
|
||||
// WAV 文件通常包含头。注意:如果只是简单合并 WAV,
|
||||
// 只有第一个 chunk 的头是有效的,后续 chunk 的头会被视为数据(可能会有杂音)。
|
||||
// 如果 API 返回的是原始 PCM,则没问题。
|
||||
// 这里我们先按照用户要求的流式写入实现。
|
||||
await file.write(new Uint8Array(audioData));
|
||||
}
|
||||
} finally {
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user