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 And

    Assistant:

  2. Assistant

    Creating a YouTube video downloader Telegram bot in Python involves using the telebot library for Telegram bot interactions and a library like pytube to download videos from YouTube. Below is a step-by-step guide, including the code you can use.

    Prerequisites

    1. Install Required Libraries: Make sure you have the necessary libraries installed. You can use pip to install them:

      pip install pyTelegramBotAPI pytube
      
    2. Create a Telegram Bot: You need to create a bot on Telegram and get the API token. Follow these steps:

      • Go to Telegram and search for the BotFather.
      • Use the /newbot command and follow the prompts to create a new bot.
      • After creation, you'll receive a token. Save this token as you'll need it in your code.

    Bot Code

    Here is a simple script to create a YouTube video downloader Telegram bot:

    import telebot
    from pytube import YouTube
    import os
    
    API_TOKEN = 'YOUR_API_TOKEN_HERE'
    bot = telebot.TeleBot(API_TOKEN)
    
    @bot.message_handler(func=lambda message: True)
    def download_video(message):
        url = message.text
        try:
            yt = YouTube(url)
            video_stream = yt.streams.filter(file_extension='mp4', progressive=True).first()
            if video_stream:
                video_file = video_stream.download(filename='video.mp4')
                with open(video_file, 'rb') as video:
                    bot.send_video(chat_id=message.chat.id, video=video, caption=f'Downloaded: {yt.title}')
                os.remove(video_file)  # Clean up the downloaded file
            else:
                bot.reply_to(message, "No downloadable video streams found.")
        except Exception as e:
            bot.reply_to(message, f"Error: {e}")
    
    if __name__ == "__main__":
        print("Bot is polling...")
        bot.polling()
    

    Explanation of the Code

    1. Import Libraries: The essential libraries, telebot for Telegram interaction and pytube for downloading YouTube videos.

    2. API Token: Replace 'YOUR_API_TOKEN_HERE' with your actual Telegram bot token.

    3. Message Handler: The download_video function listens for messages (YouTube links) sent by users.

    4. YouTube Download Logic:

      • The input URL is processed using pytube.
      • The script looks for the first available .mp4 stream with the progressive attribute (which means it includes both audio and video).
      • The video is downloaded and sent back to the user.
    5. Error Handling: Basic error handling is included to inform the user if something goes wrong.

    6. Polling: bot.polling() is used to keep the bot running and listening for messages.

    Running the Bot

    1. Save this script in a Python file, e.g., youtube_downloader_bot.py.

    2. Run the script:

      python youtube_downloader_bot.py
      
    3. Open your Telegram, find your bot by its username, and start sending YouTube links to test the downloader.

    Important Note

    • Ensure you comply with YouTube's terms of service when downloading videos. This bot is for personal use and educational purposes only.
    • Consider adding more error handling and features, like progress updates or limiting the video resolution based on user input.

    Feel free to ask if you have any questions or need further assistance!