Skip to content

Commit

Permalink
change: on video quality
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonortegajr committed May 30, 2024
1 parent 6ead0cd commit 5467200
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 65 deletions.
15 changes: 8 additions & 7 deletions public/css/home.css
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ i {
flex-wrap: wrap;
gap: 20px;
padding: 15px;
width: 75%;
max-width: 1200px;
width: 70%;
max-width: 1000px;
}

.card {
Expand Down Expand Up @@ -234,6 +234,12 @@ i {
margin-right: 0.5em;
}

.container {
width: 100%;
padding: 10px;
margin-right: 0.5em;
}

.card .text-section .title-small {
font-size: 1.2em;
}
Expand All @@ -242,11 +248,6 @@ i {
font-size: small;
text-align: justify;
}
.container {
width: 100%;
padding: 10px;
margin-right: 0.5em;
}

.support-me {
display: none; /* Hide "Buy Me a Coffee" on smaller screens */
Expand Down
88 changes: 30 additions & 58 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ const ytdl = require('ytdl-core');
const app = express();
const path = require("path");

const PORT = process.env.PORT || 2000;
const PORT = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');

//call end points
const formatCache = new Map(); // Cache for storing video formats

app.get('/', (req, res) => {
res.render('index');
});

app.get('/tubetogo', (req, res) => {
res.render('tubetogo');
})
});

app.post('/get-video-info', async (req, res) => {
const videoUrl = req.body.url;
Expand All @@ -38,73 +40,41 @@ 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)) {
// return res.status(400).json({ error: 'Invalid URL' });
// }

// try {
// const info = await ytdl.getInfo(videoUrl);
// const formats = info.formats
// .filter(format => {
// return (
// (format.itag === 18 && format.container === 'mp4' && format.qualityLabel === '360p' &&
// format.codecs.includes('avc1.42001E') && format.audioBitrate === 96) ||
// (format.itag === 137 && format.container === 'mp4' && format.qualityLabel === '1080p' &&
// format.codecs.includes('avc1.640028')) ||
// (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 === 134 && format.container === 'mp4' && format.qualityLabel === '360p' &&
// format.codecs.includes('avc1.4d401e')) ||
// (format.itag === 140 && format.container === 'mp4' && format.audioBitrate === 128)
// );
// })
// .map(format => ({
// quality: format.qualityLabel,
// itag: format.itag,
// 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);
// res.status(500).json({ error: 'Failed to fetch video formats' });
// }
// });

app.post('/get-video-formats', async (req, res) => {
const videoUrl = req.body.url;
if (!ytdl.validateURL(videoUrl)) {
return res.status(400).json({ error: 'Invalid URL' });
}

try {
const supportedQualities = ['360p', '1080p', '720p', '480p']; // Define supported qualities
const info = await ytdl.getInfo(videoUrl);
const formats = info.formats
.filter(format => {
// Check if format quality is in the supported qualities array
return supportedQualities.includes(format.qualityLabel);
})
.map(format => ({
let formats = formatCache.get(videoUrl);
if (!formats) {
const info = await ytdl.getInfo(videoUrl);
formats = info.formats.filter(format => {
return (
(format.itag === 137 && format.container === 'mp4' && format.qualityLabel === '1080p' &&
format.codecs.includes('vp9')) ||
(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 === 18 && format.container === 'mp4' && format.qualityLabel === '360p' &&
format.codecs.includes('avc1.42001E') && format.audioBitrate === 96)
);
}).map(format => ({
quality: format.qualityLabel,
itag: format.itag,
container: format.container,
codecs: format.codecs,
bitrate: format.bitrate,
audioBitrate: format.audioBitrate
}));
formatCache.set(videoUrl, formats);
}
console.log('Filtered formats:', formats); // Log the formats to check the output
res.json({ formats });
} catch (error) {
Expand Down Expand Up @@ -133,14 +103,16 @@ app.get('/download', (req, res) => {
res.header('Content-Disposition', `attachment; filename="${title} (${quality}).${format.container}"`);

ytdl(videoUrl, { format }).pipe(res);
}).catch(error => {
console.error(error);
res.status(500).json({ error: 'Failed to download video' });
});
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to download video' });
}
});


app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
});

0 comments on commit 5467200

Please sign in to comment.