1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python英语词汇读音_Python 将英语单词列表 转换为听写使用的MP3格式(每个单词朗读两遍)...

python英语词汇读音_Python 将英语单词列表 转换为听写使用的MP3格式(每个单词朗读两遍)...

时间:2020-08-18 19:24:13

相关推荐

python英语词汇读音_Python 将英语单词列表 转换为听写使用的MP3格式(每个单词朗读两遍)...

1. 首先要安装一个包 pydub,使用最流行的方式 pip 即可。

在此之后引用其中的 AudioSegment 模块,此外为了生成的文件名是当前的时间戳,因此导入了 datetime 包。

AudioSegment 模块封装了各种操作,可以较为便捷的操作MP3或者其他类型音频(pydub支持很多常见的音频格式)。

2. 其中一些基本操作如下:

读入MP3:mp3_1 = AudioSegment.from_mp3("C:\\my_mp3\\mp3_1.mp3") # 将括号内修改为你要读入的MP3文件路径

读入MP3:mp3_2 = AudioSegment.from_mp3("C:\\my_mp3\\mp3_2.mp3") # 将括号内修改为你要读入的MP3文件路径

拼接两段MP3:mp3 = mp3_1 + mp3_2 # 直接使用加号就可以拼接

输出为MP3格式:output_music.export("C:\\my_mp3\\output.mp3", format="mp3") # 将双引号内修改为要输出的文件路径

3. 我们把上述操作封装为一个拼接函数如下所示:

import os

from pydub import AudioSegment # 先导入这个模块

import datetime

def pinjie(mp3_path_list, out_div):

out_name = datetime.datetime.now().strftime('[%Y-%m-%d]%H-%M-%S.mp3')

output_music = None

first = True

for mp3_path in mp3_path_list:

if first:

first = False

input_music_1 = AudioSegment.from_mp3(mp3_path)

output_music = input_music_1

else:

input_music_1 = AudioSegment.from_mp3(mp3_path)

output_music += input_music_1 # 打开音频后,直接使用+运算符就可以将两段拼接在一起

output_music.export(out_div + "\\" + out_name, format="mp3") # 输出合并之后的音频

print(len(output_music), output_music.channels) # 打印一下合并后音频的信息

4. 紧接着需要创建一个有道词典类,专门下载有道词典的读音文件(MP3格式)

这个类太长了,我放在另外一篇博客中写吧,主要功能就是下载音频,分为下载美音和英音两种。

这个类是在另外一篇博客的基础上只做了微小的改动,点击此处链接到最初的博客,在此感谢这个博主。

5. 将 download 单独封装为一个包(文件)

from youdao import youdao as yd

trans = {"英音": 1, "美音": 0, "英美": 3}

def download(pronunciation, word_list):

sp = yd()

if trans[pronunciation] == 0 or trans[pronunciation] == 3: # 获取美音

print("获取列表单词的美音音频: ", word_list)

sp.setAccent(0)

for one_word in word_list:

sp.down(one_word)

if trans[pronunciation] == 1 or trans[pronunciation] == 3: # 获取英音

print("获取列表单词的英音音频: ", word_list)

sp.setAccent(1)

for one_word in word_list:

sp.down(one_word)

6. 找一些空白音频来作为时间间隔(这个会在我上传的代码中带有)

__5.mp3 为0.5秒的空白音频

_1.mp3 为1秒的空白音频

_2.mp3 为2秒的空白音频

_3.mp3 为3秒的空白音频

7. 准备工作到此结束,然后写下主要代码:

import datetime

from download import download as dl

import os

from pinjieMP3 import pinjie

from random import shuffle

f = open('C:/mine/my_word_list.txt') # 此处是单词列表,使用换行作为单词与单词之间的间隔

word_list = f.readlines()

word_list = [word.replace("\n", "") for word in word_list]

f.close()

dl("英音", word_list) # 在此处修改英音还是美音

type = 1 # 0为美音,1为英音 # 在此处也要修改英音还是美音

d_word_list = []

# 将单词列表随机打乱,注释掉这一行就是按照输入顺序进行生成的

shuffle(word_list)

d_word_list.append("_2")

d_word_list.append("_2")

for word in word_list:

d_word_list.append(word)

d_word_list.append("_2")

d_word_list.append(word)

d_word_list.append("_2")

d_word_list.append("_2")

mp3_file_path_list = []

dirRoot = os.path.dirname(os.path.abspath(__file__))

out_put_div = os.path.join(dirRoot, "output")

# 输出打乱后的列表

out_name = datetime.datetime.now().strftime('[%Y-%m-%d]%H-%M-%S.txt')

f = open(out_put_div + '\\' + out_name, "w")

f.writelines([word+"\n" for word in word_list])

f.close()

for word in d_word_list:

word = word.lower() # 小写

fileName = word + '.mp3'

if 0 == type:

dirSpeech = os.path.join(dirRoot, 'Speech_US') # 美音库

else:

dirSpeech = os.path.join(dirRoot, 'Speech_EN') # 英音库

filePath = os.path.join(dirSpeech, fileName)

# 判断是否存在这个MP3文件

if os.path.exists(filePath):

mp3_file_path_list.append(filePath)

else:

print("【ERROR】音频文件不存在造成错误: ", filePath)

pinjie(mp3_file_path_list, out_put_div)

print("----程序结束----")

原文链接:/weixin_40901068/article/details/107907610

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。