-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf2mp3.js
More file actions
36 lines (30 loc) · 1.33 KB
/
Copy pathpdf2mp3.js
File metadata and controls
36 lines (30 loc) · 1.33 KB
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
import { writeMediaToFile } from './lib/media.js';
import { splitTextIntoChunks } from './lib/text.js';
import { readPdfText } from './lib/pdf.js';
import { textChunkToSpeech } from './api/functions.js';
// OpenAI: ensure this value has at most 4096 characters
const TTS_CHUNK_SIZE = 4000;
const main = async () => {
// Command line arguments
const [,, pdfPath, outputFlag, outputPath] = process.argv;
if (outputFlag === '-o' && pdfPath && outputPath) {
const data = await readPdfText(pdfPath);
const chunks = splitTextIntoChunks(data, TTS_CHUNK_SIZE);
// convert the array of text chunks to the array of mp3 buffer promises
console.log(`Processing ${chunks.length} chunks in parallel.`);
const mp3Promises = chunks.map(textChunkToSpeech);
const mp3Chunks = await Promise.all(mp3Promises);
// combine all buffers and save to file
const combinedMp3Data = Buffer.concat(mp3Chunks); // Combine all MP3 data into a single buffer
await writeMediaToFile(combinedMp3Data, outputPath); // Write the combined media (MP3) data to file
} else {
console.log('Usage: node pdf2mp3.js file.pdf -o output.mp3');
}
}
// POSIX compliant apps should report an exit status
main()
.then(() => { process.exit(0); })
.catch((err) => {
console.error(err); // Writes to stderr
process.exit(1);
});