Spaces:
Sleeping
Sleeping
Create app py
Browse files
app py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# একটি সিক্রেট কী (এটি আপনার মেইন বটেও ব্যবহার করবেন)
|
| 11 |
+
API_KEY = "MySecretWatermarkKey123"
|
| 12 |
+
|
| 13 |
+
@app.post("/watermark")
|
| 14 |
+
async def add_watermark(
|
| 15 |
+
file: UploadFile = File(...),
|
| 16 |
+
watermark_text: str = "@YourGroupUsername", # ডিফল্ট টেক্সট
|
| 17 |
+
api_key: str = Header(None)
|
| 18 |
+
):
|
| 19 |
+
if api_key != API_KEY:
|
| 20 |
+
raise HTTPException(status_code=403, detail="Unauthorized")
|
| 21 |
+
|
| 22 |
+
input_path = f"input_{uuid.uuid4()}.mp4"
|
| 23 |
+
output_path = f"output_{uuid.uuid4()}.mp4"
|
| 24 |
+
|
| 25 |
+
# ভিডিও সেভ করা
|
| 26 |
+
with open(input_path, "wb") as buffer:
|
| 27 |
+
buffer.write(await file.read())
|
| 28 |
+
|
| 29 |
+
# FFmpeg কমান্ড: নিচে ডান দিকে সাদা টেক্সট বসাবে
|
| 30 |
+
# আপনি চাইলে পজিশন বা কালার চেঞ্জ করতে পারেন
|
| 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:shadowcolor=black:shadowx=2:shadowy=2",
|
| 34 |
+
"-c:v", "libx264", "-preset", "ultrafast", "-crf", "28", "-c:a", "copy",
|
| 35 |
+
output_path
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
subprocess.run(cmd, check=True)
|
| 40 |
+
return FileResponse(output_path, media_type="video/mp4", filename="watermarked.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 |
+
|
| 47 |
+
# Hugging Face-এর জন্য একটি ডামি ইন্টারফেস
|
| 48 |
+
import gradio as gr
|
| 49 |
+
def dummy(x): return x
|
| 50 |
+
demo = gr.Interface(fn=dummy, inputs="text", outputs="text")
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
# FastAPI এবং Gradio একসাথে চালানো
|
| 54 |
+
import threading
|
| 55 |
+
threading.Thread(target=lambda: uvicorn.run(app, host="0.0.0.0", port=8000), daemon=True).start()
|
| 56 |
+
demo.launch()
|