0

I want to have function like this, where I take youtube shorts link, download the video into some buffer and return it as a result.

const handleYoutubeLink = async (url) => {
    if (!url.includes('youtube')) return null

    let buffer = await youtubedl(url)

    return buffer
}

To use it later in this kind of way. Using node-telegram-bot-api to send the video as a buffer, which is totally supported.

handleYoutubeLink(url).then((dataBuffer) => {
    bot.sendVideo(chatId, dataBuffer, sendVideoOptions)
})

I tried using youtube-dl-exec library, but I didn't found a way to download the video with audio, and I don't quite understand how to put it in buffer. I also considered to just download the video into path and deleting it after I done with it, but it doesn't sound quite well. Maybe it could be done with just built-in exec without any wrappers, if so, it would be even better.

Here is what I usually do, when trying to download something, but it requires direct link, which is quite messy in youtube as far as I know.

async function download(url) {
    let res = await axios.get(url, { responseType: 'arraybuffer' })
        .catch((err) => console.log(`download(${url})|failed to download video...`))

    return Buffer.from(res.data, 'utf-8')
}
Bohdan
  • 53
  • 5

0 Answers0