pmrony commited on
Commit
e438e60
·
verified ·
1 Parent(s): a00f1f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -21
app.py CHANGED
@@ -1,23 +1,24 @@
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"
@@ -26,31 +27,20 @@ async def add_watermark(
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()
 
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
+ import threading
8
 
9
  app = FastAPI()
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
+ # API Key চেক
20
  if api_key != API_KEY:
21
+ return {"error": "Unauthorized"}
22
 
23
  input_path = f"input_{uuid.uuid4()}.mp4"
24
  output_path = f"output_{uuid.uuid4()}.mp4"
 
27
  with open(input_path, "wb") as buffer:
28
  buffer.write(await file.read())
29
 
30
+ # FFmpeg দিয়ে ওয়াটারমার্ক করা
 
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", "-preset", "ultrafast", "-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")
 
 
41
  finally:
 
42
  if os.path.exists(input_path): os.remove(input_path)
43
+ # output_path এরর এড়াতে এখানে রিমুভ করব না, FastAPI এটা হ্যান্ডেল করবে
 
 
 
 
44
 
45
  if __name__ == "__main__":
46
+ uvicorn.run(app, host="0.0.0.0", port=7860)