pmrony commited on
Commit
d2e9588
·
verified ·
1 Parent(s): 06fca8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -22
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: 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)))
 
 
 
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)