Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,87 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import uuid
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
import
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
-
API_KEY = "MySecretWatermarkKey123"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
unique_id = uuid.uuid4()
|
| 16 |
input_path = f"in_{unique_id}.mp4"
|
| 17 |
output_path = f"out_{unique_id}.mp4"
|
| 18 |
|
| 19 |
-
with open(input_path, "wb") as buffer: buffer.write(await file.read())
|
| 20 |
-
|
| 21 |
-
# নতুন কমান্ড: এতে থাম্বনেইল সরাসরি ভিডিও ফাইলের ভেতরে এম্বেড হয়ে যাবে
|
| 22 |
-
cmd = [
|
| 23 |
-
"ffmpeg", "-i", input_path,
|
| 24 |
-
"-vf", f"drawtext=text='{watermark_text}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
|
| 25 |
-
"-c:v", "libx264", "-profile:v", "main", "-pix_fmt", "yuv420p",
|
| 26 |
-
"-preset", "ultrafast", "-crf", "23", "-c:a", "copy",
|
| 27 |
-
"-movflags", "+faststart+frag_keyframe+empty_moov",
|
| 28 |
-
"-metadata:s:v:0", "handler_name=VideoHandler",
|
| 29 |
-
output_path
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
subprocess.run(cmd, check=True)
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
-
|
| 37 |
finally:
|
|
|
|
| 38 |
if os.path.exists(input_path): os.remove(input_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
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",
|
| 42 |
+
"-c:v", "libx264", "-profile:v", "main", "-pix_fmt", "yuv420p",
|
| 43 |
+
"-preset", "ultrafast", "-crf", "23", "-c:a", "copy",
|
| 44 |
+
"-movflags", "+faststart+frag_keyframe+empty_moov",
|
| 45 |
+
output_path
|
| 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)
|