Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import uuid
|
| 4 |
-
from fastapi import FastAPI, UploadFile, File, Header
|
| 5 |
from fastapi.responses import FileResponse
|
| 6 |
import uvicorn
|
| 7 |
|
|
@@ -9,43 +9,55 @@ app = FastAPI()
|
|
| 9 |
API_KEY = "MySecretWatermarkKey123"
|
| 10 |
|
| 11 |
@app.post("/watermark")
|
| 12 |
-
async def add_watermark(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if api_key != API_KEY:
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
output_path = f"out_{uuid.uuid4()}.mp4" # সব আউটপুট mp4 তে নেওয়া ভালো
|
| 20 |
|
|
|
|
| 21 |
with open(input_path, "wb") as buffer:
|
| 22 |
buffer.write(await file.read())
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
| 25 |
cmd = [
|
| 26 |
"ffmpeg", "-i", input_path,
|
| 27 |
"-vf", f"drawtext=text='{watermark_text}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
|
| 28 |
-
"-c:v", "libx264",
|
| 29 |
-
"-
|
| 30 |
-
"-pix_fmt", "yuv420p",
|
| 31 |
-
"-preset", "ultrafast",
|
| 32 |
-
"-c:a", "copy",
|
| 33 |
"-movflags", "+faststart+frag_keyframe+empty_moov",
|
| 34 |
output_path
|
| 35 |
]
|
| 36 |
|
| 37 |
try:
|
| 38 |
-
# প্রসেস র
|
| 39 |
subprocess.run(cmd, check=True)
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# output_path এরর এড়াতে এখানে রিমুভ করব না, FastAPI এটা হ্যান্ডেল করবে
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
-
# Hugging Face পোর্ট চেক
|
| 50 |
port = int(os.environ.get("PORT", 7860))
|
| 51 |
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import uuid
|
| 4 |
+
from fastapi import FastAPI, UploadFile, File, Header, HTTPException
|
| 5 |
from fastapi.responses import FileResponse
|
| 6 |
import uvicorn
|
| 7 |
|
|
|
|
| 9 |
API_KEY = "MySecretWatermarkKey123"
|
| 10 |
|
| 11 |
@app.post("/watermark")
|
| 12 |
+
async def add_watermark(
|
| 13 |
+
file: UploadFile = File(...),
|
| 14 |
+
watermark_text: str = "@YourGroup",
|
| 15 |
+
api_key: str = Header(None)
|
| 16 |
+
):
|
| 17 |
if api_key != API_KEY:
|
| 18 |
+
raise HTTPException(status_code=403, detail="Unauthorized")
|
| 19 |
|
| 20 |
+
unique_id = uuid.uuid4()
|
| 21 |
+
input_path = f"in_{unique_id}.mp4"
|
| 22 |
+
output_path = f"out_{unique_id}.mp4"
|
|
|
|
| 23 |
|
| 24 |
+
# ভিডিও ফাইলটি সেভ করা
|
| 25 |
with open(input_path, "wb") as buffer:
|
| 26 |
buffer.write(await file.read())
|
| 27 |
|
| 28 |
+
# FFmpeg কমান্ড:
|
| 29 |
+
# -movflags +faststart থাম্বনেইল এবং প্লে-ব্যাকের জন্য সবথেকে জরুরি
|
| 30 |
+
# -pix_fmt yuv420p ভিডিও প্লেয়ারে চালানোর জন্য স্ট্যান্ডার্ড
|
| 31 |
cmd = [
|
| 32 |
"ffmpeg", "-i", input_path,
|
| 33 |
"-vf", f"drawtext=text='{watermark_text}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
|
| 34 |
+
"-c:v", "libx264", "-profile:v", "main", "-pix_fmt", "yuv420p",
|
| 35 |
+
"-preset", "ultrafast", "-c:a", "copy",
|
|
|
|
|
|
|
|
|
|
| 36 |
"-movflags", "+faststart+frag_keyframe+empty_moov",
|
| 37 |
output_path
|
| 38 |
]
|
| 39 |
|
| 40 |
try:
|
| 41 |
+
# প্রসেসিং শুরু
|
| 42 |
subprocess.run(cmd, check=True)
|
| 43 |
+
|
| 44 |
+
# ফাইলটি চেক করে ফেরত পাঠানো
|
| 45 |
+
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 46 |
+
return FileResponse(
|
| 47 |
+
output_path,
|
| 48 |
+
media_type="video/mp4",
|
| 49 |
+
filename="watermarked.mp4",
|
| 50 |
+
headers={"Content-Disposition": "attachment; filename=watermarked.mp4"}
|
| 51 |
+
)
|
| 52 |
+
else:
|
| 53 |
+
raise HTTPException(status_code=500, detail="Processing failed")
|
| 54 |
+
|
| 55 |
except Exception as e:
|
| 56 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 57 |
+
|
| 58 |
+
# ফাইল ডিলিট করার লজিক এখানে দেব না কারণ FileResponse এর জন্য ফাইলটি ওপেন থাকতে হয়
|
| 59 |
+
# Hugging Face ফাইলগুলো অটোমেটিক ডিলিট করে দেয়, তাই ভয়ের কিছু নেই।
|
|
|
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
|
|
|
| 62 |
port = int(os.environ.get("PORT", 7860))
|
| 63 |
uvicorn.run(app, host="0.0.0.0", port=port)
|