Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const ytdl = require('@distube/ytdl-core');
|
| 3 |
+
const fetch = require('node-fetch');
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
const PORT = 7860;
|
| 7 |
+
|
| 8 |
+
async function getTranscript(videoUrl) {
|
| 9 |
+
const info = await ytdl.getInfo(videoUrl);
|
| 10 |
+
const captions = info.player_response.captions?.playerCaptionsTracklistRenderer.captionTracks;
|
| 11 |
+
if (!captions) return null;
|
| 12 |
+
const transcriptUrl = captions[0].baseUrl;
|
| 13 |
+
const res = await fetch(transcriptUrl);
|
| 14 |
+
const xml = await res.text();
|
| 15 |
+
return xml.match(/<text[^>]*>(.*?)<\/text>/g).map(t => t.replace(/<[^>]+>/g, ''));
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
app.get('/transcript', async (req, res) => {
|
| 19 |
+
const { url } = req.query;
|
| 20 |
+
if (!url) return res.status(400).json({ error: 'Missing video URL' });
|
| 21 |
+
try {
|
| 22 |
+
const transcript = await getTranscript(url);
|
| 23 |
+
res.json({ transcript });
|
| 24 |
+
} catch (error) {
|
| 25 |
+
res.status(500).json({ error: 'Failed to fetch transcript' });
|
| 26 |
+
}
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
app.listen(PORT, () => {
|
| 30 |
+
console.log(`Server running on http://localhost:${PORT}`);
|
| 31 |
+
});
|