Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
277 lines (239 loc) · 7.39 KB
/
Copy pathserver.js
File metadata and controls
277 lines (239 loc) · 7.39 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
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
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { v4: uuidv4 } = require('uuid');
// Configure FFmpeg path
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
ffmpeg.setFfmpegPath(ffmpegPath);
console.log('FFmpeg configured at:', ffmpegPath);
const app = express();
const PORT = process.env.PORT || 3001;
// Middleware
app.use(cors());
app.use(express.json({ limit: '50mb' }));
// Authentication middleware
const authenticateRequest = (req, res, next) => {
const authHeader = req.headers.authorization;
const expectedToken = process.env.PROCESSING_SERVICE_TOKEN || 'default-token';
if (!authHeader || authHeader !== `Bearer ${expectedToken}`) {
return res.status(401).json({ error: 'Unauthorized' });
}
next();
};
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'healthy',
service: 'video-to-audio-processor',
version: '1.0.0',
timestamp: new Date().toISOString(),
ffmpeg: 'native',
uptime: process.uptime(),
memory: {
used: Math.round(process.memoryUsage().heapUsed / 1024 / 1024),
total: Math.round(process.memoryUsage().heapTotal / 1024 / 1024),
unit: 'MB'
}
});
});
// Process video endpoint
app.post('/process', authenticateRequest, async (req, res) => {
const { jobId, videoUrl, webhookUrl, callbackHeaders } = req.body;
if (!jobId || !videoUrl || !webhookUrl) {
return res.status(400).json({
error: 'Missing required fields',
required: ['jobId', 'videoUrl', 'webhookUrl']
});
}
const externalJobId = uuidv4();
// Respond immediately
res.json({
success: true,
externalJobId,
jobId,
message: 'Processing started',
estimatedTime: '30-60 seconds'
});
// Process asynchronously
processVideoAsync(jobId, externalJobId, videoUrl, webhookUrl, callbackHeaders);
});
async function processVideoAsync(jobId, externalJobId, videoUrl, webhookUrl, callbackHeaders = {}) {
const startTime = Date.now();
let downloadedFilePath = null;
let audioFilePath = null;
try {
console.log(`Starting processing for job ${jobId} (external: ${externalJobId})`);
// Notify processing started
await sendWebhook(webhookUrl, {
jobId,
externalJobId,
status: 'processing',
progress: 10,
serviceType: 'railway'
}, callbackHeaders);
// Step 1: Download video file
console.log(`Downloading video for job ${jobId}`);
downloadedFilePath = await downloadFile(videoUrl, externalJobId);
await sendWebhook(webhookUrl, {
jobId,
externalJobId,
status: 'processing',
progress: 40,
serviceType: 'railway'
}, callbackHeaders);
// Step 2: Extract audio using native FFmpeg
console.log(`Extracting audio for job ${jobId}`);
audioFilePath = await extractAudio(downloadedFilePath, externalJobId);
await sendWebhook(webhookUrl, {
jobId,
externalJobId,
status: 'processing',
progress: 80,
serviceType: 'railway'
}, callbackHeaders);
// Step 3: Upload to temporary storage (you could use your own storage here)
console.log(`Uploading audio for job ${jobId}`);
const audioUrl = await uploadAudio(audioFilePath, externalJobId);
const processingTime = Date.now() - startTime;
// Notify completion
await sendWebhook(webhookUrl, {
jobId,
externalJobId,
status: 'completed',
progress: 100,
audioUrl,
processingTime,
serviceType: 'railway',
metadata: {
processingTimeMs: processingTime,
processingTimeSeconds: Math.round(processingTime / 1000),
service: 'railway-native-ffmpeg'
}
}, callbackHeaders);
console.log(`Job ${jobId} completed successfully in ${processingTime}ms`);
} catch (error) {
console.error(`Job ${jobId} failed:`, error);
await sendWebhook(webhookUrl, {
jobId,
externalJobId,
status: 'failed',
error: error.message,
serviceType: 'railway'
}, callbackHeaders);
} finally {
// Cleanup
if (downloadedFilePath) cleanupFile(downloadedFilePath);
if (audioFilePath) cleanupFile(audioFilePath);
}
}
async function downloadFile(url, jobId) {
const tempDir = os.tmpdir();
// Extract clean extension without URL parameters
const urlWithoutParams = url.split('?')[0];
const extension = path.extname(urlWithoutParams) || '.mp4';
const fileName = `${jobId}_video${extension}`;
const filePath = path.join(tempDir, fileName);
const response = await axios({
method: 'GET',
url: url,
responseType: 'stream',
timeout: 120000, // 2 minutes
headers: {
'User-Agent': 'Railway-Video-Processor/1.0.0'
}
});
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => resolve(filePath));
writer.on('error', reject);
});
}
async function extractAudio(inputPath, jobId) {
const tempDir = os.tmpdir();
const outputPath = path.join(tempDir, `${jobId}.mp3`);
return new Promise((resolve, reject) => {
ffmpeg(inputPath)
.audioCodec('libmp3lame')
.audioBitrate(128)
.audioChannels(2)
.audioFrequency(44100)
.format('mp3')
.output(outputPath)
.on('end', () => {
console.log('Audio extraction completed');
resolve(outputPath);
})
.on('error', (err) => {
console.error('FFmpeg error:', err);
reject(err);
})
.run();
});
}
async function uploadAudio(audioFilePath, jobId) {
// Read the audio file
const audioBuffer = fs.readFileSync(audioFilePath);
const fileName = `${jobId}.mp3`;
// Upload to Vercel Blob Storage using the same token
try {
const response = await axios.put(
`https://blob.vercel-storage.com/${fileName}`,
audioBuffer,
{
headers: {
'Authorization': `Bearer ${process.env.BLOB_READ_WRITE_TOKEN}`,
'Content-Type': 'audio/mpeg',
'x-content-type': 'audio/mpeg'
},
timeout: 60000
}
);
return response.data.url;
} catch (error) {
console.error('Upload failed:', error);
// Fallback: return a temporary URL (you could implement a simple file server)
throw new Error('Failed to upload audio file');
}
}
async function sendWebhook(webhookUrl, payload, headers = {}) {
try {
await axios.post(webhookUrl, payload, {
headers: {
'Content-Type': 'application/json',
...headers
},
timeout: 10000
});
} catch (error) {
console.error('Webhook failed:', error.message);
}
}
function cleanupFile(filePath) {
try {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
console.log(`Cleaned up: ${filePath}`);
}
} catch (error) {
console.error(`Failed to cleanup ${filePath}:`, error);
}
}
// Error handling
app.use((error, req, res, next) => {
console.error('Server error:', error);
res.status(500).json({
error: 'Internal server error',
message: error.message
});
});
// Start server
app.listen(PORT, () => {
console.log(`🚀 Video-to-Audio Processor running on port ${PORT}`);
console.log(`📊 Health check: http://localhost:${PORT}/health`);
console.log(`🎵 Process endpoint: http://localhost:${PORT}/process`);
});