This commit is contained in:
Andrey Kondratev
2025-08-28 16:37:59 +05:00
parent 913f833b8c
commit b8e2bf1090
23 changed files with 838 additions and 7 deletions

View File

@@ -2,6 +2,10 @@ import express, { Request, Response, NextFunction } from 'express';
import path from 'path';
import fs from 'fs';
import ffmpeg from 'fluent-ffmpeg';
// Configure ffmpeg paths
ffmpeg.setFfmpegPath('/usr/bin/ffmpeg');
ffmpeg.setFfprobePath('/usr/bin/ffprobe');
import { Database } from './database';
import { SoundCloudService } from './soundcloud';
@@ -87,9 +91,36 @@ app.post('/api/convert', async (req: Request, res: Response) => {
const audioStream = await soundcloud.getAudioStream(videoId, url);
console.log('Audio stream obtained, starting FFmpeg conversion...');
// Convert to MP3 using ffmpeg
// Download to temporary file first, then convert
const tempInputPath = path.join(downloadsDir, `temp_${videoId}.tmp`);
// Save stream to temporary file
await new Promise<void>((resolve, reject) => {
const conversion = ffmpeg(audioStream)
const writeStream = fs.createWriteStream(tempInputPath);
audioStream.pipe(writeStream);
audioStream.on('end', resolve);
audioStream.on('error', reject);
writeStream.on('error', reject);
});
console.log('Temporary file saved, starting FFmpeg conversion...');
// Debug: check temp file
const stats = fs.statSync(tempInputPath);
console.log(`Temp file size: ${stats.size} bytes`);
// Test ffmpeg with simple command first
try {
const { execSync } = require('child_process');
const result = execSync(`ffmpeg -i "${tempInputPath}" -t 1 -f null -`, { encoding: 'utf8', stdio: 'pipe' });
console.log('FFmpeg file test passed');
} catch (e: any) {
console.error('FFmpeg file test failed:', e.stderr || e.message);
}
// Convert temporary file to MP3 using ffmpeg
await new Promise<void>((resolve, reject) => {
const conversion = ffmpeg(tempInputPath)
.audioCodec('libmp3lame')
.audioBitrate('192k')
.audioChannels(2)
@@ -106,10 +137,16 @@ app.post('/api/convert', async (req: Request, res: Response) => {
})
.on('end', () => {
console.log('MP3 conversion completed successfully');
// Clean up temporary file
fs.unlink(tempInputPath, (err) => {
if (err) console.error('Failed to delete temp file:', err);
});
resolve();
})
.on('error', (err: Error) => {
console.error('FFmpeg error:', err.message);
// Clean up temporary file on error
fs.unlink(tempInputPath, () => {});
reject(err);
});