Maximofn commited on
Commit
9ecf696
1 Parent(s): 7bfb36e

Script for concatenate chunck transcriptions into one file

Browse files
Files changed (1) hide show
  1. concat_transcriptions.py +77 -0
concat_transcriptions.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import re
3
+
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"
29
+
30
+ # Read chunk files
31
+ with open(chunk_files, "r") as f:
32
+ files = f.read().splitlines()
33
+
34
+ # Concatenate transcriptions
35
+ transcription = ""
36
+ num_transcriptions = 1
37
+ for i, file in enumerate(files):
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:
45
+
46
+ # if line is dd:dd:dd,ddd --> dd:dd:dd,ddd
47
+ if re.match(r"\d\d:\d\d:\d\d,\d\d\d --> \d\d:\d\d:\d\d,\d\d\d", line):
48
+ # Get start time (dd:dd:dd,ddd) and end time (dd:dd:dd,ddd)
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
+
56
+ # if line is a number and carriage return --> number
57
+ elif re.match(r"\d+$", line):
58
+ transcription += f"{num_transcriptions}\n"
59
+ num_transcriptions += 1
60
+
61
+ else:
62
+ transcription += f"{line}\n"
63
+
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
+
71
+ 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)