Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,26 +12,34 @@ API_KEY = "MySecretWatermarkKey123"
|
|
| 12 |
async def add_watermark(file: UploadFile = File(...), watermark_text: str = "@YourGroup", api_key: str = Header(None)):
|
| 13 |
if api_key != API_KEY: return {"error": "Unauthorized"}
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
with open(input_path, "wb") as buffer: buffer.write(await file.read())
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
try:
|
| 29 |
subprocess.run(cmd, check=True)
|
| 30 |
-
return FileResponse(output_path
|
| 31 |
finally:
|
| 32 |
if os.path.exists(input_path): os.remove(input_path)
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
| 35 |
-
|
| 36 |
-
port = int(os.environ.get("PORT", 7860))
|
| 37 |
-
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 12 |
async def add_watermark(file: UploadFile = File(...), watermark_text: str = "@YourGroup", api_key: str = Header(None)):
|
| 13 |
if api_key != API_KEY: return {"error": "Unauthorized"}
|
| 14 |
|
| 15 |
+
ext = os.path.splitext(file.filename)[1].lower() # ফাইল ফরম্যাট চেক
|
| 16 |
+
input_path = f"in_{uuid.uuid4()}{ext}"
|
| 17 |
+
output_path = f"out_{uuid.uuid4()}{ext}"
|
| 18 |
|
| 19 |
with open(input_path, "wb") as buffer: buffer.write(await file.read())
|
| 20 |
|
| 21 |
+
if ext in [".mp4", ".mov", ".mkv"]:
|
| 22 |
+
# ভিডিওর জন্য কমান্ড (থাম্বনেইল ঠিক রাখতে -movflags +faststart)
|
| 23 |
+
cmd = [
|
| 24 |
+
"ffmpeg", "-i", input_path,
|
| 25 |
+
"-vf", f"drawtext=text='{watermark_text}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
|
| 26 |
+
"-c:v", "libx264", "-profile:v", "main", "-pix_fmt", "yuv420p",
|
| 27 |
+
"-preset", "ultrafast", "-c:a", "copy", "-movflags", "+faststart",
|
| 28 |
+
output_path
|
| 29 |
+
]
|
| 30 |
+
else:
|
| 31 |
+
# ছবির জন্য কমান্ড
|
| 32 |
+
cmd = [
|
| 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 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
|
|
|