| const express = require('express'); |
| const ytdl = require('@distube/ytdl-core'); |
| const app = express(); |
| const PORT = 3000; |
|
|
| |
| app.get('/download-audio', async (req, res) => { |
| const videoUrl = req.query.url; |
|
|
| |
| if (!videoUrl) { |
| return res.status(400).send('برجاء إرسال رابط فيديو يوتيوب صحيح. مثال: ?url=https://youtube.com/...'); |
| } |
|
|
| try { |
| |
| const info = await ytdl.getInfo(videoUrl); |
| |
| const videoTitle = info.videoDetails.title.replace(/[^\w\s]/gi, ''); |
|
|
| |
| res.setHeader('Content-Disposition', `attachment; filename="${videoTitle}.mp3"`); |
| res.setHeader('Content-Type', 'audio/mpeg'); |
|
|
| |
| ytdl(videoUrl, { |
| filter: 'audioonly', |
| quality: 'highestaudio' |
| }).pipe(res); |
|
|
| } catch (error) { |
| console.error('حدث خطأ:', error.message); |
| res.status(500).send('فشل تشغيل الطلب. تأكد من أن الرابط صحيح أو جرب لاحقاً.'); |
| } |
| }); |
|
|
| |
| app.listen(PORT, () => { |
| console.log(`السيرفر يعمل الآن على: http://localhost:${PORT}`); |
| }); |