pmrony commited on
Commit
3ecda7f
·
verified ·
1 Parent(s): b39d6ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -13,7 +13,7 @@ app = FastAPI()
13
  SECRET_KEY = "MySecretWatermarkKey123"
14
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
15
 
16
- # গ্লোব বট কানেকশন
17
  t_request = HTTPXRequest(connect_timeout=30, read_timeout=60, write_timeout=600)
18
  bot_instance = Bot(token=BOT_TOKEN, request=t_request) if BOT_TOKEN else None
19
 
@@ -30,44 +30,52 @@ async def add_watermark(
30
  unique_id = uuid.uuid4()
31
  input_path = f"in_{unique_id}.mp4"
32
  output_path = f"out_{unique_id}.mp4"
 
33
 
34
  try:
35
- # ১. ফাইলটি ডি্ক সে করা
36
  with open(input_path, "wb") as buffer:
37
  shutil.copyfileobj(file.file, buffer)
38
 
39
- # ২. FFmpeg দিয়ে সেস কর
 
40
  cmd = [
41
  "ffmpeg", "-i", input_path,
42
- "-vf", f"drawtext=text='{watermark_text}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
43
- "-c:v", "libx264", "-profile:v", "main", "-pix_fmt", "yuv420p",
44
- "-preset", "ultrafast", "-crf", "23", "-c:a", "copy",
45
- "-movflags", "+faststart+frag_keyframe+empty_moov",
46
  output_path
47
  ]
48
  process = await asyncio.create_subprocess_exec(*cmd)
49
  await process.wait()
50
 
51
- # ৩. Hugging Face থেকে আপোড করার টা
 
 
 
 
 
52
  if os.path.exists(output_path):
53
  try:
54
- with open(output_path, "rb") as v:
55
  await bot_instance.send_video(
56
- chat_id=chat_id, video=v,
 
 
57
  caption=f"✅ {watermark_text}",
58
  supports_streaming=True
59
  )
60
- return JSONResponse({"status": "success", "message": "Uploaded by HF"})
61
  except Exception as e:
62
- print(f"HF Upload Failed, sending back to Main Bot: {e}")
63
- # আপলোড ফেইল হলে ভিডিওটি মেইন বটকে রিটার্ন করবে
64
- return FileResponse(output_path, media_type="video/mp4", filename="watermarked.mp4")
65
 
66
  except Exception as e:
67
  return JSONResponse({"status": "error", "message": str(e)}, status_code=500)
68
  finally:
69
- if os.path.exists(input_path): os.remove(input_path)
70
- # output_path রিমুভ করা হবে না যদি FileResponse দেওয়া হয় (FastAPI নিজে হ্যান্ডেল করবে)
 
71
 
72
  if __name__ == "__main__":
73
  import uvicorn
 
13
  SECRET_KEY = "MySecretWatermarkKey123"
14
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
15
 
16
+ # স্পিড কানেকশন পুল
17
  t_request = HTTPXRequest(connect_timeout=30, read_timeout=60, write_timeout=600)
18
  bot_instance = Bot(token=BOT_TOKEN, request=t_request) if BOT_TOKEN else None
19
 
 
30
  unique_id = uuid.uuid4()
31
  input_path = f"in_{unique_id}.mp4"
32
  output_path = f"out_{unique_id}.mp4"
33
+ thumb_path = f"thumb_{unique_id}.jpg"
34
 
35
  try:
36
+ # ১. ফাইল সেভ
37
  with open(input_path, "wb") as buffer:
38
  shutil.copyfileobj(file.file, buffer)
39
 
40
+ # ২. সুস্ট FFmpeg (Speed optimized)
41
+ # crf 28 এবং preset superfast ভিডিও এডিটিং দ্রুত করবে
42
  cmd = [
43
  "ffmpeg", "-i", input_path,
44
+ "-vf", f"drawtext=text='{watermark_text}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.5",
45
+ "-c:v", "libx264", "-preset", "superfast", "-crf", "28", "-c:a", "copy",
46
+ "-movflags", "+faststart",
 
47
  output_path
48
  ]
49
  process = await asyncio.create_subprocess_exec(*cmd)
50
  await process.wait()
51
 
52
+ # ৩. থাম্বনজেনারেট করা (ভিডিও২য় সকেনড থেকে)
53
+ thumb_cmd = ["ffmpeg", "-i", output_path, "-ss", "00:00:02", "-vframes", "1", "-y", thumb_path]
54
+ thumb_proc = await asyncio.create_subprocess_exec(*thumb_cmd)
55
+ await thumb_proc.wait()
56
+
57
+ # ৪. আপলোড করার চেষ্টা (থাম্বনেইল সহ)
58
  if os.path.exists(output_path):
59
  try:
60
+ with open(output_path, "rb") as v, open(thumb_path, "rb") as t:
61
  await bot_instance.send_video(
62
+ chat_id=chat_id,
63
+ video=v,
64
+ thumbnail=t, # থাম্বনেইল সেট করা হলো
65
  caption=f"✅ {watermark_text}",
66
  supports_streaming=True
67
  )
68
+ return JSONResponse({"status": "success"})
69
  except Exception as e:
70
+ # আপলোড ফেইল হলে মেইন বটকে ভিডিও এবং থাম্বনেইল ফেরত পাঠানো (Rare case)
71
+ return FileResponse(output_path, media_type="video/mp4")
 
72
 
73
  except Exception as e:
74
  return JSONResponse({"status": "error", "message": str(e)}, status_code=500)
75
  finally:
76
+ # ক্লিনআপ
77
+ for path in [input_path, thumb_path]:
78
+ if os.path.exists(path): os.remove(path)
79
 
80
  if __name__ == "__main__":
81
  import uvicorn