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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
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
- input_path = f"in_{uuid.uuid4()}.mp4"
16
- output_path = f"out_{uuid.uuid4()}.mp4"
 
17
 
18
  with open(input_path, "wb") as buffer: buffer.write(await file.read())
19
 
20
- cmd = [
21
- "ffmpeg", "-i", input_path,
22
- "-vf", f"drawtext=text='{watermark_text}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
23
- "-c:v", "libx264", "-profile:v", "main", "-pix_fmt", "yuv420p",
24
- "-preset", "ultrafast", "-c:a", "copy",
25
- output_path
26
- ]
 
 
 
 
 
 
 
 
 
27
 
28
  try:
29
  subprocess.run(cmd, check=True)
30
- return FileResponse(output_path, media_type="video/mp4")
31
  finally:
32
  if os.path.exists(input_path): os.remove(input_path)
33
 
34
  if __name__ == "__main__":
35
- # Hugging Face এ পোর্ট ৭৯৬০ বা এনভায়রনমেন্ট পোর্ট ব্যবহার করা ভালো
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)))