html_and_vue/创建wav.py

70 lines
2.4 KiB
Python
Raw Normal View History

2023-11-06 23:36:05 +00:00
import re
2023-10-30 09:41:20 +00:00
import pandas as pd
2023-11-06 23:36:05 +00:00
import wave
import struct
2023-10-30 09:41:20 +00:00
# 打开Excel文件
2023-11-06 23:36:05 +00:00
def add_zero_to_title(title):
while len(title) < 3:
title = "0" + title
return title
2023-10-30 09:41:20 +00:00
def get_data(file_path, column_list, cv_name):
excel_data = pd.read_excel(file_path)
2023-11-06 23:36:05 +00:00
column_names = excel_data.columns.tolist()
title = column_names[0]
excel_data[title] = excel_data[title].ffill()
column_cv = column_names[2]
print(column_cv)
filter_value = cv_name
filtered_data = excel_data[excel_data[column_cv].astype(str).str.contains(filter_value, na=False)]
print(filtered_data)
2023-10-30 09:41:20 +00:00
wav_title_list = []
# 逐行读取指定列的数据
2023-11-06 23:36:05 +00:00
for index, row in filtered_data.iterrows():
2023-10-30 09:41:20 +00:00
title = row[column_list[0]]
2023-11-06 23:36:05 +00:00
title = re.findall(r"\d+\.?\d*", title)
title = add_zero_to_title(title[0])
2023-10-30 09:41:20 +00:00
role = row[column_list[1]]
num = row[column_list[2]]
2023-11-06 23:36:05 +00:00
print(num)
# wav_title = title[0] + '-' + role + '-' + cv_name + '-' + str(num)[:-2]
wav_title = title + '-' + role + '-' + cv_name + '-' + str(num)
2023-10-30 09:41:20 +00:00
wav_title_list.append(wav_title)
2023-11-06 23:36:05 +00:00
print(wav_title_list)
2023-10-30 09:41:20 +00:00
return wav_title_list
2023-11-06 23:36:05 +00:00
def creat_wav_file(wav_title_list):
2023-10-30 09:41:20 +00:00
2023-11-06 23:36:05 +00:00
# 设置音频参数
sample_width = 3 # 24位深度为3个字节
sample_rate = 44100 # 采样率为44100Hz
num_channels = 1 # 单声道
2023-10-30 09:41:20 +00:00
2023-11-06 23:36:05 +00:00
# 创建WAV文件
for file_name in wav_title_list:
with wave.open(r"G:\ready\绝天神印\{}.wav".format(file_name), 'w') as wav_file:
# 设置音频参数
wav_file.setsampwidth(sample_width)
wav_file.setframerate(sample_rate)
wav_file.setnchannels(num_channels)
2023-10-30 09:41:20 +00:00
2023-11-06 23:36:05 +00:00
# 生成音频数据
duration = 1 # 音频时长为10秒
num_samples = int(sample_rate * duration)
for i in range(num_samples):
# 生成一个24位整数的随机样本
sample = struct.pack('i', 0) # 这里设置为0你可以根据需要生成不同的音频样本
wav_file.writeframes(sample)
print('已成功创建{}文件'.format(file_name))
2023-10-30 09:41:20 +00:00
if __name__ == '__main__':
2023-11-06 23:36:05 +00:00
file_path = r"C:\Users\Administrator\Downloads\aadd.xlsx"
column_list = ['章节名称', '角色名', '台词量']
cv_name = '请叫我张钧'
2023-10-30 09:41:20 +00:00
wav_title_list = get_data(file_path, column_list, cv_name)
2023-11-06 23:36:05 +00:00
print(wav_title_list)
creat_wav_file(wav_title_list)
2023-10-30 09:41:20 +00:00