Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
# ২.
|
|
|
|
| 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.
|
| 43 |
-
"-c:v", "libx264", "-
|
| 44 |
-
"-
|
| 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 |
-
# ৩.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
|
|
|
|
|
| 57 |
caption=f"✅ {watermark_text}",
|
| 58 |
supports_streaming=True
|
| 59 |
)
|
| 60 |
-
return JSONResponse({"status": "success"
|
| 61 |
except Exception as e:
|
| 62 |
-
|
| 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 |
-
|
| 70 |
-
|
|
|
|
| 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
|