-
Notifications
You must be signed in to change notification settings - Fork 0
/
processVideos.ts
345 lines (310 loc) · 10.2 KB
/
processVideos.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import dotenv from "dotenv";
import fs from "fs-extra";
import path from "path";
import ytdl from "@distube/ytdl-core";
import ffmpeg from "fluent-ffmpeg";
import OpenAI from "openai";
import cliProgress from "cli-progress";
import yaml from "js-yaml";
import pLimit from "p-limit";
import * as art from "ascii-art"; // Updated import statement
import { AssemblyAI } from "assemblyai";
dotenv.config();
const ASSEMBLYAI_API_KEY = process.env.ASSEMBLYAI_API_KEY;
if (!ASSEMBLYAI_API_KEY) {
console.error(
"Error: ASSEMBLYAI_API_KEY is not set in the environment variables.",
);
process.exit(1);
}
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
if (!OPENAI_API_KEY) {
console.error(
"Error: OPENAI_API_KEY is not set in the environment variables.",
);
process.exit(1);
}
// Initialize OpenAI client
const openai = new OpenAI({
apiKey: OPENAI_API_KEY,
});
// Initialize AssemblyAI client
const assemblyai = new AssemblyAI({
apiKey: ASSEMBLYAI_API_KEY,
});
const configPath = path.join(__dirname, "config.yaml");
const config = yaml.load(fs.readFileSync(configPath, "utf8")) as {
videoUrls: string[];
proxies: string[];
transcriptionService: "openai" | "assemblyai";
};
const VIDEO_URLS: string[] = config.videoUrls;
const TRANSCRIPTION_SERVICE: "openai" | "assemblyai" =
config.transcriptionService;
const TRANSCRIPTS_DIR = path.join(__dirname, "transcripts");
const AUDIO_DIR = path.join(__dirname, "audio");
const CHUNKS_DIR = path.join(__dirname, "chunks");
// Ensure directories exist
fs.ensureDirSync(TRANSCRIPTS_DIR);
fs.ensureDirSync(AUDIO_DIR);
fs.ensureDirSync(CHUNKS_DIR);
/**
* Downloads YouTube audio and converts it to MP3 with a filename based on video title and author.
* @param videoUrl - The YouTube video URL.
* @returns Path to the downloaded MP3 file, video title, and author.
*/
async function downloadAudio(
videoUrl: string,
): Promise<{ audioPath: string; videoTitle: string; videoAuthor: string }> {
const info = await ytdl.getInfo(videoUrl);
const videoTitle = info.videoDetails.title.replace(/[^a-z0-9 \-_]/gi, "");
const videoAuthor = info.videoDetails.author.name.replace(
/[^a-z0-9 \-_]/gi,
"",
);
const output = path.join(AUDIO_DIR, `${videoTitle} - ${videoAuthor}.mp3`);
if (await fs.pathExists(output)) {
console.log(
`MP3 file already exists for "${videoTitle}". Skipping download.`,
);
return { audioPath: output, videoTitle, videoAuthor };
}
// Use the highest quality audio-only format
const format = ytdl.chooseFormat(info.formats, { quality: "highestaudio" });
return new Promise((resolve, reject) => {
ytdl(videoUrl, {
format: format,
requestOptions: {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
},
},
})
.pipe(fs.createWriteStream(output))
.on("finish", () => {
console.log(`Download completed for ${videoTitle}.`);
resolve({ audioPath: output, videoTitle, videoAuthor });
})
.on("error", (err) => {
console.error(
`Error during download for ${videoTitle}: ${err.message}`,
);
reject(err);
});
});
}
/**
* Splits the audio file into smaller chunks under the specified size limit.
* @param filePath - Path to the original MP3 file.
* @param videoTitle - Title of the video (used for chunk directory).
* @param videoId - ID of the video (used for chunk directory).
* @param chunkSizeMB - Maximum size for each chunk in MB.
* @returns Array of chunk file paths.
*/
async function splitAudio(
filePath: string,
videoTitle: string,
videoId: string,
chunkSizeMB: number = 25,
): Promise<string[]> {
const stats = await fs.stat(filePath);
const fileSize = stats.size;
const chunkSize = chunkSizeMB * 1024 * 1024; // Convert MB to bytes
const numberOfChunks = Math.ceil(fileSize / chunkSize);
if (numberOfChunks <= 1) {
return [filePath];
}
// Create a unique directory for chunks of this video
const videoChunksDir = path.join(CHUNKS_DIR, `${videoTitle}-${videoId}`);
await fs.ensureDir(videoChunksDir);
return new Promise((resolve, reject) => {
ffmpeg(filePath)
.outputOptions([
`-f segment`,
`-segment_time ${chunkSizeMB * 60}`, // Use fixed segment time in seconds
`-reset_timestamps 1`,
])
.output(path.join(videoChunksDir, "chunk-%03d.mp3"))
.on("end", async () => {
try {
const files = await fs.readdir(videoChunksDir);
const chunkFiles = files
.filter(
(file) => file.startsWith("chunk-") && file.endsWith(".mp3"),
)
.map((file) => path.join(videoChunksDir, file))
.sort((a, b) => {
const aNum = parseInt(a.match(/\d+/)?.[0] || "0");
const bNum = parseInt(b.match(/\d+/)?.[0] || "0");
return aNum - bNum;
});
if (chunkFiles.length === 0) {
throw new Error("No chunks were created");
}
console.log(`Created ${chunkFiles.length} chunks for ${videoTitle}`);
resolve(chunkFiles);
} catch (error) {
reject(error);
}
})
.on("error", (err: Error) => {
reject(err);
})
.run();
});
}
/**
* Transcribes audio using the selected transcription service.
* @param filePath - Path to the MP3 file.
* @returns Transcribed text.
*/
async function transcribeAudio(filePath: string): Promise<string> {
if (TRANSCRIPTION_SERVICE === "openai") {
return transcribeWithOpenAI(filePath);
} else {
return transcribeWithAssemblyAI(filePath);
}
}
/**
* Transcribes audio using OpenAI Whisper API with retry mechanism.
* @param filePath - Path to the MP3 file.
* @returns Transcribed text.
*/
async function transcribeWithOpenAI(
filePath: string,
maxRetries = 3,
retryDelay = 5000,
): Promise<string> {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const fileStream = fs.createReadStream(filePath);
const response = await openai.audio.transcriptions.create({
file: fileStream,
model: "whisper-1",
});
return response.text;
} catch (error: any) {
if (attempt === maxRetries) {
console.error(
`Error transcribing ${filePath} after ${maxRetries} attempts:`,
error.message,
);
return "";
}
console.warn(
`Attempt ${attempt} failed for ${filePath}. Retrying in ${retryDelay / 1000} seconds...`,
);
await new Promise((resolve) => setTimeout(resolve, retryDelay));
}
}
return ""; // This line should never be reached, but TypeScript requires it
}
/**
* Transcribes audio using AssemblyAI.
* @param filePath - Path to the MP3 file.
* @returns Transcribed text.
*/
async function transcribeWithAssemblyAI(filePath: string): Promise<string> {
try {
const transcript = await assemblyai.transcripts.transcribe({
audio: filePath,
});
return transcript.text ?? "";
} catch (error: any) {
console.error(
`Error transcribing ${filePath} with AssemblyAI:`,
error.message,
);
return "";
}
}
/**
* Processes a single YouTube video: downloads audio and transcribes it.
* @param videoUrl - The YouTube video URL.
*/
async function processVideo(videoUrl: string) {
const progressBar = new cliProgress.SingleBar(
{
format: "{videoId} |{bar}| {percentage}% ",
hideCursor: true,
},
cliProgress.Presets.shades_classic,
);
try {
const info = await ytdl.getInfo(videoUrl);
const videoTitle = info.videoDetails.title.replace(/[^a-z0-9 \-_]/gi, "");
const videoAuthor = info.videoDetails.author.name.replace(
/[^a-z0-9 \-_]/gi,
"",
);
const videoId = info.videoDetails.videoId;
const transcriptPath = path.join(
TRANSCRIPTS_DIR,
`${videoTitle} - ${videoAuthor}.txt`,
);
if (await fs.pathExists(transcriptPath)) {
console.log(
`Transcript already exists for "${videoTitle}". Skipping transcription.`,
);
return;
}
progressBar.start(100, 0, { videoId: "Initializing" });
// Download audio
progressBar.update(10, { videoId: `Downloading audio` });
const { audioPath } = await downloadAudio(videoUrl);
progressBar.update(30, { videoId: `Downloaded: ${videoTitle}` });
// Transcribe audio
progressBar.update(40, { videoId: `Transcribing audio` });
let fullTranscript = "";
if (TRANSCRIPTION_SERVICE === "openai") {
// Split audio if using OpenAI (due to file size limitations)
const chunks = await splitAudio(audioPath, videoTitle, videoId);
progressBar.update(50, {
videoId: `Audio split into ${chunks.length} chunks`,
});
// Transcribe each chunk
for (let i = 0; i < chunks.length; i++) {
progressBar.update(50 + (i / chunks.length) * 40, {
videoId: `Transcribing chunk ${i + 1}`,
});
const transcript = await transcribeAudio(chunks[i]);
if (transcript) {
fullTranscript += transcript + " ";
} else {
console.warn(
`Failed to transcribe chunk ${i + 1} for ${videoTitle}.`,
);
}
}
} else {
// Use AssemblyAI for transcription (no chunking required)
fullTranscript = await transcribeAudio(audioPath);
}
// Save transcript
progressBar.update(95, { videoId: `Saving transcript` });
await fs.writeFile(transcriptPath, fullTranscript.trim());
progressBar.update(100, { videoId: `Completed: ${videoTitle}` });
console.log(`Transcript saved to ${transcriptPath}`);
// Cleanup chunks
await fs.remove(path.join(CHUNKS_DIR, `${videoTitle}-${videoId}`));
progressBar.stop();
} catch (err) {
console.error(`An error occurred while processing ${videoUrl}:`, err);
progressBar.stop();
}
}
/**
* Main function to process all videos.
*/
async function main() {
const CONCURRENCY_LIMIT = 3;
const limit = pLimit(CONCURRENCY_LIMIT);
await Promise.all(
VIDEO_URLS.map((videoUrl) => limit(() => processVideo(videoUrl))),
);
const rendered = await art.font("Success!", "Doom").toPromise();
console.log(rendered);
console.log("All videos have been processed.");
}
main();