Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

Hello! How can i help you today?

  1. User

    Assistant: Hello! How can i help you today? User: Make a youtube video downloader telegram bot in python Downloader the video and send in .mp4 format yo user who send the link Use telebot Assistant:

  2. Assistant

    Creating a YouTube video downloader Telegram bot using the telebot library in Python can be a fun project! Below is a simple example of how you can create such a bot. This example assumes you have access to the pytube library for downloading videos from YouTube.

    Requirements:

    1. Python installed on your system.
    2. pytelegrambotapi (telebot) library: You can install it using pip install pytelegrambotapi.
    3. pytube library: Install this using pip install pytube.
    4. Your bot token from the BotFather on Telegram.

    Here is the bot code:

    import telebot
    from pytube import YouTube
    import os
    
    # Initialize the bot with your token
    TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'
    bot = telebot.TeleBot(TOKEN)
    
    @bot.message_handler(commands=['start'])
    def send_welcome(message):
        bot.send_message(message.chat.id, "Welcome! Send me a YouTube link and I'll download the video for you.")
    
    @bot.message_handler(func=lambda message: True)
    def download_video(message):
        url = message.text
    
        try:
            # Download the video
            yt = YouTube(url)
            video = yt.streams.get_highest_resolution()
            video.download(output_path='downloads', filename='video.mp4')
            
            # Send the video back to the user
            with open('downloads/video.mp4', 'rb') as video_file:
                bot.send_video(message.chat.id, video_file)
    
            # Clean up the downloaded file
            os.remove('downloads/video.mp4')
    
        except Exception as e:
            bot.send_message(message.chat.id, f"An error occurred: {e}")
    
    # Create the downloads folder if it does not exist
    if not os.path.exists('downloads'):
        os.makedirs('downloads')
    
    # Start the bot
    bot.polling()
    

    Instructions:

    1. Replace YOUR_TELEGRAM_BOT_TOKEN with your actual bot token.
    2. Make sure you have the necessary libraries installed. You can do this by running:
      pip install pytelegrambotapi pytube
      
    3. Save your script as youtube_downloader_bot.py.
    4. Run your script:
      python youtube_downloader_bot.py
      

    Usage:

    • Once you start the bot in Telegram and send a valid YouTube link, the bot will download the video and send it back to you in .mp4 format.

    Note:

    • Be aware of copyright issues. Ensure that the content being downloaded and shared complies with YouTube's Terms of Service.
    • The bot may have limitations on the size of files it can send, depending on Telegram's restrictions.
    • This is just a basic implementation. You can enhance it by adding error handling, logging, or allowing users to choose the video quality.