Youtube |top| Downloader Telegram Bot -
COPY bot.py .
# Validate URL if not ('youtube.com/watch' in url or 'youtu.be/' in url): await update.message.reply_text("❌ Please send a valid YouTube URL!") return
worker: python bot.py FROM python:3.9-slim RUN apt-get update && apt-get install -y ffmpeg youtube downloader telegram bot
Build and run:
YDL_OPTS_AUDIO = { 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }], 'outtmpl': f'{DOWNLOAD_DIR}/%(title)s.%(ext)s', 'quiet': True, 'no_warnings': True, } COPY bot
try: # Extract video info with yt_dlp.YoutubeDL(YDL_OPTS_INFO) as ydl: info = ydl.extract_info(url, download=False) title = info.get('title', 'Unknown Title') duration = info.get('duration', 0) # Format duration minutes, seconds = divmod(duration, 60) duration_str = f"{minutes}:{seconds:02d}" # Create keyboard for format selection keyboard = [ [ InlineKeyboardButton("🎥 Video (720p)", callback_data="video"), InlineKeyboardButton("🎵 Audio (MP3)", callback_data="audio") ], [InlineKeyboardButton("❌ Cancel", callback_data="cancel")] ] reply_markup = InlineKeyboardMarkup(keyboard) # Update message with video info info_text = ( f"✅ *Video found!*\n\n" f"📹 *Title:* {title}\n" f"⏱️ *Duration:* {duration_str}\n\n" f"Choose download format:" ) await status_msg.edit_text(info_text, parse_mode='Markdown', reply_markup=reply_markup) user_data[user_id]['title'] = title except Exception as e: logger.error(f"Error extracting info: {e}") await status_msg.edit_text("❌ Failed to analyze video. Please check the URL and try again.") async def button_callback(update: Update, context: ContextTypes.DEFAULT_TYPE): query = update.callback_query await query.answer()
if user_id not in user_data or 'url' not in user_data[user_id]: await query.edit_message_text("❌ Session expired. Please send the URL again.") return Please send the URL again
COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt