html_and_vue/创建wav.py

93 lines
3.5 KiB
Python
Raw Permalink 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
2023-11-06 23:36:05 +00:00
2023-11-07 09:28:34 +00:00
# 补全章节位数
def add_zero_to_title(chapterName, places):
"""
:param chapterName: 章节名称
:param places: 章节编号规定的位数, 如003, 即命名时需要的位数为3位
:return: 补全位数的章节编号
"""
while len(chapterName) < places:
chapterName = "0" + chapterName
return chapterName
2023-11-06 23:36:05 +00:00
2023-11-07 09:28:34 +00:00
# 打开Excel文件
def get_wav_title(filePath, selectedColumnList, sheetName, cvName, places):
"""
:param filePath: 剧组文件的路径
:param selectedColumnList: 选择要保留的数据列
:param sheetName: 要读取的sheet名称
:param cvName: cv名称
:return:
"""
role_data = pd.read_excel(filePath, sheet_name=sheetName)
role_data[selectedColumnList[0]] = role_data[selectedColumnList[0]].ffill()
filtered_role_data = role_data[role_data[selectedColumnList[1]].astype(str).str.contains(cvName, na=False)]
# filtered_role_data = role_data[role_data[selectedColumnList[1]].str.contains(cvName, na=False)]
print(filtered_role_data)
2023-10-30 09:41:20 +00:00
wav_title_list = []
2023-11-07 09:28:34 +00:00
2023-10-30 09:41:20 +00:00
# 逐行读取指定列的数据
2023-11-07 09:28:34 +00:00
for index, row in filtered_role_data.iterrows():
title = row[selectedColumnList[0]]
2023-11-06 23:36:05 +00:00
title = re.findall(r"\d+\.?\d*", title)
2023-11-07 09:28:34 +00:00
title = add_zero_to_title(title[0], places)
roleName = row[selectedColumnList[2]]
linesNum = str(row[selectedColumnList[3]])
if '.' in linesNum:
wav_title = title + '-' + roleName + '-' + cvName + '-' + linesNum[:-2]
elif linesNum == 'nan':
wav_title = title + '-' + roleName + '-' + cvName + '-' + '0'
else:
wav_title = title + '-' + roleName + '-' + cvName + '-' + linesNum
2023-10-30 09:41:20 +00:00
wav_title_list.append(wav_title)
2023-11-07 09:28:34 +00:00
2023-10-30 09:41:20 +00:00
return wav_title_list
2023-11-07 09:28:34 +00:00
def creat_wav_file(wav_title_list, sample_width, sample_rate, num_channels, save_path):
"""
:param wav_title_list: 波形文件名列表
:param sample_width: 位深度24位深度为3个字节
:param sample_rate: 采样率
:param num_channels: 声道1为单声道2为立体声
:return:
"""
2023-10-30 09:41:20 +00:00
2023-11-06 23:36:05 +00:00
# 创建WAV文件
for file_name in wav_title_list:
2023-11-07 09:28:34 +00:00
wav_name = save_path + '/' + file_name + '.wav'
with wave.open(wav_name, 'w') as wav_file:
2023-11-06 23:36:05 +00:00
# 设置音频参数
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
2023-11-07 09:28:34 +00:00
2023-10-30 09:41:20 +00:00
if __name__ == '__main__':
2023-11-07 09:28:34 +00:00
file_path = r"C:\Users\Cori\Downloads\逍遥小医仙剧组 (1).xlsx"
# column_list = ['章节名称', 'CV名称', '角色名', '台词量']
column_list = ['章节名', 'CV', '角色名', '台词数']
2023-11-06 23:36:05 +00:00
cv_name = '请叫我张钧'
2023-11-07 09:28:34 +00:00
sheetName = '角色音进度表201-487'
sample_width = 3 # 24位深度为3个字节
sample_rate = 44100 # 采样率为44100Hz
num_channels = 1 # 单声道
wav_title_list = get_wav_title(file_path, column_list, sheetName, cv_name, 4)
print(wav_title_list, sample_width, sample_rate, num_channels)
# creat_wav_file(wav_title_list)