pmrony commited on
Commit
33b0588
·
verified ·
1 Parent(s): fb7ddc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -24
app.py CHANGED
@@ -3,27 +3,27 @@ import subprocess
3
  import uuid
4
  import httpx
5
  import asyncio
 
6
  from fastapi import FastAPI, BackgroundTasks, UploadFile, File, Form, Header, HTTPException
7
  from telegram import Bot
 
8
 
9
  app = FastAPI()
10
 
11
  SECRET_KEY = "MySecretWatermarkKey123"
12
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
13
 
14
- async def process_video_task(file_bytes, watermark: str, chat_id: int, original_msg_id: int):
15
  if not BOT_TOKEN: return
16
- bot = Bot(token=BOT_TOKEN)
17
- unique_id = uuid.uuid4()
18
- input_path = f"in_{unique_id}.mp4"
19
- output_path = f"out_{unique_id}.mp4"
 
 
20
 
21
  try:
22
- # ফাইল সেভ
23
- with open(input_path, "wb") as f:
24
- f.write(file_bytes)
25
-
26
- # FFmpeg এডিটিং
27
  cmd = [
28
  "ffmpeg", "-i", input_path,
29
  "-vf", f"drawtext=text='{watermark}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
@@ -32,19 +32,24 @@ async def process_video_task(file_bytes, watermark: str, chat_id: int, original_
32
  "-movflags", "+faststart+frag_keyframe+empty_moov",
33
  output_path
34
  ]
35
- subprocess.run(cmd, check=True)
 
 
 
36
 
37
- # সরাসরি Hugging Face থেকে আপলোড
38
- with open(output_path, "rb") as v:
39
- await bot.send_video(
40
- chat_id=chat_id,
41
- video=v,
42
- caption=f"✅ {watermark}",
43
- supports_streaming=True
44
- )
 
45
  except Exception as e:
46
- print(f"Error: {e}")
47
  finally:
 
48
  if os.path.exists(input_path): os.remove(input_path)
49
  if os.path.exists(output_path): os.remove(output_path)
50
 
@@ -59,10 +64,15 @@ async def trigger_watermark(
59
  if api_key != SECRET_KEY:
60
  raise HTTPException(status_code=403, detail="Unauthorized")
61
 
62
- # ফাইলটি মেমরিতে নিয়ে ব্যাকগ্রাউন্ডে পাঠানো
63
- file_content = await file.read()
64
- background_tasks.add_task(process_video_task, file_content, watermark_text, chat_id, 0)
65
- return {"status": "Success"}
 
 
 
 
 
66
 
67
  if __name__ == "__main__":
68
  import uvicorn
 
3
  import uuid
4
  import httpx
5
  import asyncio
6
+ import shutil
7
  from fastapi import FastAPI, BackgroundTasks, UploadFile, File, Form, Header, HTTPException
8
  from telegram import Bot
9
+ from telegram.request import HTTPXRequest
10
 
11
  app = FastAPI()
12
 
13
  SECRET_KEY = "MySecretWatermarkKey123"
14
  BOT_TOKEN = os.environ.get("BOT_TOKEN")
15
 
16
+ async def process_video_task(input_path: str, watermark: str, chat_id: int):
17
  if not BOT_TOKEN: return
18
+
19
+ # আপলোড টাইমআউট ফিক্স (১০ মিনিট সময় দেওয়া হয়েছে)
20
+ t_request = HTTPXRequest(connect_timeout=600, read_timeout=600, write_timeout=600)
21
+ bot = Bot(token=BOT_TOKEN, request=t_request)
22
+
23
+ output_path = f"out_{uuid.uuid4()}.mp4"
24
 
25
  try:
26
+ # FFmpegন্ড
 
 
 
 
27
  cmd = [
28
  "ffmpeg", "-i", input_path,
29
  "-vf", f"drawtext=text='{watermark}':x=w-tw-20:y=h-th-20:fontsize=30:fontcolor=white@0.6",
 
32
  "-movflags", "+faststart+frag_keyframe+empty_moov",
33
  output_path
34
  ]
35
+
36
+ # প্রসেসিং রান করা
37
+ process = await asyncio.create_subprocess_exec(*cmd)
38
+ await process.wait()
39
 
40
+ # সরাসরি ফাইল থেকে স্ট্রিম করে টেলিগ্রামে আপলোড
41
+ if os.path.exists(output_path):
42
+ with open(output_path, "rb") as v:
43
+ await bot.send_video(
44
+ chat_id=chat_id,
45
+ video=v,
46
+ caption=f"✅ {watermark}",
47
+ supports_streaming=True
48
+ )
49
  except Exception as e:
50
+ print(f"Detailed Error: {e}")
51
  finally:
52
+ # সব ফাইল পরিষ্কার করা
53
  if os.path.exists(input_path): os.remove(input_path)
54
  if os.path.exists(output_path): os.remove(output_path)
55
 
 
64
  if api_key != SECRET_KEY:
65
  raise HTTPException(status_code=403, detail="Unauthorized")
66
 
67
+ unique_id = uuid.uuid4()
68
+ input_path = f"in_{unique_id}.mp4"
69
+
70
+ # মেমরি বাঁচাতে ফাইলটি সরাসরি ডিস্কে রাইট করা
71
+ with open(input_path, "wb") as buffer:
72
+ shutil.copyfileobj(file.file, buffer)
73
+
74
+ background_tasks.add_task(process_video_task, input_path, watermark_text, chat_id)
75
+ return {"status": "Processing queued"}
76
 
77
  if __name__ == "__main__":
78
  import uvicorn