93 lines
3.5 KiB
Python
93 lines
3.5 KiB
Python
import re
|
||
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 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_role_data.iterrows():
|
||
title = row[selectedColumnList[0]]
|
||
title = re.findall(r"\d+\.?\d*", title)
|
||
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)
|
||
|
||
return wav_title_list
|
||
|
||
|
||
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:
|
||
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)
|
||
wav_file.setnchannels(num_channels)
|
||
|
||
# 生成音频数据
|
||
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))
|
||
|
||
|
||
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)
|