|
1 | 1 | import { OpenAI } from 'openai';
|
2 | 2 | import Configuration from 'openai';
|
3 |
| -import { spawn } from 'child_process'; |
| 3 | +import { execSync, spawn } from 'child_process'; |
4 | 4 | import { nanoid } from 'nanoid';
|
5 | 5 | import { signUrl, uploadToS3 } from '../aws/upload';
|
6 |
| -import fs, { unlinkSync } from 'fs'; |
| 6 | +import fs, { statSync, unlinkSync } from 'fs'; |
7 | 7 | import { join } from 'path';
|
8 | 8 | import { getAudioDuration, splitAudioOnSilence } from '../audio/chunker';
|
9 | 9 |
|
@@ -52,28 +52,26 @@ export class TranscribeService {
|
52 | 52 | this.openai = new OpenAI({ ...config });
|
53 | 53 | }
|
54 | 54 |
|
55 |
| - private convertToMP3(videoUrl: string): Promise<string[]> { |
| 55 | + private async convertToMP3(videoUrl: string): Promise<string[]> { |
56 | 56 | const stagingDir = process.env.STAGING_DIR || '/tmp/';
|
57 | 57 | const tempFile = join(stagingDir, `${nanoid()}.mp3`);
|
58 | 58 |
|
59 |
| - return new Promise((resolve, reject) => { |
| 59 | + try { |
60 | 60 | console.log(`Converting ${videoUrl} to ${tempFile}`);
|
61 |
| - const ffmpeg = spawn('ffmpeg', ['-i', videoUrl, '-f', 'mp3', tempFile]); |
62 |
| - ffmpeg.on('close', async () => { |
63 |
| - try { |
64 |
| - console.log(`Conversion compleded. Now splitting into chunks...`); |
65 |
| - const chunks = await splitAudioOnSilence(tempFile); |
66 |
| - unlinkSync(tempFile); |
67 |
| - resolve(chunks); |
68 |
| - } catch (error) { |
69 |
| - reject(error); |
70 |
| - } |
71 |
| - }); |
72 |
| - ffmpeg.on('error', (err) => { |
73 |
| - console.error('Error:', err); |
74 |
| - reject(err); |
75 |
| - }); |
76 |
| - }); |
| 61 | + execSync(`ffmpeg -i "${videoUrl}" -f mp3 "${tempFile}"`); |
| 62 | + console.log(`Conversion completed. Now splitting into chunks...`); |
| 63 | + if (!statSync(tempFile).isFile()) { |
| 64 | + throw new Error('Error converting video, temp file not found'); |
| 65 | + } |
| 66 | + const chunks = splitAudioOnSilence(tempFile); |
| 67 | + console.log(`Splited into ${chunks.length} chunks`); |
| 68 | + unlinkSync(tempFile); |
| 69 | + console.log(`Deleted temp file ${tempFile}`); |
| 70 | + return chunks; |
| 71 | + } catch (error) { |
| 72 | + console.error(error); |
| 73 | + throw new Error('Error converting video'); |
| 74 | + } |
77 | 75 | }
|
78 | 76 |
|
79 | 77 | get id(): string {
|
|
0 commit comments