-1

How to make a telegram bot that converts simple youtube link (something like https://www.youtube.com/watch?v=v3F3v8yNucc) into .mp3?

Please suggest something with minimum efford

Scott
  • 4,974
  • 6
  • 35
  • 62

1 Answers1

-2

This will do the minimum job:

import os
import telebot
import youtube_dl

# get YOUR_API_KEY from @BotFather
bot = telebot.TeleBot("YOUR_API_KEY")


@bot.message_handler(commands=['start'])
def shoot(message):
  bot.send_message(message.chat.id, "Gimme YouTube link")


@bot.message_handler()
def run(message):
  if "https://www.youtube.com" not in message.text:
    print("This is not YouTube link!")
    return

  bot.send_message(message.chat.id, "Please wait...")
    
  video_info = youtube_dl.YoutubeDL().extract_info(
      url = message.text, download=False
  )
  filename = f"{video_info['title']}.mp3"
  options={
      'format':'bestaudio/best',
      'keepvideo':False,
      'outtmpl':filename,
  }

  with youtube_dl.YoutubeDL(options) as ydl:
      ydl.download([video_info['webpage_url']])

  print("Download complete... {}".format(filename))
  bot.send_audio(message.chat.id, audio=open(filename, 'rb'))

bot.polling()

Thise code will download mp3 music to your server and sends it to the user in telegram, so you might wanna delete it after you send it, or need a larger server? Up to you.

Scott
  • 4,974
  • 6
  • 35
  • 62