-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtranscribe.ts
250 lines (214 loc) · 9.39 KB
/
transcribe.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { generateTranscriptAudio } from "./audioGenerator.ts";
import concatenateAudioFiles from "./concat.ts";
import getAudioDuration from "./audioduration.ts";
import { generateCleanSrt } from "./cleanSrt.ts";
const transcribeAudio = async (audios: any) => {
console.log("🎯 Starting transcribeAudio function");
console.log("📦 Received audios:", audios);
const retryDelays = [1000, 2000, 3000];
let retryCount = 0;
while (retryCount < retryDelays.length) {
try {
console.log(
`🔄 Attempt ${retryCount + 1} of ${retryDelays.length}`
);
console.log("🌐 Making request to transcription service...");
const response = await fetch("http://127.0.0.1:5005/transcribe", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ audios }),
});
if (!response.ok) {
console.error(
"❌ Response not OK:",
response.status,
response.statusText
);
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log("✅ Response received successfully");
const data = await response.json();
console.log("📄 Received data:", JSON.stringify(data, null, 2));
if (!Array.isArray(data)) {
console.error("❌ Data is not an array:", data);
throw new Error(
"Expected array response from transcription service"
);
}
console.log("🔄 Processing transcription data...");
return data.map(([transcription, audioPath]: any) => {
console.log(`📝 Processing transcription for: ${audioPath}`);
console.log("🔍 Transcription data:", transcription);
if (!transcription || !transcription.segments) {
console.error(
"❌ Invalid transcription format:",
transcription
);
throw new Error(
`Invalid transcription format for ${audioPath}`
);
}
const processedTranscription = {
...transcription,
segments: transcription.segments.map((segment: any) => {
console.log("➡️ Processing segment:", segment);
return {
...segment,
words:
segment.words ||
segment.text
.split(" ")
.map((word: any, index: any) => {
const wordTiming = {
text: word,
start:
segment.start +
index *
((segment.end -
segment.start) /
segment.text.split(" ")
.length),
end:
segment.start +
(index + 1) *
((segment.end -
segment.start) /
segment.text.split(" ")
.length),
};
console.log(
"📊 Generated word timing:",
wordTiming
);
return wordTiming;
}),
};
}),
};
console.log("✅ Successfully processed transcription");
return [processedTranscription, audioPath];
});
} catch (error) {
console.error(`❌ Error in attempt ${retryCount + 1}:`, error);
console.error("🔍 Error details:", {
name: (error as Error).name,
message: (error as Error).message,
stack: (error as Error).stack,
});
if (retryCount < retryDelays.length - 1) {
const delay = retryDelays[retryCount];
console.log(`⏳ Retrying in ${delay / 1000} second(s)...`);
await new Promise((resolve) => setTimeout(resolve, delay));
} else {
console.error("❌ All retry attempts failed");
throw error;
}
retryCount++;
}
}
};
function srtTimeToSeconds(srtTime: string) {
const [hours, minutes, secondsAndMillis] = srtTime.split(":");
const [seconds, milliseconds] = secondsAndMillis.split(",");
return (
Number(hours) * 3600 +
Number(minutes) * 60 +
Number(seconds) +
Number(milliseconds) / 1000
);
}
function secondsToSrtTime(seconds: number) {
const pad = (num: number, size: number) => String(num).padStart(size, "0");
const hrs = Math.floor(seconds / 3600);
const mins = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
const millis = Math.round((seconds % 1) * 1000);
return `${pad(hrs, 2)}:${pad(mins, 2)}:${pad(secs, 2)},${pad(millis, 3)}`;
}
export default async function transcribe(
transcript: Transcript[],
secrets: VideoSecrets
) {
const { audios } = await generateTranscriptAudio(transcript, secrets);
let startingTime = 0;
console.log("🎬 Initial startingTime:", startingTime);
concatenateAudioFiles();
const transcriptionResults = await transcribeAudio(
audios.map((audio) => audio.audio)
);
if (!transcriptionResults) {
throw new Error("Failed to get transcription results");
}
const uncleanSrtContentArr = [];
for (let i = 0; i < transcriptionResults.length; i++) {
console.log(`\n🎯 Processing SRT #${i} ==================`);
const transcription = transcriptionResults[i][0];
const audio = audios[i];
console.log(`📝 Processing audio: ${audio.audio}`);
console.log(`⏰ Current startingTime: ${startingTime}`);
let srtIndex = 1;
let srtContent = "";
const words = transcription.segments.flatMap(
(segment: any) => segment.words
);
console.log(`🔤 Total words in segment: ${words.length}`);
for (let j = 0; j < words.length; j++) {
const word = words[j];
const nextWord = words[j + 1];
const wordStartTime = word.start + startingTime;
let wordEndTime;
if (nextWord) {
wordEndTime = nextWord.start + startingTime;
} else {
wordEndTime = word.end + startingTime;
}
const startTime = secondsToSrtTime(wordStartTime);
const endTime = secondsToSrtTime(wordEndTime);
console.log(`📊 Word "${word.text}":
- Raw start: ${word.start}
- Adjusted start: ${wordStartTime}
- Formatted start: ${startTime}
- End: ${endTime}`);
srtContent += `${srtIndex}\n${startTime} --> ${endTime}\n${word.text}\n\n`;
srtIndex++;
}
const lines = srtContent.split("\n");
console.log(`📑 Generated ${lines.length} lines for SRT #${i}`);
const incrementedSrtLines = lines.map((line) => {
const timeMatch = line.match(
/(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})/
);
if (timeMatch) {
const startTime = srtTimeToSeconds(timeMatch[1]);
const endTime = srtTimeToSeconds(timeMatch[2]);
return `${secondsToSrtTime(startTime)} --> ${secondsToSrtTime(
endTime
)}`;
}
return line;
});
const srtFileName = audio.audio
.replace("public/voice/", "")
.replace(".mp3", ".srt");
uncleanSrtContentArr.push({
content: incrementedSrtLines.join("\n"),
fileName: srtFileName,
});
console.log(`✅ Completed SRT #${i}`);
console.log(`📁 Saved to: ${srtFileName}`);
const currentDuration = transcription.segments.reduce(
(acc: number, segment: any) => {
return Math.max(acc, segment.end);
},
0
);
console.log(`⏱️ Current segment duration: ${currentDuration} seconds`);
const duration = (await getAudioDuration(audio.audio)) as number;
startingTime += duration + 0.25;
console.log(`⏱️ New startingTime after gap: ${startingTime}`);
}
console.log(`\n🎉 Final startingTime: ${startingTime}`);
await generateCleanSrt(transcript, uncleanSrtContentArr, secrets);
}