new
parent
94f1683989
commit
bd8f94353a
99
创建wav.py
99
创建wav.py
|
@ -3,48 +3,66 @@ import pandas as pd
|
|||
import wave
|
||||
import struct
|
||||
|
||||
|
||||
# 补全章节位数
|
||||
def add_zero_to_title(chapterName, places):
|
||||
"""
|
||||
:param chapterName: 章节名称
|
||||
:param places: 章节编号规定的位数, 如003, 即命名时需要的位数为3位
|
||||
:return: 补全位数的章节编号
|
||||
"""
|
||||
while len(chapterName) < places:
|
||||
chapterName = "0" + chapterName
|
||||
return chapterName
|
||||
|
||||
|
||||
# 打开Excel文件
|
||||
def add_zero_to_title(title):
|
||||
while len(title) < 3:
|
||||
title = "0" + title
|
||||
return title
|
||||
|
||||
def get_data(file_path, column_list, cv_name):
|
||||
excel_data = pd.read_excel(file_path)
|
||||
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)
|
||||
|
||||
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)
|
||||
wav_title_list = []
|
||||
|
||||
# 逐行读取指定列的数据
|
||||
for index, row in filtered_data.iterrows():
|
||||
title = row[column_list[0]]
|
||||
for index, row in filtered_role_data.iterrows():
|
||||
title = row[selectedColumnList[0]]
|
||||
title = re.findall(r"\d+\.?\d*", title)
|
||||
title = add_zero_to_title(title[0])
|
||||
role = row[column_list[1]]
|
||||
num = row[column_list[2]]
|
||||
print(num)
|
||||
# wav_title = title[0] + '-' + role + '-' + cv_name + '-' + str(num)[:-2]
|
||||
wav_title = title + '-' + role + '-' + cv_name + '-' + str(num)
|
||||
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
|
||||
wav_title_list.append(wav_title)
|
||||
print(wav_title_list)
|
||||
|
||||
return wav_title_list
|
||||
|
||||
def creat_wav_file(wav_title_list):
|
||||
|
||||
# 设置音频参数
|
||||
sample_width = 3 # 24位深度为3个字节
|
||||
sample_rate = 44100 # 采样率为44100Hz
|
||||
num_channels = 1 # 单声道
|
||||
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:
|
||||
"""
|
||||
|
||||
# 创建WAV文件
|
||||
for file_name in wav_title_list:
|
||||
with wave.open(r"G:\ready\绝天神印\{}.wav".format(file_name), 'w') as wav_file:
|
||||
wav_name = save_path + '/' + file_name + '.wav'
|
||||
with wave.open(wav_name, 'w') as wav_file:
|
||||
# 设置音频参数
|
||||
wav_file.setsampwidth(sample_width)
|
||||
wav_file.setframerate(sample_rate)
|
||||
|
@ -59,11 +77,16 @@ def creat_wav_file(wav_title_list):
|
|||
wav_file.writeframes(sample)
|
||||
print('已成功创建{}文件'.format(file_name))
|
||||
|
||||
if __name__ == '__main__':
|
||||
file_path = r"C:\Users\Administrator\Downloads\aadd.xlsx"
|
||||
column_list = ['章节名称', '角色名', '台词量']
|
||||
cv_name = '请叫我张钧'
|
||||
wav_title_list = get_data(file_path, column_list, cv_name)
|
||||
print(wav_title_list)
|
||||
creat_wav_file(wav_title_list)
|
||||
|
||||
if __name__ == '__main__':
|
||||
file_path = r"C:\Users\Cori\Downloads\逍遥小医仙剧组 (1).xlsx"
|
||||
# column_list = ['章节名称', 'CV名称', '角色名', '台词量']
|
||||
column_list = ['章节名', 'CV', '角色名', '台词数']
|
||||
cv_name = '请叫我张钧'
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue