【修行】2024最好的活法:拒絕內耗,擁抱松弛感!(微软XiaoXiao-Multilingua朗读)(附srt清理代码)...

以下python脚本将选择srt字幕,清理序号和时间,只留下内容:

import re
import tkinter as tk
from tkinter import filedialog


def clean_srt_content(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        srt_content = file.read()


    filtered_content = re.sub(r'\d+\n\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\n', '', srt_content)
    filtered_content = re.sub(r'\n{2,}', '\n', filtered_content).strip()


    return filtered_content


def save_to_txt(output_path, content):
    with open(output_path, 'w', encoding='utf-8') as file:
        file.write(content)


def select_srt_file_and_process():
    root = tk.Tk()
    root.withdraw()  # Hide the main window
    file_path = filedialog.askopenfilename(
        title="Select a SRT file",
        filetypes=(("SRT files", "*.srt"), ("All files", "*.*"))
    )
    if file_path:
        cleaned_content = clean_srt_content(file_path)
        output_file_path = file_path.rsplit('.', 1)[0] + '_cleaned.txt'  # Save as a new file with '_cleaned' suffix
        save_to_txt(output_file_path, cleaned_content)
        print(f'Cleaned subtitles have been saved to {output_file_path}')
    else:
        print("No file selected, operation cancelled.")


# Invoke the file selection
select_srt_file_and_process()

以下python脚本遍历当前目录下所有srt字幕,依次处理字幕文件为txt,移除序号和时间,只留下内容:

import re
import os




def clean_srt_content(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        srt_content = file.read()


    # Remove indexes and timestamps
    filtered_content = re.sub(
        r'\d+\n\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\n', '', srt_content)
    # Remove excessive empty lines
    filtered_content = re.sub(r'\n{2,}', '\n', filtered_content).strip()


    return filtered_content




def save_to_txt(output_path, content):
    with open(output_path, 'w', encoding='utf-8') as file:
        file.write(content)




def process_all_srt_files(directory):
    for filename in os.listdir(directory):
        if filename.endswith(".srt"):
            file_path = os.path.join(directory, filename)
            cleaned_content = clean_srt_content(file_path)
            output_file_path = os.path.splitext(file_path)[0] + '_cleaned.txt'
            save_to_txt(output_file_path, cleaned_content)
            print(f"Processed {filename}")




# Call the function with the current directory
process_all_srt_files(".")

SRT字幕如下:

2fa01055e6d7db4bb9d2efc6ed6ea9af.png

从youtube下载字幕后,处理结果:

3632160ff2d2850365cd0865189fbab4.png

193e228d4c1186609bfc0f82c02bc6fc.png

生成的字幕使用Azure Speech API文本转语音:(选择最新的XiaoXiao-Multilingual)

f1ef3e0f3b4b3f9bf343e931605ddadd.png

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值