import ytdl from 'ytdl-core';
// ....
fastify.get('/ytmp3', async (req, res) => {
let { id, lang } = req.query;
if (!ytdl.validateID(id)) {
res.send({
result: false,
error: "Invalid ID"
})
}
try {
let ytdata = await ytdl.getInfo(`https://youtube.com/watch?v=${id}`, { lang: lang ?? 'en' })
let audio = ytdata.player_response.streamingData.adaptiveFormats.filter((d) => d.mimeType.startsWith("audio"))
res.send({
result: true,
audio
})
} catch (err) {
res.send({
result: false,
error: String(err)
})
}
})
Last active
August 19, 2021 15:10
-
-
Save ArnabXD/fdcdfa27d71c69236c1cccd2dd6eccf4 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some notes:
lang ?? 'en'
can lead to the language being empty, you can use||
instead.ytdl.validateURL
to accept both URLs and IDs.fastify.get
andres.send
parts to make it usable for any http library.