Skip to content

Commit

Permalink
新增 save_markdown 可选项
Browse files Browse the repository at this point in the history
原版 save_audio = False 时,会直接不写md记录。现在将逻辑分开控制。
  • Loading branch information
H1DDENADM1N committed Dec 3, 2024
1 parent dcb45b5 commit b2260b6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ClientConfig:
restore_clipboard_after_paste = True # 模拟粘贴后是否恢复剪贴板

save_audio = True # 是否保存录音文件
save_markdown = True # 是否将记录写入 md 文件
audio_name_len = 20 # 将录音识别结果的前多少个字存储到录音文件名中,建议不要超过200
reduce_audio_files = True # 如果用户已安装 ffmpeg ,调用 ffmpeg 录音时输出 mp3 格式的音频文件,大大减小文件体积,减少磁盘占用

Expand Down
9 changes: 8 additions & 1 deletion util/client_recv_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ async def recv_result():
file_audio = rename_audio(
message["task_id"], text, message["time_start"]
)
else:
file_audio = None

if Config.save_markdown:
# 记录写入 md 文件
write_md(text, message["time_start"], file_audio)
if Config.convert_to_traditional_chinese:
write_md(traditional_text, message["time_start"], file_audio)
else:
write_md(text, message["time_start"], file_audio)

# 控制台输出
console.print(f" 转录时延:{delay:.2f}s")
Expand Down
46 changes: 27 additions & 19 deletions util/client_write_md.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from util.hot_kwds import kwd_list
import time
from pathlib import Path
from os import makedirs
from pathlib import Path

from util.hot_kwds import kwd_list

# def do_updata_kwd(kwd_text: str):
# """
Expand All @@ -16,7 +17,7 @@
# kwd_list.append(kwd)
# return len(kwd_list)

header_md = r'''```txt
header_md = r"""```txt
正则表达式 Tip
匹配到音频文件链接:\[(.+)\]\((.{10,})\)[\s]*
Expand All @@ -27,38 +28,45 @@
```
'''
"""


def create_md(file_md):
with open(file_md, 'w', encoding="utf-8") as f:
with open(file_md, "w", encoding="utf-8") as f:
f.write(header_md)


def write_md(text: str, time_start: float, file_audio: Path):


time_year = time.strftime('%Y', time.localtime(time_start))
time_month = time.strftime('%m', time.localtime(time_start))
time_day = time.strftime('%d', time.localtime(time_start))
time_hms = time.strftime('%H:%M:%S', time.localtime(time_start))
time_year = time.strftime("%Y", time.localtime(time_start))
time_month = time.strftime("%m", time.localtime(time_start))
time_day = time.strftime("%d", time.localtime(time_start))
time_hms = time.strftime("%H:%M:%S", time.localtime(time_start))
folder_path = Path() / time_year / time_month
makedirs(folder_path, exist_ok=True)

# 列表内的元素是元组,元组内包含了:关键词、md路径
md_list = [(kwd, folder_path / f'{kwd + "-" if kwd else ""}{time_day}.md')
for kwd in kwd_list
if text.startswith(kwd)]
md_list = [
(kwd, folder_path / f'{kwd + "-" if kwd else ""}{time_day}.md')
for kwd in kwd_list
if text.startswith(kwd)
]

# 为 md 文件写入识别记录
for kwd, file_md in md_list:

# 确保 md 文件存在
if not file_md.exists():
create_md(file_md)

# 写入 md
with open(file_md, 'a', encoding="utf-8") as f:
path_ = file_audio.relative_to(file_md.parent).as_posix().replace(" ", "%20")
text_ = text[len(kwd):].lstrip(",。,.")
f.write(f'[{time_hms}]({path_}) {text_}\n\n')
with open(file_md, "a", encoding="utf-8") as f:
text_ = text[len(kwd) :].lstrip(",。,.")
if file_audio is not None:
path_ = (
file_audio.relative_to(file_md.parent)
.as_posix()
.replace(" ", "%20")
)
f.write(f"[{time_hms}]({path_}) {text_}\n\n")
else:
# path_ = ""
f.write(f"[{time_hms}]() {text_}\n\n")

0 comments on commit b2260b6

Please sign in to comment.