-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
293 lines (230 loc) · 9.13 KB
/
Copy pathbot.py
File metadata and controls
293 lines (230 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import sys
import logging
import asyncio
import re
import shutil
from pathlib import Path
import uuid
from aiogram import Bot, Dispatcher, F, Router
from aiogram.types import Message, FSInputFile, InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.filters import Command
from aiohttp import web
import yt_dlp
from dotenv import load_dotenv
load_dotenv()
BOT_TOKEN = os.getenv("BOT_TOKEN")
BASE_DIR = Path(__file__).parent
DATA_DIR = BASE_DIR / "bot_data"
DATA_DIR.mkdir(exist_ok=True)
MAX_TELEGRAM_DOWNLOAD_MB = 20
MAX_VIDEO_DURATION_SECONDS = 1800
PORT = int(os.getenv("PORT", 10000))
def safe_rmtree(path: Path, retries: int = 5, delay: float = 0.5):
"""Delete a directory, retrying on transient Windows file locks."""
import time
for attempt in range(retries):
try:
shutil.rmtree(path)
return
except PermissionError:
if attempt == retries - 1:
logger.warning(
f"Could not fully clean up {path} after {retries} attempts."
)
return
time.sleep(delay)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler(DATA_DIR / "bot.log", encoding="utf-8"),
],
)
logger = logging.getLogger("VocalRemoverBot")
YOUTUBE_REGEX = re.compile(
r'(?:https?://)?(?:www\.)?(?:youtube\.com|youtu\.be|youtube-nocookie\.com)'
r'/(?:watch\?v=|embed/|v/|shorts/)?[a-zA-Z0-9_-]{11}\S*'
)
class YouTubeService:
@staticmethod
async def download_audio(url: str, target_dir: Path) -> Path:
def _sync_download():
ydl_opts = {
"format": "bestaudio/best",
"outtmpl": str(target_dir / "input_track.%(ext)s"),
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "192",
}
],
"quiet": True,
"no_warnings": True,
"noplaylist": True,
"socket_timeout": 30,
"match_filter": yt_dlp.utils.match_filter_func(
f"duration < {MAX_VIDEO_DURATION_SECONDS}"
),
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
return target_dir / "input_track.mp3"
return await asyncio.to_thread(_sync_download)
class DemucsSeparatorService:
def __init__(self, model_name: str = "htdemucs"):
self.model_name = model_name
async def separate_vocals(self, input_file_path: Path, output_base_dir: Path) -> Path:
logger.info(f"Starting separation for: {input_file_path.name}")
cmd = [
"demucs",
"--two-stems",
"vocals",
"-n",
self.model_name,
"-o",
str(output_base_dir),
str(input_file_path),
]
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
_, stderr = await process.communicate()
if process.returncode != 0:
error_msg = stderr.decode(errors="ignore")
logger.error(f"Demucs failed: {error_msg}")
raise RuntimeError("Vocal separation failed.")
filename_stem = input_file_path.stem
expected_vocal_path = (
output_base_dir / self.model_name / filename_stem / "vocals.wav"
)
if not expected_vocal_path.exists():
raise FileNotFoundError("Separated vocal file not found in output path.")
return expected_vocal_path
router = Router()
separator_service = DemucsSeparatorService()
@router.message(Command("start"))
async def cmd_start(message: Message):
welcome_text = (
"Welcome to the Vocal Remover Bot\n\n"
"This bot separates the singer’s voice from the music using AI.\n\n"
"📥 How to use:\n"
"• Send a YouTube video link directly.\n"
"• Or upload an audio file in MP3 format.\n\n"
"⚙️ I’ll handle the rest and send you a clean audio track without music."
)
keyboard = InlineKeyboardMarkup(
inline_keyboard=[
[
InlineKeyboardButton(
text="Bot Developer 👨💻",
url="https://t.me/ramidevx",
)
]
]
)
await message.reply(welcome_text, reply_markup=keyboard)
async def process_audio_pipeline(
message: Message,
bot: Bot,
source_type: str,
source_data: str,
file_ext: str = "mp3",
):
request_id = str(uuid.uuid4())
session_dir = DATA_DIR / request_id
session_dir.mkdir(exist_ok=True)
status_message = await message.reply("⏳ Request received, starting processing...")
try:
if source_type == "youtube":
await status_message.edit_text("📥 Downloading audio from YouTube...")
input_file = await YouTubeService.download_audio(source_data, session_dir)
elif source_type == "file":
await status_message.edit_text("📥 Downloading your audio file...")
file_info = await bot.get_file(source_data)
input_file = session_dir / f"input_track.{file_ext}"
await bot.download_file(file_info.file_path, destination=input_file)
else:
raise ValueError("Unsupported source type.")
await status_message.edit_text(
"🤖 Separating vocals from music (this can take a minute)..."
)
vocals_wav = await separator_service.separate_vocals(input_file, session_dir)
await status_message.edit_text("📤 Done! Sending your separated audio...")
output_filename = f"Vocals_{message.from_user.id}_{request_id[:8]}.wav"
vocal_audio_file = FSInputFile(path=vocals_wav, filename=output_filename)
await message.reply_audio(
audio=vocal_audio_file,
caption="🎤 Vocals separated successfully. Enjoy!",
)
await status_message.delete()
except Exception as e:
logger.error(f"Error processing request {request_id}: {e}")
await status_message.edit_text(
"❌ Something went wrong while processing your file. "
"Please check the file/link and try again."
)
finally:
if session_dir.exists():
safe_rmtree(session_dir)
logger.info(f"Cleaned up session directory: {request_id}")
@router.message(F.text.regexp(YOUTUBE_REGEX))
async def handle_youtube_messages(message: Message, bot: Bot):
match = YOUTUBE_REGEX.search(message.text)
url = match.group(0) if match else message.text
await process_audio_pipeline(message, bot, source_type="youtube", source_data=url)
@router.message(F.audio)
async def handle_audio_file_messages(message: Message, bot: Bot):
file_size = message.audio.file_size or 0
if file_size > MAX_TELEGRAM_DOWNLOAD_MB * 1024 * 1024:
await message.reply(
f"⚠️ File exceeds the {MAX_TELEGRAM_DOWNLOAD_MB}MB limit bots can download "
"via the Telegram API. Please send a smaller file, or set up a local Bot API server."
)
return
ext = "mp3"
if message.audio.file_name and "." in message.audio.file_name:
candidate = message.audio.file_name.rsplit(".", 1)[-1].lower()
if re.fullmatch(r"[a-zA-Z0-9]{1,5}", candidate):
ext = candidate
await process_audio_pipeline(
message,
bot,
source_type="file",
source_data=message.audio.file_id,
file_ext=ext,
)
async def start_health_server():
"""Minimal HTTP server so Render's port scan sees something open.
Doesn't do anything else — the bot itself works via polling, not HTTP."""
async def health_check(request):
return web.Response(text="Bot is alive and polling.")
app = web.Application()
app.router.add_get("/", health_check)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, host="0.0.0.0", port=PORT)
await site.start()
logger.info(f"Health-check server listening on 0.0.0.0:{PORT}")
async def main():
if not BOT_TOKEN:
logger.critical("BOT_TOKEN environment variable is not set.")
sys.exit(1)
bot = Bot(token=BOT_TOKEN)
dp = Dispatcher()
dp.include_router(router)
logger.info("Bot is running and polling for updates...")
await bot.delete_webhook(drop_pending_updates=True)
await asyncio.gather(
start_health_server(),
dp.start_polling(bot),
)
if __name__ == "__main__":
try:
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
logger.info("Bot stopped.")