# -*- coding:utf-8 -*-
# python 3.10
import json,shutil,os
from pydub import AudioSegment#需要pip install pydub,并且安装ffpmeg,添加bin目录到环境变量的path里面
# 当前目录
curPath = os.getcwd()

# 定义要清空的文件夹路径
folder_path = curPath
 
# 删除文件夹下的所有文件
for file_name in os.listdir(folder_path):
    file_path = os.path.join(folder_path, file_name)
    print(file_path)
    if os.path.isdir(file_path):
        shutil.rmtree(file_path)
        os.mkdir(file_path)
        os.mkdir(file_path + '/' + 'sound')

# 文件夹下所有文件
def find_files(folder_path):
    file_list = []
    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)
        if os.path.isfile(file_path):
            file_list.append(file_path)
        elif os.path.isdir(file_path):
            file_list.extend(find_files(file_path))
    return file_list

# 获取mp3下所有文件路径
wenJianPath = 'D:/project/xianyudefanji/remotesound/mp3'
fileAlls = find_files(wenJianPath)
print(fileAlls)

# 读取DH_1脚本文件寻找effectUrl,去匹配文件,复制文件
DH_Path = 'D:/project/xianyudefanji/assets/Scripts/DH'
DH_MaxNum = 5
for i in range(DH_MaxNum):
    updated_lines = []
    with open(DH_Path + '/' + 'DH_' + str(i+1) + '.ts', 'r', encoding = 'utf-8') as file:
        lines = file.readlines()
        for line in lines:
            # 对每一行进行操作
            # print(line)
            # 查找语音字符串
            aa = 'DH/' + 'DH_' + str(i+1) + '/sound/'
            index = line.find(aa)
            if index != -1:
                _index = line.find(',')
                findstr = line[index:_index-1]
                findstr_arr = findstr.split('/')
                print(findstr.split('/'))
                # 查找对应语音的路径存不存在
                isFind = False
                _wenjian = ''
                for wenjian in fileAlls:
                    if findstr_arr[len(findstr_arr)-1] in wenjian:
                        shutil.copy2(wenjian, curPath + '/' + 'DH_' + str(i+1) + '/sound/')
                        isFind = True
                        _wenjian = wenjian
                        break
                if not isFind:
                    print(findstr,"未找到对应语音文件----------------")
                    updated_lines.append(line)
                else:
                    # 替换音频时长
                    bb = 'delayTime:'
                    bb_index = line.find(bb)
                    if bb_index != -1:
                        _bb_index = line.find(',',bb_index)
                        bb_findstr = line[bb_index:_bb_index]
                        print(bb_findstr,"bb_findstr====")
                        audio = AudioSegment.from_file(_wenjian)
                        # 获取音频时长(毫秒)
                        duration_ms = len(audio)
                        # 转换为秒,保留两位小数
                        duration_sec = round(duration_ms / 1000,2)
                        print(f"音频时长:{duration_sec}秒")
                        updated_line = line.replace(bb_findstr, 'delayTime: '+ str(duration_sec))
                        updated_lines.append(updated_line)
            else:
                updated_lines.append(line)
    with open(DH_Path + '/' + 'DH_' + str(i+1) + '.ts', 'w', encoding = 'utf-8') as file:
        file.writelines(updated_lines)

# 读取其他脚本文件寻找effectUrl,去匹配文件,复制文件
genpath = 'D:/project/xianyudefanji/assets/Scripts/'
otherPath = [genpath + 'ZaoCanDian/ZaoCanDian.ts']
for i in range(len(otherPath)):
    otherPathupdated_lines = []
    with open(otherPath[i], 'r', encoding = 'utf-8') as file:
        lines = file.readlines()
        for line in lines:
            # 对每一行进行操作
            # print(line)
            # 查找语音字符串
            aa = 'DH/MainHall/sound/'
            index = line.find(aa)
            if index != -1:
                _index = line.find(',')
                findstr = line[index:_index-1]
                findstr_arr = findstr.split('/')
                print(findstr.split('/'))
                # 查找对应语音的路径存不存在
                isFind = False
                _wenjian = ''
                for wenjian in fileAlls:
                    if findstr_arr[len(findstr_arr)-1] in wenjian:
                        print(wenjian,curPath + '/' + 'MainHall/sound/')
                        shutil.copy2(wenjian, curPath + '/' + 'MainHall/sound/')
                        isFind = True
                        _wenjian = wenjian
                        break
                if not isFind:
                    print(findstr,"未找到对应语音文件----------------")
                    otherPathupdated_lines.append(line)
                else:
                    # 替换音频时长
                    bb = 'delayTime:'
                    bb_index = line.find(bb)
                    if bb_index != -1:
                        _bb_index = line.find(',',bb_index)
                        bb_findstr = line[bb_index:_bb_index]
                        print(bb_findstr,"bb_findstr====")
                        audio = AudioSegment.from_file(_wenjian)
                        # 获取音频时长(毫秒)
                        duration_ms = len(audio)
                        # 转换为秒,保留两位小数
                        duration_sec = round(duration_ms / 1000,2)
                        print(f"音频时长:{duration_sec}秒")
                        otherupdated_line = line.replace(bb_findstr, 'delayTime: '+ str(duration_sec))
                        otherPathupdated_lines.append(otherupdated_line)
            else:
                otherPathupdated_lines.append(line)
    with open(otherPath[i], 'w', encoding = 'utf-8') as file:
        file.writelines(otherPathupdated_lines)