Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import uuid
|
| 4 |
-
import httpx
|
| 5 |
import asyncio
|
| 6 |
import shutil
|
| 7 |
-
from fastapi import FastAPI,
|
|
|
|
| 8 |
from telegram import Bot
|
| 9 |
from telegram.request import HTTPXRequest
|
| 10 |
|
|
@@ -13,67 +13,62 @@ app = FastAPI()
|
|
| 13 |
SECRET_KEY = "MySecretWatermarkKey123"
|
| 14 |
BOT_TOKEN = os.environ.get("BOT_TOKEN")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
cmd = [
|
| 28 |
"ffmpeg", "-i", input_path,
|
| 29 |
-
"-vf", f"drawtext=text='{
|
| 30 |
"-c:v", "libx264", "-profile:v", "main", "-pix_fmt", "yuv420p",
|
| 31 |
"-preset", "ultrafast", "-crf", "23", "-c:a", "copy",
|
| 32 |
"-movflags", "+faststart+frag_keyframe+empty_moov",
|
| 33 |
output_path
|
| 34 |
]
|
| 35 |
-
|
| 36 |
-
# প্রসেসিং রান করা
|
| 37 |
process = await asyncio.create_subprocess_exec(*cmd)
|
| 38 |
await process.wait()
|
| 39 |
|
| 40 |
-
#
|
| 41 |
if os.path.exists(output_path):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
-
|
| 51 |
finally:
|
| 52 |
-
# সব ফাইল পরিষ্কার করা
|
| 53 |
if os.path.exists(input_path): os.remove(input_path)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
@app.post("/watermark")
|
| 57 |
-
async def trigger_watermark(
|
| 58 |
-
background_tasks: BackgroundTasks,
|
| 59 |
-
file: UploadFile = File(...),
|
| 60 |
-
watermark_text: str = Form(...),
|
| 61 |
-
chat_id: int = Form(...),
|
| 62 |
-
api_key: str = Header(None)
|
| 63 |
-
):
|
| 64 |
-
if api_key != SECRET_KEY:
|
| 65 |
-
raise HTTPException(status_code=403, detail="Unauthorized")
|
| 66 |
-
|
| 67 |
-
unique_id = uuid.uuid4()
|
| 68 |
-
input_path = f"in_{unique_id}.mp4"
|
| 69 |
-
|
| 70 |
-
# মেমরি বাঁচাতে ফাইলটি সরাসরি ডিস্কে রাইট করা
|
| 71 |
-
with open(input_path, "wb") as buffer:
|
| 72 |
-
shutil.copyfileobj(file.file, buffer)
|
| 73 |
-
|
| 74 |
-
background_tasks.add_task(process_video_task, input_path, watermark_text, chat_id)
|
| 75 |
-
return {"status": "Processing queued"}
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
import uvicorn
|
| 79 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import uuid
|
|
|
|
| 4 |
import asyncio
|
| 5 |
import shutil
|
| 6 |
+
from fastapi import FastAPI, UploadFile, File, Form, Header, HTTPException
|
| 7 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 8 |
from telegram import Bot
|
| 9 |
from telegram.request import HTTPXRequest
|
| 10 |
|
|
|
|
| 13 |
SECRET_KEY = "MySecretWatermarkKey123"
|
| 14 |
BOT_TOKEN = os.environ.get("BOT_TOKEN")
|
| 15 |
|
| 16 |
+
# গ্লোবাল বট কানেকশন
|
| 17 |
+
t_request = HTTPXRequest(connect_timeout=30, read_timeout=60, write_timeout=600)
|
| 18 |
+
bot_instance = Bot(token=BOT_TOKEN, request=t_request) if BOT_TOKEN else None
|
| 19 |
+
|
| 20 |
+
@app.post("/watermark")
|
| 21 |
+
async def add_watermark(
|
| 22 |
+
file: UploadFile = File(...),
|
| 23 |
+
watermark_text: str = Form(...),
|
| 24 |
+
chat_id: int = Form(...),
|
| 25 |
+
api_key: str = Header(None)
|
| 26 |
+
):
|
| 27 |
+
if api_key != SECRET_KEY:
|
| 28 |
+
raise HTTPException(status_code=403, detail="Unauthorized")
|
| 29 |
|
| 30 |
+
unique_id = uuid.uuid4()
|
| 31 |
+
input_path = f"in_{unique_id}.mp4"
|
| 32 |
+
output_path = f"out_{unique_id}.mp4"
|
| 33 |
|
| 34 |
try:
|
| 35 |
+
# ১. ফাইলটি ডিস্কে সেভ করা
|
| 36 |
+
with open(input_path, "wb") as buffer:
|
| 37 |
+
shutil.copyfileobj(file.file, buffer)
|
| 38 |
+
|
| 39 |
+
# ২. FFmpeg দিয়ে প্রসেস করা
|
| 40 |
cmd = [
|
| 41 |
"ffmpeg", "-i", input_path,
|
| 42 |
+
"-vf", f"drawtext=text='{watermark_text}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
|
| 43 |
"-c:v", "libx264", "-profile:v", "main", "-pix_fmt", "yuv420p",
|
| 44 |
"-preset", "ultrafast", "-crf", "23", "-c:a", "copy",
|
| 45 |
"-movflags", "+faststart+frag_keyframe+empty_moov",
|
| 46 |
output_path
|
| 47 |
]
|
|
|
|
|
|
|
| 48 |
process = await asyncio.create_subprocess_exec(*cmd)
|
| 49 |
await process.wait()
|
| 50 |
|
| 51 |
+
# ৩. Hugging Face থেকে আপলোড করার চেষ্টা
|
| 52 |
if os.path.exists(output_path):
|
| 53 |
+
try:
|
| 54 |
+
with open(output_path, "rb") as v:
|
| 55 |
+
await bot_instance.send_video(
|
| 56 |
+
chat_id=chat_id, video=v,
|
| 57 |
+
caption=f"✅ {watermark_text}",
|
| 58 |
+
supports_streaming=True
|
| 59 |
+
)
|
| 60 |
+
return JSONResponse({"status": "success", "message": "Uploaded by HF"})
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"HF Upload Failed, sending back to Main Bot: {e}")
|
| 63 |
+
# আপলোড ফেইল হলে ভিডিওটি মেইন বটকে রিটার্ন করবে
|
| 64 |
+
return FileResponse(output_path, media_type="video/mp4", filename="watermarked.mp4")
|
| 65 |
+
|
| 66 |
except Exception as e:
|
| 67 |
+
return JSONResponse({"status": "error", "message": str(e)}, status_code=500)
|
| 68 |
finally:
|
|
|
|
| 69 |
if os.path.exists(input_path): os.remove(input_path)
|
| 70 |
+
# output_path রিমুভ করা হবে না যদি FileResponse দেওয়া হয় (FastAPI নিজে হ���যান্ডেল করবে)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
import uvicorn
|
| 74 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|