Maximofn commited on
Commit
3dbe153
1 Parent(s): b004aea

Finish concatenate transcriptions script

Browse files
Files changed (1) hide show
  1. concat_transcriptions.py +45 -15
concat_transcriptions.py CHANGED
@@ -4,25 +4,44 @@ import re
4
  def sum_seconds(time, seconds):
5
  # Get time in seconds
6
  time = time.split(",")
7
- milisecons = time[1]
 
8
  time = time[0].split(":")
9
  time = int(time[0])*3600 + int(time[1])*60 + int(time[2])
10
 
 
 
 
 
 
11
  # Add seconds
12
- time += seconds
 
 
 
 
 
13
 
14
  # Get time in hh:mm:ss,mmm format
15
- hours = time // 3600
16
- minutes = (time % 3600) // 60
17
- seconds = (time % 3600) % 60
18
- time = f"{hours:02d}:{minutes:02d}:{seconds:02d},{milisecons}"
 
19
 
20
  return time
21
 
22
- def main(args):
23
- chunk_files = args.chunk_files
24
- seconds = int(args.seconds)
25
- speaker = int(args.speaker)
 
 
 
 
 
 
 
26
  chunk_folder = "transcriptions"
27
  output_folder = "concatenated_transcriptions"
28
  transcription_extension = "srt"
@@ -38,7 +57,7 @@ def main(args):
38
  chunk = file
39
  _, file = chunk.split("/")
40
  file, _ = file.split(".")
41
- transcription_chunk_file = f"{chunk_folder}/{file}_speaker{speaker:003d}.{transcription_extension}"
42
  with open(transcription_chunk_file, "r") as f:
43
  transcription_chunk = f.read().splitlines()
44
  for line in transcription_chunk:
@@ -49,7 +68,7 @@ def main(args):
49
  start, end = line.split(" --> ")
50
  # Add seconds to start and end time
51
  start = sum_seconds(start, i*seconds)
52
- end = sum_seconds(end, seconds)
53
  # Add to transcription
54
  transcription += f"{start} --> {end}\n"
55
 
@@ -64,7 +83,7 @@ def main(args):
64
  # Write transcription
65
  file_split = file.split("_")[:-1]
66
  file = "_".join(file_split)
67
- output_file = f"{output_folder}/{file}_speaker{speaker:003d}.{transcription_extension}"
68
  with open(output_file, "w") as f:
69
  f.write(transcription)
70
 
@@ -72,6 +91,17 @@ if __name__ == "__main__":
72
  parser = argparse.ArgumentParser()
73
  parser.add_argument("chunk_files", help="Path to the file containing the paths to the chunk files")
74
  parser.add_argument("seconds", help="Duration of each chunk in seconds")
75
- parser.add_argument("speaker", help="Speaker name")
76
  args = parser.parse_args()
77
- main(args)
 
 
 
 
 
 
 
 
 
 
 
 
4
  def sum_seconds(time, seconds):
5
  # Get time in seconds
6
  time = time.split(",")
7
+ time_milisecons = time[1]
8
+ time_milisecons = int(time_milisecons)/1000
9
  time = time[0].split(":")
10
  time = int(time[0])*3600 + int(time[1])*60 + int(time[2])
11
 
12
+ # Get integer and decimal part of seconds
13
+ seconds, seconds_miliseconds = divmod(seconds, 1)
14
+ seconds = int(seconds)
15
+ seconds_miliseconds = round(seconds_miliseconds, 3)
16
+
17
  # Add seconds
18
+ time = time + seconds
19
+ time_milisecons = time_milisecons + seconds_miliseconds
20
+ if time_milisecons >= 1:
21
+ time = time + 1
22
+ time_milisecons = time_milisecons - 1
23
+ time_milisecons = round(time_milisecons, 3)
24
 
25
  # Get time in hh:mm:ss,mmm format
26
+ hours = int(time) // 3600
27
+ minutes = (int(time) % 3600) // 60
28
+ seconds = (int(time) % 3600) % 60
29
+ time_milisecons = str(time_milisecons).split(".")[1]
30
+ time = f"{hours:02d}:{minutes:02d}:{seconds:02d},{time_milisecons}"
31
 
32
  return time
33
 
34
+ def hmsms_to_seconds(time):
35
+ # Get time in seconds
36
+ time = time.split(",")
37
+ milisecons = time[1]
38
+ time = time[0].split(":")
39
+ time = int(time[0])*3600 + int(time[1])*60 + int(time[2])
40
+ time = time + int(milisecons)/1000
41
+
42
+ return time
43
+
44
+ def concatenate_transcriptions(chunk_files, seconds):
45
  chunk_folder = "transcriptions"
46
  output_folder = "concatenated_transcriptions"
47
  transcription_extension = "srt"
 
57
  chunk = file
58
  _, file = chunk.split("/")
59
  file, _ = file.split(".")
60
+ transcription_chunk_file = f"{chunk_folder}/{file}.{transcription_extension}"
61
  with open(transcription_chunk_file, "r") as f:
62
  transcription_chunk = f.read().splitlines()
63
  for line in transcription_chunk:
 
68
  start, end = line.split(" --> ")
69
  # Add seconds to start and end time
70
  start = sum_seconds(start, i*seconds)
71
+ end = sum_seconds(end, i*seconds)
72
  # Add to transcription
73
  transcription += f"{start} --> {end}\n"
74
 
 
83
  # Write transcription
84
  file_split = file.split("_")[:-1]
85
  file = "_".join(file_split)
86
+ output_file = f"{output_folder}/{file}.{transcription_extension}"
87
  with open(output_file, "w") as f:
88
  f.write(transcription)
89
 
 
91
  parser = argparse.ArgumentParser()
92
  parser.add_argument("chunk_files", help="Path to the file containing the paths to the chunk files")
93
  parser.add_argument("seconds", help="Duration of each chunk in seconds")
94
+ parser.add_argument('speakers_file', help='File with the number of speakers')
95
  args = parser.parse_args()
96
+
97
+ chunk_files = args.chunk_files
98
+ seconds = int(args.seconds)
99
+ with open(args.speakers_file, 'r') as f:
100
+ speakers = f.read().splitlines()
101
+ speakers = int(speakers[0])
102
+
103
+ if speakers > 0:
104
+ for speaker in range(speakers):
105
+ pass
106
+ else:
107
+ concatenate_transcriptions(chunk_files, seconds)