new file
parent
a26f3fc789
commit
94f1683989
73
创建wav.py
73
创建wav.py
|
@ -1,44 +1,69 @@
|
|||
import numpy as np
|
||||
from scipy.io import wavfile
|
||||
import re
|
||||
import pandas as pd
|
||||
import wave
|
||||
import struct
|
||||
|
||||
# 打开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)
|
||||
|
||||
wav_title_list = []
|
||||
# 逐行读取指定列的数据
|
||||
for index, row in excel_data.iterrows():
|
||||
for index, row in filtered_data.iterrows():
|
||||
title = row[column_list[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]]
|
||||
wav_title = title + '-' + role + '-' + cv_name + '-' + num
|
||||
print(num)
|
||||
# wav_title = title[0] + '-' + role + '-' + cv_name + '-' + str(num)[:-2]
|
||||
wav_title = title + '-' + role + '-' + cv_name + '-' + str(num)
|
||||
wav_title_list.append(wav_title)
|
||||
print(wav_title_list)
|
||||
return wav_title_list
|
||||
|
||||
def creat_wav_file(wav_title_list):
|
||||
|
||||
def creat_wav_file(wav_title_list, sample_rate, duration):
|
||||
# 生成一个时间数组,用于表示音频信号的时间
|
||||
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
|
||||
# 设置音频参数
|
||||
sample_width = 3 # 24位深度为3个字节
|
||||
sample_rate = 44100 # 采样率为44100Hz
|
||||
num_channels = 1 # 单声道
|
||||
|
||||
# 生成一个440 Hz的正弦波
|
||||
frequency = 440 # 440 Hz
|
||||
audio_signal = np.sin(2 * np.pi * frequency * t)
|
||||
# 创建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)
|
||||
|
||||
# 将音频信号缩放到24位的范围[-2^23, 2^23-1]
|
||||
audio_signal = np.int32(audio_signal * 2**23)
|
||||
|
||||
for i in wav_title_list:
|
||||
wavfile.write(r"C:\Users\Cori\Downloads\test\{}.wav".format(i), sample_rate, audio_signal)
|
||||
print('已成功创建{}文件'.format(i))
|
||||
# 生成音频数据
|
||||
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\逍遥小医仙剧组.xlsx"
|
||||
column_list = ['章节名','角色名','台词数']
|
||||
cv_name = input('请输入CV姓名')
|
||||
file_path = r"C:\Users\Administrator\Downloads\aadd.xlsx"
|
||||
column_list = ['章节名称', '角色名', '台词量']
|
||||
cv_name = '请叫我张钧'
|
||||
wav_title_list = get_data(file_path, column_list, cv_name)
|
||||
# 设置采样率为44100 Hz
|
||||
sample_rate = 44100
|
||||
# 设置音频信号的时长(秒)
|
||||
duration = 5.0
|
||||
creat_wav_file(wav_title_list, sample_rate, duration)
|
||||
print(wav_title_list)
|
||||
creat_wav_file(wav_title_list)
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
from pydub import AudioSegment
|
||||
import os
|
||||
|
||||
# 指定文件夹路径
|
||||
folder_path = r"C:\Users\Administrator\Downloads\test"
|
||||
|
||||
# 存储文件名的列表
|
||||
wav_files_list = []
|
||||
|
||||
# 遍历文件夹中的文件
|
||||
for filename in os.listdir(folder_path):
|
||||
# 获取文件的完整路径
|
||||
file_path = os.path.join(folder_path, filename)
|
||||
|
||||
# 判断是否为WAV文件
|
||||
if filename.lower().endswith('.wav') and os.path.isfile(file_path):
|
||||
# 将文件名添加到列表中
|
||||
wav_files_list.append(filename)
|
||||
|
||||
for file_name in wav_files_list:
|
||||
file_name = file_name.split(".")[0]
|
||||
wav_file = AudioSegment.from_wav(folder_path + '\\' + '{}.wav'.format(file_name))
|
||||
|
||||
# 设置导出参数
|
||||
bitrate = '192k' # 比特率为192kbps
|
||||
channels = 2 # 双声道
|
||||
|
||||
# 导出为MP3文件
|
||||
save_path = r"C:\Users\Administrator\Downloads\test2"
|
||||
mp3_file = save_path + '\\' '{}.mp3'.format(file_name)
|
||||
wav_file.export(mp3_file, format='mp3', bitrate=bitrate, parameters=['-ac', str(channels)])
|
|
@ -0,0 +1,31 @@
|
|||
import os
|
||||
import shutil
|
||||
from pypinyin import lazy_pinyin, Style
|
||||
|
||||
# 指定文件夹路径
|
||||
folder_path = r"G:\ready\绝天神印"
|
||||
|
||||
# 遍历文件夹中的文件
|
||||
for filename in os.listdir(folder_path):
|
||||
# 获取文件的完整路径
|
||||
file_path = os.path.join(folder_path, filename)
|
||||
|
||||
# 判断是否为文件
|
||||
if os.path.isfile(file_path):
|
||||
# 根据文件名进行拆分
|
||||
parts = filename.split('-')
|
||||
|
||||
# 获取拆分后列表中的第2个值
|
||||
if len(parts) >= 2:
|
||||
target_folder_name = parts[1].strip()
|
||||
target_folder_first_letter = ''.join(lazy_pinyin(target_folder_name[0], style=Style.FIRST_LETTER))
|
||||
target_folder = target_folder_first_letter.upper() + '-' + target_folder_name
|
||||
print(target_folder)
|
||||
|
||||
# 创建目标文件夹(如果不存在)
|
||||
save_path = folder_path + '/' + target_folder
|
||||
if not os.path.exists(save_path):
|
||||
os.makedirs(save_path)
|
||||
|
||||
# 移动文件到目标文件夹
|
||||
shutil.move(file_path, os.path.join(save_path))
|
Loading…
Reference in New Issue