听书党福利:用Python把电子书转换成mp3

作者: shisaq 日期: April 7, 2023

准备工作

  • txt电子书
  • 网络
  • 安装好edge-tts $ pip install edge-tts
  • 安装好Python3

基础用例

$ edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt

更多用例,可参阅Edge-tts Usage

用Python把电子书拆分成小的章节

假设我们有名为“白鹿原.txt”的电子书。在该电子书的同级目录下,创建“split.py”文件,把下方代码粘贴进去。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 打开电子书
with open("白鹿原.txt", "r") as input_file:
    # 读电子书的内容
    file_contents = input_file.read()
    
    # 用“\n\n第”来分隔不同的章节(不同的电子书结构不同,请手动确认分隔符)
    file_chunks = file_contents.split("\n\n第")
    
    # 分割电子书
    for i, file_chunk in enumerate(file_chunks):
        # 为输出的电子书命名
        output_filename = f"白鹿原_{i+1}.txt"
        
        # 打开输出的电子书
        with open(output_filename, "w") as output_file:
            # 写入分割出来的内容,并在命令行提示结果
            output_file.write("第" + file_chunk)
            print("\n finishing %s" % output_file)

# 完成
print ("done.")

用edge-tts把章节转换成mp3

把输出的章节txt统一放入“splitted”文件夹中,在该文件夹中创建“txt_to_audio.py”文件,把下方代码粘贴进去:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os
import subprocess

# 指定工作目录和输出文件夹
txt_path = "./"
output_folder = "output"

# 创建输出文件夹
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# 遍历该目录下的所有txt文件
for txt_file in os.listdir(txt_path):
    # 检查是否是txt文件
    if txt_file.endswith(".txt"):
        # 创建终端命令(参考edge-tts命令)
        cmd = f"edge-tts -f {txt_path}/{txt_file} --write-media {output_folder}/{txt_file[:-4]}.mp3 --write-subtitles {output_folder}/{txt_file[:-4]}.vtt --voice zh-CN-XiaoxiaoNeural --rate=+10%"
        # 执行终端命令,输出执行状态
        print('file %s is being processed...' % txt_file)
        subprocess.run(cmd, shell=True)
        print('file %s completed.' % txt_file)

等待完成

用到的是在线文字生成语音技术,等待任务完成即可收获mp3文件。

可用语言列表

这里仅摘录中英文,完整列表可参阅这里:

$ edge-tts --list-voices

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Name: zh-CN-XiaoxiaoNeural (通用性强)
Gender: Female

Name: zh-CN-XiaoyiNeural
Gender: Female

Name: zh-CN-YunjianNeural
Gender: Male

Name: zh-CN-YunxiNeural
Gender: Male

Name: zh-CN-YunxiaNeural
Gender: Male

Name: zh-CN-YunyangNeural (沉稳)
Gender: Male

Name: zh-CN-liaoning-XiaobeiNeural
Gender: Female

Name: zh-CN-shaanxi-XiaoniNeural
Gender: Female

Name: zh-HK-HiuGaaiNeural
Gender: Female

Name: zh-HK-HiuMaanNeural
Gender: Female

Name: zh-HK-WanLungNeural
Gender: Male

Name: zh-TW-HsiaoChenNeural
Gender: Female

Name: zh-TW-HsiaoYuNeural
Gender: Female

Name: zh-TW-YunJheNeural
Gender: Male

Name: en-US-AnaNeural (童声)
Gender: Female

Name: en-US-AriaNeural
Gender: Female

Name: en-US-ChristopherNeural
Gender: Male

Name: en-US-EricNeural
Gender: Male

Name: en-US-GuyNeural
Gender: Male

Name: en-US-JennyNeural
Gender: Female

Name: en-US-MichelleNeural
Gender: Female

Name: en-US-RogerNeural
Gender: Male

Name: en-US-SteffanNeural
Gender: Male