YouTube videos ko directly download karna YouTube ke Terms of Service ke khilaf hai, is liye main aapko illegal ya unauthorized download ka code nahi de sakta. Lekin agar aap educational ya personal use ke liye kisi video ko download karna chahte hain (aur aapke paas proper permission ho), to main aapko legal open-source tools ka use batata hoon, jaise youtube-dl ya yt-dlp.
Agar aap HTML file se Node.js backend ko use karke YouTube video ko download karwana chahte hain, to yeh basic example hai:
1. Node.js Backend (using ytdl-core)
Install ytdl-core:
npm install express ytdl-core
Create server.js:
const express = require('express');
const ytdl = require('ytdl-core');
const app = express();
const path = require('path');
app.use(express.static('public'));
app.get('/download', async (req, res) => {
const videoURL = req.query.url;
if (!ytdl.validateURL(videoURL)) {
return res.status(400).send('Invalid URL');
}
const info = await ytdl.getInfo(videoURL);
const title = info.videoDetails.title.replace(/[^\w\s]/gi, '');
res.header('Content-Disposition', `attachment; filename="${title}.mp4"`);
ytdl(videoURL, { format: 'mp4' }).pipe(res);
});
app.listen(3000, () => {
console.log('Server started on http://localhost:3000');
});
2. HTML Frontend (public/index.html)
<!DOCTYPE html>
<html>
<head>
<title>YouTube Video Downloader</title>
</head>
<body>
<h1>Download YouTube Video</h1>
<form id="download-form">
<input type="text" id="url" placeholder="Enter YouTube URL" required />
<button type="submit">Download</button>
</form>
<script>
document.getElementById('download-form').addEventListener('submit', function(e) {
e.preventDefault();
const url = document.getElementById('url').value;
window.location.href = `/download?url=${encodeURIComponent(url)}`;
});
</script>
</body>
</html>
Yeh solution locally run hone wale server ke liye hai. Aap is code ko apne system pe run kar sakte hain, lekin isse public server pe host karna YouTube ke policies ka violation ho sakta hai.
Kya aap chahenge ke main aapko yeh code zip file mein ready kar ke do?
No comments:
Post a Comment