terry-li-hm commited on
Commit
128a0e2
·
1 Parent(s): 458cf48
Files changed (1) hide show
  1. sv.py +35 -5
sv.py CHANGED
@@ -241,11 +241,41 @@ def generate_diarization(audio_path):
241
  diarization_segments = []
242
  txt_file = "mtr_dn.txt"
243
  with open(txt_file, "w") as f:
 
 
 
 
244
  for turn, _, speaker in output.itertracks(yield_label=True):
245
- start_time = format_time(turn.start)
246
- end_time = format_time(turn.end)
247
- duration = format_time(turn.end - turn.start)
248
- line = f"{start_time} - {end_time} ({duration}): {speaker}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  f.write(line)
250
  print(line.strip())
251
  diarization_segments.append(
@@ -253,7 +283,7 @@ def generate_diarization(audio_path):
253
  parse_time(start_time),
254
  parse_time(end_time),
255
  parse_time(duration),
256
- speaker,
257
  )
258
  )
259
 
 
241
  diarization_segments = []
242
  txt_file = "mtr_dn.txt"
243
  with open(txt_file, "w") as f:
244
+ current_speaker = None
245
+ current_start = None
246
+ current_end = None
247
+
248
  for turn, _, speaker in output.itertracks(yield_label=True):
249
+ if speaker != current_speaker:
250
+ if current_speaker is not None:
251
+ start_time = format_time(current_start)
252
+ end_time = format_time(current_end)
253
+ duration = format_time(current_end - current_start)
254
+ line = (
255
+ f"{start_time} - {end_time} ({duration}): {current_speaker}\n"
256
+ )
257
+ f.write(line)
258
+ print(line.strip())
259
+ diarization_segments.append(
260
+ (
261
+ parse_time(start_time),
262
+ parse_time(end_time),
263
+ parse_time(duration),
264
+ current_speaker,
265
+ )
266
+ )
267
+ current_speaker = speaker
268
+ current_start = turn.start
269
+ current_end = turn.end
270
+ else:
271
+ current_end = turn.end
272
+
273
+ # Write the last segment
274
+ if current_speaker is not None:
275
+ start_time = format_time(current_start)
276
+ end_time = format_time(current_end)
277
+ duration = format_time(current_end - current_start)
278
+ line = f"{start_time} - {end_time} ({duration}): {current_speaker}\n"
279
  f.write(line)
280
  print(line.strip())
281
  diarization_segments.append(
 
283
  parse_time(start_time),
284
  parse_time(end_time),
285
  parse_time(duration),
286
+ current_speaker,
287
  )
288
  )
289