Skip to content

Commit

Permalink
fix: video quality[360p,720p,1080p]
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonortegajr committed May 28, 2024
1 parent d1f4ed8 commit 7267dfb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
7 changes: 7 additions & 0 deletions public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ document.getElementById('converter-form').addEventListener('submit', async funct
option.dataset.quality = format.quality; // Store quality for later use
qualitySelect.appendChild(option);
});

// Enable the download button once the quality options are loaded
document.getElementById('download-button').disabled = false;
} else {
alert(formatsData.error);
}
Expand All @@ -54,6 +57,10 @@ document.getElementById('converter-form').addEventListener('submit', async funct
cogIcon.classList.remove('spin-animation');
}
});

// Initially disable the download button
document.getElementById('download-button').disabled = true;

document.getElementById('download-button').addEventListener('click', function () {
const videoUrl = document.getElementById('video-url').value;
const qualitySelect = document.getElementById('quality');
Expand Down
40 changes: 34 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const express = require('express');
const cors = require('cors');
const ytdl = require('ytdl-core');
const app = express();
const PORT = process.env.PORT || 3000;
const PORT = process.env.PORT || 2000;

app.use(cors());
app.use(express.json());
Expand Down Expand Up @@ -32,6 +32,7 @@ app.post('/get-video-info', async (req, res) => {
}
});


app.post('/get-video-formats', async (req, res) => {
const videoUrl = req.body.url;
if (!ytdl.validateURL(videoUrl)) {
Expand All @@ -41,12 +42,35 @@ app.post('/get-video-formats', async (req, res) => {
try {
const info = await ytdl.getInfo(videoUrl);
const formats = info.formats
.filter(format => format.container === 'mp4' && format.qualityLabel) // Filter out non-mp4 formats and those without a quality label
.filter(format => {
return (
(format.itag === 18 && format.container === 'mp4' && format.qualityLabel === '360p' &&
format.codecs.includes('avc1.42001E') && format.audioBitrate === 96) ||
(format.itag === 248 && format.container === 'webm' && format.qualityLabel === '1080p' &&
format.codecs.includes('vp9')) ||
(format.itag === 136 && format.container === 'mp4' && format.qualityLabel === '720p' &&
format.codecs.includes('avc1.4d4016')) ||
(format.itag === 247 && format.container === 'webm' && format.qualityLabel === '720p' &&
format.codecs.includes('vp9')) ||
(format.itag === 135 && format.container === 'mp4' && format.qualityLabel === '480p' &&
format.codecs.includes('avc1.4d4014'))
// (format.itag === 140 && format.container === 'mp4' && format.audioBitrate === 128)
// (format.itag === 137 && format.container === 'mp4' && format.qualityLabel === '1080p' &&
// format.codecs.includes('avc1.640028')) ||
// (format.itag === 134 && format.container === 'mp4' && format.qualityLabel === '360p' &&
// format.codecs.includes('avc1.4d401e')) ||

);
})
.map(format => ({
quality: format.qualityLabel,
itag: format.itag,
container: format.container
container: format.container,
codecs: format.codecs,
bitrate: format.bitrate,
audioBitrate: format.audioBitrate
}));
console.log('Filtered formats:', formats); // Log the formats to check the output
res.json({ formats });
} catch (error) {
console.error(error);
Expand All @@ -66,10 +90,13 @@ app.get('/download', (req, res) => {
try {
ytdl.getInfo(videoUrl).then(info => {
const format = ytdl.chooseFormat(info.formats, { quality: itag });
const title = info.videoDetails.title.replace(/[<>:"\/\\|?*]+/g, ''); // Sanitize filename
if (!format) {
return res.status(400).json({ error: 'Unsupported format' });
}

const title = info.videoDetails.title.replace(/[<>:"\/\\|?*]+/g, '');
res.header('Content-Disposition', `attachment; filename="${title} (${quality}).${format.container}"`);

// Stream the video in the selected format

ytdl(videoUrl, { format }).pipe(res);
});
} catch (error) {
Expand All @@ -78,6 +105,7 @@ app.get('/download', (req, res) => {
}
});


app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
1 change: 0 additions & 1 deletion views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<nav class="navbar">
<div class="navbar-left">
<button class="menu-icon">&#9776;</button>
<button class="home-icon">&#8962;</button>
</div>
<div class="navbar-right">
<i class="fa fa-question-circle" aria-hidden="true"></i>
Expand Down

0 comments on commit 7267dfb

Please sign in to comment.