pmrony commited on
Commit
fb7ddc7
·
verified ·
1 Parent(s): 07e9b3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -39
app.py CHANGED
@@ -3,39 +3,27 @@ import subprocess
3
  import uuid
4
  import httpx
5
  import asyncio
6
- from fastapi import FastAPI, BackgroundTasks, Body, Header, HTTPException
7
  from telegram import Bot
8
 
9
  app = FastAPI()
10
 
11
- # ১. সরাসরি কোডের ভেতরে সিক্রেট কী (বটের কোডের সাথে মিল থাকতে হবে)
12
  SECRET_KEY = "MySecretWatermarkKey123"
13
-
14
- # ২. বটের টোকেন Hugging Face Space এর 'Secrets' থেকে নিবে
15
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
16
 
17
- async def process_video_task(video_url: str, watermark: str, chat_id: int):
18
- """ব্যাকগ্রাউন্ডে ভিডিও ডাউনলোড, এডিট ও আপলোড করার ফাংশন"""
19
- if not BOT_TOKEN:
20
- print("Error: BOT_TOKEN not found in Secrets!")
21
- return
22
-
23
  bot = Bot(token=BOT_TOKEN)
24
  unique_id = uuid.uuid4()
25
  input_path = f"in_{unique_id}.mp4"
26
  output_path = f"out_{unique_id}.mp4"
27
 
28
  try:
29
- # টেলিগ্র কে রাসরি ভিডিও ডাউনলোড
30
- async with httpx.AsyncClient() as client:
31
- response = await client.get(video_url, timeout=600.0)
32
- if response.status_code == 200:
33
- with open(input_path, "wb") as f:
34
- f.write(response.content)
35
- else:
36
- return
37
 
38
- # FFmpeg ওয়াটারমার্ক ও মেটাা ফিক্স কমান্ড
39
  cmd = [
40
  "ffmpeg", "-i", input_path,
41
  "-vf", f"drawtext=text='{watermark}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
@@ -46,42 +34,36 @@ async def process_video_task(video_url: str, watermark: str, chat_id: int):
46
  ]
47
  subprocess.run(cmd, check=True)
48
 
49
- # িডিও আপলোড
50
- with open(output_path, "rb") as video_file:
51
  await bot.send_video(
52
  chat_id=chat_id,
53
- video=video_file,
54
  caption=f"✅ {watermark}",
55
  supports_streaming=True
56
  )
57
-
58
  except Exception as e:
59
- print(f"Background Process Error: {e}")
60
  finally:
61
- # টেম্পোরারি ফাইল পরিষ্কার
62
  if os.path.exists(input_path): os.remove(input_path)
63
  if os.path.exists(output_path): os.remove(output_path)
64
 
65
  @app.post("/watermark")
66
  async def trigger_watermark(
67
- background_tasks: BackgroundTasks,
68
- api_key: str = Header(None),
69
- data: dict = Body(...)
 
 
70
  ):
71
- # কোডের ভেতরকার কী-এর সাথে হেডার চেক
72
  if api_key != SECRET_KEY:
73
  raise HTTPException(status_code=403, detail="Unauthorized")
74
 
75
- # প্ি ব্যাকগ্রাউন্ডে পাঠিয়ে দেওয় হল
76
- background_tasks.add_task(
77
- process_video_task,
78
- data["video_url"],
79
- data["watermark_text"],
80
- data["chat_id"]
81
- )
82
- return {"status": "Task queued successfully"}
83
 
84
  if __name__ == "__main__":
85
  import uvicorn
86
- port = int(os.environ.get("PORT", 7860))
87
- uvicorn.run(app, host="0.0.0.0", port=port)
 
3
  import uuid
4
  import httpx
5
  import asyncio
6
+ from fastapi import FastAPI, BackgroundTasks, UploadFile, File, Form, Header, HTTPException
7
  from telegram import Bot
8
 
9
  app = FastAPI()
10
 
 
11
  SECRET_KEY = "MySecretWatermarkKey123"
 
 
12
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
13
 
14
+ async def process_video_task(file_bytes, watermark: str, chat_id: int, original_msg_id: int):
15
+ if not BOT_TOKEN: return
 
 
 
 
16
  bot = Bot(token=BOT_TOKEN)
17
  unique_id = uuid.uuid4()
18
  input_path = f"in_{unique_id}.mp4"
19
  output_path = f"out_{unique_id}.mp4"
20
 
21
  try:
22
+ # ইল রা
23
+ with open(input_path, "wb") as f:
24
+ f.write(file_bytes)
 
 
 
 
 
25
 
26
+ # FFmpeg িটি
27
  cmd = [
28
  "ffmpeg", "-i", input_path,
29
  "-vf", f"drawtext=text='{watermark}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
 
34
  ]
35
  subprocess.run(cmd, check=True)
36
 
37
+ # সরাসরি Hugging Face থেকে আপলোড
38
+ with open(output_path, "rb") as v:
39
  await bot.send_video(
40
  chat_id=chat_id,
41
+ video=v,
42
  caption=f"✅ {watermark}",
43
  supports_streaming=True
44
  )
 
45
  except Exception as e:
46
+ print(f"Error: {e}")
47
  finally:
 
48
  if os.path.exists(input_path): os.remove(input_path)
49
  if os.path.exists(output_path): os.remove(output_path)
50
 
51
  @app.post("/watermark")
52
  async def trigger_watermark(
53
+ background_tasks: BackgroundTasks,
54
+ file: UploadFile = File(...),
55
+ watermark_text: str = Form(...),
56
+ chat_id: int = Form(...),
57
+ api_key: str = Header(None)
58
  ):
 
59
  if api_key != SECRET_KEY:
60
  raise HTTPException(status_code=403, detail="Unauthorized")
61
 
62
+ # ফাইলটি মেমিতিয়ে ব্যাকগ্রাউন্ডে পাঠা
63
+ file_content = await file.read()
64
+ background_tasks.add_task(process_video_task, file_content, watermark_text, chat_id, 0)
65
+ return {"status": "Success"}
 
 
 
 
66
 
67
  if __name__ == "__main__":
68
  import uvicorn
69
+ uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))