Ai
1 Star 0 Fork 7

LZY/Sound_File_Processing

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
short_file_plicing.py 2.22 KB
一键复制 编辑 原始数据 按行查看 历史
李子 提交于 2021-12-10 00:56 +08:00 . 自动新建文件夹
import os
from pydub import AudioSegment
from utils.file_io import file_r, file_w
transcript_short_path = "transcript/transcript_short.txt"
sound_root_path = "wav"
transcript_root_path = "transcript"
min_text = 5 # 当拼接出的文本长度长于此值时才会输出一段
min_second = 0.1 # 当拼接出的音频长度长于此值时才会输出一段
transcript_short_text = file_r("transcript/transcript_short.txt")
file_name = ""
file_name_begin = ""
file_list = []
file_text = ""
def get_sound_time(file_list):
sound_all_this = AudioSegment.silent(1)
for wav_file_this in file_list:
sound_all_this = sound_all_this + AudioSegment.from_wav(
sound_root_path + "/output_short/" + file_name_begin + "_" + wav_file_this + ".wav")
sound_time = sound_all_this.duration_seconds
return sound_time
def main():
"""
合并过短的音频
"""
global file_name_begin
if not os.path.exists(sound_root_path+"/output/"):
os.mkdir(sound_root_path+"/output/")
for line in transcript_short_text.split("\n"):
if len(line) == 0:
continue
line_file_name = line.split(" ")[0]
file_name_list = line_file_name.split("_")
file_name_list.remove(file_name_list[-1])
if file_name_begin != "_".join(file_name_list):
file_text = ""
file_list = []
file_name_begin = "_".join(file_name_list) # list.remove函数返回值为none
file_text = file_text + line.split(" ")[1]
file_list.append(line_file_name.split("_")[-1])
print(get_sound_time(file_list))
if len(file_text) > min_text and get_sound_time(file_list) > min_second:
file_name = file_name_begin + "_" + "_".join(file_list)
sound_all = AudioSegment.silent(1)
for wav_file in file_list:
sound_all = sound_all + AudioSegment.from_wav(sound_root_path + "/output_short/" + file_name_begin + "_" + wav_file + ".wav" )
sound_all.export(sound_root_path + "/output/" + file_name + ".wav", format="wav")
file_w(transcript_root_path + "/transcript.txt",file_name + " " + file_text + "\n", "a")
file_text = ""
file_list = []
if __name__ == "__main__":
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LZY2006/Sound_File_Processing.git
git@gitee.com:LZY2006/Sound_File_Processing.git
LZY2006
Sound_File_Processing
Sound_File_Processing
master

搜索帮助