好友
阅读权限20
听众
最后登录1970-1-1
|
死月
发表于 2024-1-6 16:18
本帖最后由 死月 于 2024-1-6 16:27 编辑
合并的代码
import os
import sys
def merge_srt_files(srt_file_path):
# 提取文件名和目录
directory = os.path.dirname(srt_file_path)
filename = os.path.splitext(os.path.basename(srt_file_path))[0]
# 构建对应的txt文件路径
time_file_path = os.path.join(directory, f"shijian{filename}.txt")
text_file_path = os.path.join(directory, f"yiwen{filename}.txt")
# 检查txt文件是否存在
if not (os.path.isfile(time_file_path) and os.path.isfile(text_file_path)):
print(f"找不到对应的txt文件:{time_file_path} 或 {text_file_path}")
return
# 构建合并后的srt文件路径
merged_srt_file_path = os.path.join(directory, f"合并{filename}.srt")
with open(time_file_path, 'r', encoding='utf-8') as f1, \
open(text_file_path, 'r', encoding='utf-8') as f2, \
open(merged_srt_file_path, 'w', encoding='utf-8') as f3:
# 读取两个文件的内容
time_lines = f1.readlines()
text_lines = f2.readlines()
# 合并两个文件的内容
for i in range(len(time_lines)):
if i % 2 == 0: # 处理时间戳行
f3.write(str((i+2)//2) + '\n')
else: # 处理文本行
f3.write(time_lines.strip() + '\n')
f3.write(text_lines[(i-1)//2].strip() + '\n')
if i != len(time_lines) - 1 and (i+1) % 2 == 0: # 最后一行不需要加上空行
f3.write('\n') # 每个文本行之后加上一个空行,用于分隔时间戳行和文本行
print(f"合并完成,结果保存在:{merged_srt_file_path}")
# 获取拖拽的文件路径
dragged_files = sys.argv[1:]
# 批量处理拖拽的文件
for file_path in dragged_files:
if file_path.lower().endswith('.srt'):
merge_srt_files(file_path)
和字幕文件放在相同目录 不管是拆分还是合并 都是拖拽最原始是SRT到 .PY上会自动遍历目录下的文件 并合并
拆分会分成3个文件
幸乃小姐.srt
shijian幸乃小姐.txt
yuanwen幸乃小姐.txt
合并的时候 需要另外一个文件
因为我是为了翻译才拆分的
所以合并 的话要
yiwen幸乃小姐.txt
改名成这样才能合并
看明白之后 你甚至可以无中生 把不相关的普通文本和毫无相关的时间轴合并到一起 生成一个 伪字幕
在合并的基础上还可以
进行双语字幕的合并
import sys
import os
def read_srt(filename):
with open(filename, 'r', encoding='utf-8') as file:
lines = file.readlines()
subtitles = []
current_sub = []
for line in lines:
if line.strip() == '':
if current_sub:
subtitles.append(current_sub)
current_sub = []
else:
current_sub.append(line.strip())
if current_sub:
subtitles.append(current_sub)
return subtitles
def merge_subtitles(files):
subtitles_list = [read_srt(file) for file in files]
merged_subs = []
for sub_parts in zip(*subtitles_list):
merged_sub = [sub_parts[0][0], sub_parts[0][1]] # 序号和时间码
for sub in sub_parts:
merged_sub.append(sub[2]) # 字幕文本
merged_subs.append(merged_sub)
return merged_subs
def write_merged_srt(merged_subs, output_file):
with open(output_file, 'w', encoding='utf-8') as file:
for i, sub in enumerate(merged_subs, 1):
file.write(f'{i}\n')
file.write(f'{sub[1]}\n')
for text in sub[2:]:
file.write(f'{text}\n')
file.write('\n')
def find_matching_file(dragged_file):
base_name = os.path.basename(dragged_file)
directory = os.path.dirname(dragged_file)
for file in os.listdir(directory):
if file.startswith('合并') and file.endswith(base_name):
return os.path.join(directory, file)
return None
def main():
if len(sys.argv) != 2:
print("请拖拽一个 SRT 文件到该脚本上。")
return
dragged_file = sys.argv[1]
matching_file = find_matching_file(dragged_file)
if not matching_file:
print("没有找到匹配的文件来合并。")
return
output_file = f'双语 {os.path.basename(dragged_file)}'
merged_subs = merge_subtitles([dragged_file, matching_file])
write_merged_srt(merged_subs, output_file)
print(f"合并字幕文件已生成:{output_file}")
if __name__ == "__main__":
main()
也是拖拽原始 SRT到.PY上 会遍历刚才合并完的 翻译后的文件 跟 拖拽的文件合并一个新的双语字幕文件出来
|
|