Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,36 +10,42 @@ API_KEY = "MySecretWatermarkKey123"
|
|
| 10 |
|
| 11 |
@app.post("/watermark")
|
| 12 |
async def add_watermark(file: UploadFile = File(...), watermark_text: str = "@YourGroup", api_key: str = Header(None)):
|
| 13 |
-
if api_key != API_KEY:
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
input_path = f"in_{uuid.uuid4()}{ext}"
|
| 17 |
-
output_path = f"out_{uuid.uuid4()}
|
| 18 |
|
| 19 |
-
with open(input_path, "wb") as buffer:
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"ffmpeg", "-i", input_path,
|
| 34 |
-
"-vf", f"drawtext=text='{watermark_text}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
|
| 35 |
-
output_path
|
| 36 |
-
]
|
| 37 |
|
| 38 |
try:
|
|
|
|
| 39 |
subprocess.run(cmd, check=True)
|
| 40 |
-
return FileResponse(output_path)
|
|
|
|
|
|
|
| 41 |
finally:
|
|
|
|
| 42 |
if os.path.exists(input_path): os.remove(input_path)
|
|
|
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
-
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@app.post("/watermark")
|
| 12 |
async def add_watermark(file: UploadFile = File(...), watermark_text: str = "@YourGroup", api_key: str = Header(None)):
|
| 13 |
+
if api_key != API_KEY:
|
| 14 |
+
return {"error": "Unauthorized"}
|
| 15 |
|
| 16 |
+
# ফাইলের এক্সটেনশন চেক
|
| 17 |
+
ext = os.path.splitext(file.filename)[1].lower()
|
| 18 |
input_path = f"in_{uuid.uuid4()}{ext}"
|
| 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 |
+
"-profile:v", "main",
|
| 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 |
+
return FileResponse(output_path, media_type="video/mp4")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return {"error": str(e)}
|
| 43 |
finally:
|
| 44 |
+
# ফাইল ডিলিট করা
|
| 45 |
if os.path.exists(input_path): os.remove(input_path)
|
| 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)
|