This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
315 lines (274 loc) · 9.58 KB
/
app.js
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
require("dotenv").config();
const express = require("express");
const multer = require("multer");
const ffmpeg = require("fluent-ffmpeg");
const fs = require("fs");
const path = require("path");
const axios = require("axios");
const FormData = require("form-data");
const tmp = require("tmp");
const cors = require("cors");
const app = express();
app.use(cors());
const upload = multer({
storage: multer.memoryStorage(),
fileFilter: (req, file, cb) => {
const acceptedMimeTypes = [
"audio/mpeg",
"audio/wav",
"audio/mp3",
"video/mp4",
"video/mpeg",
"video/quicktime",
"video/x-msvideo",
];
if (acceptedMimeTypes.includes(file.mimetype)) {
cb(null, true); // Accept the file
} else {
cb(
new Error("Invalid file type. Only audio and video files are allowed."),
false
); // Reject the file
}
},
});
const currentDirectory = process.cwd();
async function sendAudioToApi(filePath) {
const formData = new FormData();
formData.append("file", fs.createReadStream(filePath));
formData.append("model", "whisper-1");
formData.append("timestamps", "true");
const response = await axios.post(
"https://api.openai.com/v1/audio/transcriptions",
formData,
{
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
...formData.getHeaders(),
},
}
);
console.log("[INFO] WhisperAPI translation: ", response.data);
return response.data.text;
}
app.post("/upload", upload.single("file"), async (req, res) => {
try {
const fileBuffer = req.file.buffer;
const maxSegmentSize = 25 * 1024 * 1024; // 25 MB
// Create a temporary file and write the buffer to it
const tmpFile = tmp.fileSync();
fs.writeFileSync(tmpFile.name, fileBuffer);
// Automatically detect input format
const segmentFilenames = [];
const processFile = () =>
new Promise((resolve, reject) => {
const command = ffmpeg(tmpFile.name)
.withAudioCodec("libmp3lame")
.toFormat("mp3")
.on("start", () =>
console.log("Starting audio extraction and segmentation...")
)
.on("codecData", (data) =>
console.log("Input codec information:", data)
)
.on("progress", (progress) =>
console.log("Processing:", progress.percent + "% done")
)
.on("end", async () => {
console.log("Audio extraction and segmentation finished");
tmpFile.removeCallback(); // Clean up the temporary file
// Populate segmentFilenames array
let index = 0;
console.log("hello");
while (
(segmentFilePath = path.join(
currentDirectory,
`segment-${index.toString().padStart(3, "0")}.mp3`
)) &&
fs.existsSync(segmentFilePath)
) {
console.log(`Segmented file found: ${segmentFilePath}`);
segmentFilenames.push(
`segment-${index.toString().padStart(3, "0")}.mp3`
);
index++;
}
resolve();
})
.on("error", (err) => {
console.error("Error:", err);
tmpFile.removeCallback(); // Clean up the temporary file
reject(err);
});
// Segment the audio stream into 25 MB chunks
command.addOutputOptions([
`-f segment`,
`-segment_time ${(maxSegmentSize / 16000).toFixed(0)}`,
`-reset_timestamps 1`,
`-map 0:a`, // Map only the audio stream
]);
// Save each segment to a file
const segmentPattern = `segment-%03d.mp3`;
command.save(segmentPattern);
command.run();
});
// const processFileAsync = (inputFile) => {
// return new Promise(async (resolve, reject) => {
// try {
// const command = ffmpeg(inputFile)
// .withAudioCodec("libmp3lame")
// .toFormat("mp3")
// .on("start", () =>
// console.log("Starting audio extraction and segmentation...")
// )
// .on("codecData", (data) =>
// console.log("Input codec information:", data)
// )
// .on("progress", (progress) =>
// console.log("Processing:", progress.percent + "% done")
// )
// .on("error", (err) => {
// console.error("Error:", err);
// reject(err);
// });
// // Segment the audio stream into 25 MB chunks
// command.addOutputOptions([
// `-f segment`,
// `-segment_time ${(maxSegmentSize / 16000).toFixed(0)}`,
// `-reset_timestamps 1`,
// `-map 0:a`, // Map only the audio stream
// ]);
// // Save each segment to a file
// const segmentPattern = `segment-%03d.mp3`;
// console.log(
// "Executing ffmpeg command:",
// command._getArguments().join(" ")
// );
// command.save(segmentPattern);
// // Use promisify to convert 'end' event callback to a promise
// const commandEnd = promisify(command.on.bind(command), ["end"]);
// await commandEnd();
// // Populate segmentFilenames array
// let index = 0;
// while (
// (segmentFilePath = path.join(
// currentDirectory,
// `segment-${index.toString().padStart(3, "0")}.mp3`
// )) &&
// fs.existsSync(segmentFilePath)
// ) {
// console.log(`Segmented file found: ${segmentFilePath}`);
// segmentFilenames.push(
// `segment-${index.toString().padStart(3, "0")}.mp3`
// );
// index++;
// }
// console.log("Audio extraction and segmentation finished");
// tmpFile.removeCallback(); // Clean up the temporary file
// resolve();
// } catch (err) {
// console.error("Error:", err);
// tmpFile.removeCallback(); // Clean up the temporary file
// reject(err);
// }
// });
// };
await processFile();
// fs.unlinkSync(segmentFilename); // Remove segment file after processing
// Send segmented files to the external API
const transcriptionPromises = segmentFilenames.map(
async (segmentFilename) => {
const fileBuffer = await fs.promises.readFile(
path.join(currentDirectory, segmentFilename)
);
const formData = new FormData();
formData.append("file", fileBuffer, { filename: segmentFilename });
formData.append("model", "whisper-1");
const response = await axios.post(
"https://api.openai.com/v1/audio/transcriptions",
formData,
{
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
...formData.getHeaders(),
},
}
);
console.log(
"🚀 ~ file: app.js:171 ~ app.post ~ response:",
response.data
);
// fs.unlinkSync(path.join(currentDirectory, segmentFilename)); // Remove segment file after processing
return response.data.text;
}
);
const transcriptions = await Promise.all(transcriptionPromises);
res.status(200).send({
message: "Audio extraction and transcription completed successfully.",
transcriptions,
});
} catch (err) {
console.error(err);
res
.status(500)
.send("An error occurred during audio extraction and segmentation.");
}
});
app.post("/upload222", upload.single("file"), (req, res) => {
const inputFile = req.file.path;
const fileExtension = path.extname(req.file.originalname);
const outputDir = `output/${req.file.filename}`;
const audioOutput = `${outputDir}/audio${fileExtension}`;
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
ffmpeg(inputFile)
.outputOptions("-vn")
.save(audioOutput)
.on("end", () => {
ffmpeg.ffprobe(audioOutput, (err, metadata) => {
if (err) {
console.error("Error during ffprobe:", err);
res.sendStatus(500);
return;
}
const duration = metadata.streams[0].duration;
const segmentDuration = Math.min(
10,
(maxSegmentSize / req.file.size) * duration
);
ffmpeg(audioOutput)
.outputOptions([
`-f segment`,
`-segment_time ${segmentDuration}`,
`-c copy`,
])
.save(`${outputDir}/segment%03d${fileExtension}`)
.on("end", async () => {
const segmentFiles = fs
.readdirSync(outputDir)
.filter((file) => file.startsWith("segment"));
let combinedTranscript = "";
for (const segmentFile of segmentFiles) {
const transcript = await sendAudioToApi(
`${outputDir}/${segmentFile}`
);
combinedTranscript += transcript;
}
// fs.writeFileSync(`${outputDir}/transcript.txt`, combinedTranscript);
res.status(200).json({ data: combinedTranscript });
})
.on("error", (err) => {
console.error("Error during processing:", err);
res.sendStatus(500);
});
});
})
.on("error", (err) => {
console.error("Error during audio extraction:", err);
res.sendStatus(500);
});
});
app.listen(9000, () => {
console.log("Server is running on port 9000");
});