Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,55 +9,32 @@ app = FastAPI()
|
|
| 9 |
API_KEY = "MySecretWatermarkKey123"
|
| 10 |
|
| 11 |
@app.post("/watermark")
|
| 12 |
-
async def add_watermark(
|
| 13 |
-
|
| 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 |
-
#
|
| 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 |
-
|
| 59 |
-
# Hugging Face ফাইলগুলো অটোমেটিক ডিলিট করে দেয়, তাই ভয়ের কিছু নেই।
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
-
|
| 63 |
-
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 9 |
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: raise HTTPException(status_code=403, detail="Unauthorized")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
output_path
|
| 29 |
]
|
| 30 |
|
| 31 |
try:
|
|
|
|
| 32 |
subprocess.run(cmd, check=True)
|
| 33 |
+
return FileResponse(output_path, media_type="video/mp4", filename="watermarked.mp4")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
raise HTTPException(status_code=500, detail=str(e))
|
| 36 |
+
finally:
|
| 37 |
+
if os.path.exists(input_path): os.remove(input_path)
|
|
|
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|