Native yt-dlp usage

This commit is contained in:
Wizzard 2024-08-17 15:58:45 -04:00
parent 387bef3677
commit 4862446076
1 changed files with 38 additions and 29 deletions

View File

@ -1,5 +1,5 @@
const { addToQueue, playNextInQueue } = require('../utils/queueManager'); const { addToQueue, playNextInQueue } = require('../utils/queueManager');
const ytDlpExec = require('yt-dlp-exec'); const { exec } = require('child_process');
const { v4: uuidv4 } = require('uuid'); const { v4: uuidv4 } = require('uuid');
const path = require('path'); const path = require('path');
const { EmbedBuilder } = require('discord.js'); const { EmbedBuilder } = require('discord.js');
@ -48,13 +48,8 @@ module.exports = {
} }
} }
if (!searchQuery) {
return message.reply('Please provide a YouTube link, MP3 link, or a song name.');
}
if (isValidURL(searchQuery)) { if (isValidURL(searchQuery)) {
url = searchQuery; url = searchQuery;
if (url.endsWith('.mp3')) { if (url.endsWith('.mp3')) {
title = path.basename(url); title = path.basename(url);
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
@ -63,34 +58,22 @@ module.exports = {
const buffer = await response.buffer(); const buffer = await response.buffer();
fs.writeFileSync(tempFilePath, buffer); fs.writeFileSync(tempFilePath, buffer);
} else { } else {
const info = await ytDlpExec(url, { dumpSingleJson: true });
title = info.title;
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
await ytDlpExec(url, { title = await downloadWithYtDlp(url, tempFilePath);
cookies: path.join(__dirname, '../cookies.txt'),
format: 'bestaudio',
output: tempFilePath,
quiet: true,
});
} }
} else { } else {
const searchResult = await ytDlpExec(`ytsearch:${searchQuery}`, { const searchUrl = `ytsearch:${searchQuery}`;
dumpSingleJson: true, const searchResult = await getYtDlpInfo(searchUrl);
noPlaylist: true,
format: 'bestaudio/best', if (!searchResult || !searchResult.entries || searchResult.entries.length === 0) {
quiet: true, return message.reply('No results found for your search query.');
}); }
url = searchResult.entries[0].webpage_url; url = searchResult.entries[0].webpage_url;
title = searchResult.entries[0].title; title = searchResult.entries[0].title;
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
await ytDlpExec(url, { await downloadWithYtDlp(url, tempFilePath);
cookies: path.join(__dirname, '../cookies.txt'),
format: 'bestaudio',
output: tempFilePath,
quiet: true,
});
} }
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
@ -107,7 +90,7 @@ module.exports = {
} catch (error) { } catch (error) {
console.error('yt-dlp error:', error); console.error('yt-dlp error:', error);
message.reply('Failed to retrieve or download video. Please try again.'); message.reply('Failed to retrieve or download the video. Please try again.');
} }
}, },
}; };
@ -120,3 +103,29 @@ function isValidURL(string) {
return false; return false;
} }
} }
function downloadWithYtDlp(url, output) {
return new Promise((resolve, reject) => {
const command = `yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output ${output} --quiet --verbose "${url}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
reject(`yt-dlp error: ${stderr}`);
} else {
resolve(output);
}
});
});
}
function getYtDlpInfo(url) {
return new Promise((resolve, reject) => {
const command = `yt-dlp --dump-single-json "${url}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
reject(`yt-dlp info error: ${stderr}`);
} else {
resolve(JSON.parse(stdout));
}
});
});
}