svjack commited on
Commit
e6a0ac7
·
verified ·
1 Parent(s): 9ab2b41

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +1 -117
README.md CHANGED
@@ -6,120 +6,4 @@ configs:
6
  path:
7
  - "*.wav"
8
  - "metadata.csv"
9
- ---
10
-
11
-
12
- <!--
13
-
14
-
15
- from pydub import AudioSegment
16
- import os
17
- import re
18
- from datetime import timedelta
19
-
20
- def process_audio_with_srt(wav_path, srt_path, output_folder, max_duration_sec=10):
21
- """
22
- 根据SRT字幕分割WAV音频文件,并合并相邻片段
23
- 参数:
24
- wav_path: 输入WAV文件路径
25
- srt_path: 输入SRT字幕文件路径
26
- output_folder: 输出文件夹路径
27
- max_duration_sec: 最大合并时长(秒)
28
- 返回:
29
- 无,直接生成分割后的WAV和TXT文件
30
- """
31
- # 创建输出文件夹
32
- os.makedirs(output_folder, exist_ok=True)
33
-
34
- # 解析SRT文件
35
- def parse_srt(srt_content):
36
- pattern = r'(\d+)\n(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})\n([\s\S]*?)\n\n'
37
- matches = re.findall(pattern, srt_content + '\n\n')
38
- segments = []
39
- for match in matches:
40
- idx, start, end, text = match
41
- # 清理文本中的多余空格和换行
42
- text = ' '.join(text.strip().splitlines())
43
- segments.append({
44
- 'index': int(idx),
45
- 'start': start.replace(',', '.'),
46
- 'end': end.replace(',', '.'),
47
- 'text': text
48
- })
49
- return segments
50
-
51
- # 时间字符串转毫秒
52
- def time_str_to_ms(time_str):
53
- h, m, s = time_str.split(':')
54
- s, ms = s.split('.')
55
- return int(timedelta(
56
- hours=int(h),
57
- minutes=int(m),
58
- seconds=int(s),
59
- milliseconds=int(ms)
60
- ).total_seconds() * 1000)
61
-
62
- # 读取并解析SRT文件
63
- with open(srt_path, 'r', encoding='utf-8') as f:
64
- srt_content = f.read()
65
- segments = parse_srt(srt_content)
66
-
67
- # 加载音频文件
68
- audio = AudioSegment.from_wav(wav_path)
69
-
70
- # 分割并合并相邻片段
71
- merged_segments = []
72
- current_group = []
73
- current_duration = 0
74
-
75
- for seg in segments:
76
- start_ms = time_str_to_ms(seg['start'])
77
- end_ms = time_str_to_ms(seg['end'])
78
- duration_ms = end_ms - start_ms
79
-
80
- # 检查是否需要新建合并组
81
- if current_duration + duration_ms > max_duration_sec * 1000 and current_group:
82
- merged_segments.append(current_group)
83
- current_group = [seg]
84
- current_duration = duration_ms
85
- else:
86
- current_group.append(seg)
87
- current_duration += duration_ms
88
-
89
- if current_group:
90
- merged_segments.append(current_group)
91
-
92
- # 生成输出文件
93
- for i, group in enumerate(merged_segments):
94
- # 计算合并片段的起止时间
95
- start_ms = time_str_to_ms(group[0]['start'])
96
- end_ms = time_str_to_ms(group[-1]['end'])
97
-
98
- # 提取音频片段
99
- segment_audio = audio[start_ms:end_ms]
100
-
101
- # 合并文本
102
- merged_text = '\n'.join([seg['text'] for seg in group])
103
-
104
- # 生成文件名(按字典序)
105
- filename = f"segment_{i:04d}"
106
- wav_output = os.path.join(output_folder, f"{filename}.wav")
107
- txt_output = os.path.join(output_folder, f"{filename}.txt")
108
-
109
- # 保存文件
110
- segment_audio.export(wav_output, format="wav")
111
- with open(txt_output, 'w', encoding='utf-8') as f:
112
- f.write(merged_text)
113
-
114
- print(f"处理完成! 共生成 {len(merged_segments)} 个合并片段")
115
-
116
- # 使用示例
117
- process_audio_with_srt(
118
- wav_path="Wang_Leehom_Music_Class_Wav/[P01]1-1呼吸与声音的健康.wav",
119
- srt_path="Wang_Leehom_Music_Class_Wav/[P01]1-1呼吸与声音的健康.srt",
120
- output_folder="output_segments",
121
- max_duration_sec=15 # 合并最长15秒的相邻片段
122
- )
123
-
124
-
125
- -->
 
6
  path:
7
  - "*.wav"
8
  - "metadata.csv"
9
+ ---