-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
96 lines (81 loc) · 3.13 KB
/
Copy pathapp.js
File metadata and controls
96 lines (81 loc) · 3.13 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
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
require('dotenv').config();
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const { generateAudio } = require('./audioGeneration');
const { processVideo, mergeTexts } = require('./gemini');
const fetch = require('node-fetch');
const ffmpeg = require('fluent-ffmpeg');
const app = express();
const port = 3050;
app.use(express.static(path.join(__dirname, 'public')));
const storage = multer.diskStorage({
destination: './uploads/',
filename: (req, file, cb) => {
cb(null, `${Date.now()}-${file.originalname}`);
}
});
const upload = multer({ storage: storage });
app.post('/api/upload', upload.single('videoInput'), async (req, res) => {
const { genre } = req.body;
const videoFilePath = req.file.path;
try {
const generatedText = await processVideo(videoFilePath, "");
const base10ResponseText = await makeBase10WhisperAICall(videoFilePath);
const mergedText = await mergeTexts(generatedText, base10ResponseText);
console.log("generatedText", generatedText);
console.log("base10ResponseText", base10ResponseText);
console.log("mergedText", mergedText);
const cdnLink = await generateAudio(mergedText, genre);
res.json({
audioUrl: cdnLink,
transcription: base10ResponseText,
description: generatedText
});
} catch (error) {
console.error("Error in /api/upload:", error);
res.status(500).json({ error: error.message || 'Failed to process the request' });
}
});
async function makeBase10WhisperAICall(videoFilePath) {
try {
const audioFilePath = await extractAudioFromVideo(videoFilePath);
const audioBase64 = fs.readFileSync(audioFilePath).toString('base64');
const resp = await fetch('https://model-7qkpp9dw.api.baseten.co/development/predict', {
method: 'POST',
headers: {
Authorization: 'Api-Key 7b7Gya44.tdNdHgu8NH4CsC0ZfjmKftR5sDNi34m0',
'Content-Type': 'application/json'
},
body: JSON.stringify({ "audio": audioBase64 })
});
if (!resp.ok) {
throw new Error(`Base10 Whisper AI API request failed with status ${resp.status}`);
}
const data = await resp.json();
return data.text || '';
} catch (error) {
console.error('Error in makeBase10WhisperAICall:', error);
throw error;
}
}
function extractAudioFromVideo(videoFilePath) {
return new Promise((resolve, reject) => {
const audioFilePath = videoFilePath.replace(path.extname(videoFilePath), '.mp3');
ffmpeg(videoFilePath)
.output(audioFilePath)
.on('end', () => {
console.log(`Audio extracted to ${audioFilePath}`);
resolve(audioFilePath);
})
.on('error', (err) => {
console.error('Error extracting audio:', err);
reject(err);
})
.run();
});
}
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});