Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1094,12 +1094,18 @@ async def process_single_video(task, redis_client):
|
|
| 1094 |
wm_text = task['watermark_text']
|
| 1095 |
task_id = f"{owner_id}_{message_id}"
|
| 1096 |
|
| 1097 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1098 |
|
| 1099 |
-
|
| 1100 |
-
thumb_path = f"thumb_{task_id}.jpg" # থাম্বনেইল এর ভেরিয়েবল
|
| 1101 |
|
| 1102 |
try:
|
|
|
|
| 1103 |
await clone_client.start()
|
| 1104 |
print(f"▶️ [WM] Clone Client Started Successfully")
|
| 1105 |
|
|
@@ -1108,7 +1114,6 @@ async def process_single_video(task, redis_client):
|
|
| 1108 |
raise Exception("Video not found or deleted by user.")
|
| 1109 |
|
| 1110 |
status_msg = await clone_client.send_message(owner_id, "⏳ <b>ভিডিও প্রসেসিং শুরু হয়েছে...</b>", parse_mode=enums.ParseMode.HTML)
|
| 1111 |
-
print(f"▶️ [WM] Status Message Sent to User")
|
| 1112 |
|
| 1113 |
raw_video_path = await clone_client.download_media(msg)
|
| 1114 |
print(f"▶️ [WM] Original Video Downloaded: {raw_video_path}")
|
|
@@ -1123,7 +1128,6 @@ async def process_single_video(task, redis_client):
|
|
| 1123 |
raw_storage_id = raw_sent.id
|
| 1124 |
stream_link = f"{BACKEND_URL}/stream/{raw_storage_id}"
|
| 1125 |
download_link = f"{BACKEND_URL}/download/{raw_storage_id}"
|
| 1126 |
-
print(f"▶️ [WM] Original video Uploaded to Storage")
|
| 1127 |
|
| 1128 |
await clone_client.edit_message_text(owner_id, status_msg.id, "⏳ <b>ওয়াটারমার্ক যুক্ত করা হচ্ছে... (কিছুক্ষণ সময় লাগতে পারে)</b>", parse_mode=enums.ParseMode.HTML)
|
| 1129 |
|
|
@@ -1136,25 +1140,19 @@ async def process_single_video(task, redis_client):
|
|
| 1136 |
"-c:a", "copy", "-movflags", "+faststart", watermarked_path
|
| 1137 |
]
|
| 1138 |
|
| 1139 |
-
print(f"▶️ [WM] Starting FFmpeg processing...")
|
| 1140 |
process = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
| 1141 |
stdout, stderr = await process.communicate()
|
| 1142 |
|
| 1143 |
if process.returncode != 0 or not os.path.exists(watermarked_path) or os.path.getsize(watermarked_path) == 0:
|
| 1144 |
-
print(f"❌ [WM] FFmpeg Error: {stderr.decode()}")
|
| 1145 |
raise Exception("FFmpeg processing failed! The video format might be unsupported.")
|
| 1146 |
|
| 1147 |
print(f"▶️ [WM] FFmpeg Processing Complete!")
|
| 1148 |
|
| 1149 |
-
# ==========================================
|
| 1150 |
-
# থাম্বনেইল জেনারেট করা হচ্ছে
|
| 1151 |
-
# ==========================================
|
| 1152 |
thumb_cmd = ["ffmpeg", "-y", "-i", watermarked_path, "-vframes", "1", thumb_path]
|
| 1153 |
t_proc = await asyncio.create_subprocess_exec(*thumb_cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
| 1154 |
await t_proc.communicate()
|
| 1155 |
if not os.path.exists(thumb_path) or os.path.getsize(thumb_path) == 0:
|
| 1156 |
thumb_path = None
|
| 1157 |
-
# ==========================================
|
| 1158 |
|
| 1159 |
await clone_client.edit_message_text(owner_id, status_msg.id, "⏳ <b>ফাইনাল ভিডিও আপলোড হচ্ছে...</b>", parse_mode=enums.ParseMode.HTML)
|
| 1160 |
|
|
@@ -1162,9 +1160,8 @@ async def process_single_video(task, redis_client):
|
|
| 1162 |
chat_id=STORAGE_CHANNEL_ID,
|
| 1163 |
video=watermarked_path,
|
| 1164 |
caption=f"Watermarked Backup for Clone Owner {owner_id}",
|
| 1165 |
-
thumb=thumb_path
|
| 1166 |
)
|
| 1167 |
-
print(f"▶️ [WM] Watermarked Backup Uploaded to Storage")
|
| 1168 |
|
| 1169 |
final_caption = (
|
| 1170 |
f"✅ <b>Watermark Successfully Added!</b>\n\n"
|
|
@@ -1177,26 +1174,28 @@ async def process_single_video(task, redis_client):
|
|
| 1177 |
video=watermarked_path,
|
| 1178 |
caption=final_caption,
|
| 1179 |
parse_mode=enums.ParseMode.HTML,
|
| 1180 |
-
thumb=thumb_path
|
| 1181 |
)
|
| 1182 |
-
print(f"▶️ [WM] Final Video Sent to User Successfully!")
|
| 1183 |
|
| 1184 |
await clone_client.delete_messages(owner_id, status_msg.id)
|
| 1185 |
|
| 1186 |
except Exception as e:
|
| 1187 |
print(f"❌ [WM] Process Error: {e}")
|
| 1188 |
try:
|
| 1189 |
-
|
|
|
|
| 1190 |
except Exception as ex:
|
| 1191 |
print(f"❌ [WM] Failed to send error msg to user: {ex}")
|
| 1192 |
|
| 1193 |
finally:
|
| 1194 |
-
# ফাইল
|
| 1195 |
-
if os.path.exists(raw_video_path): os.remove(raw_video_path)
|
| 1196 |
-
if os.path.exists(watermarked_path): os.remove(watermarked_path)
|
| 1197 |
if thumb_path and os.path.exists(thumb_path): os.remove(thumb_path)
|
| 1198 |
|
| 1199 |
-
|
|
|
|
|
|
|
| 1200 |
await redis_client.delete(f"wm_processing:{owner_id}")
|
| 1201 |
processing_owners.discard(owner_id)
|
| 1202 |
print(f"▶️ [WM] Task Cleaned Up and Client Stopped")
|
|
|
|
| 1094 |
wm_text = task['watermark_text']
|
| 1095 |
task_id = f"{owner_id}_{message_id}"
|
| 1096 |
|
| 1097 |
+
# ==============================
|
| 1098 |
+
# FIX: আগে ভেরিয়েবল ডিফাইন করা হলো যাতে finally ব্লকে এরর না আসে
|
| 1099 |
+
raw_video_path = None
|
| 1100 |
+
watermarked_path = None
|
| 1101 |
+
thumb_path = f"thumb_{task_id}.jpg"
|
| 1102 |
+
clone_client = None
|
| 1103 |
+
# ==============================
|
| 1104 |
|
| 1105 |
+
print(f"▶️ [WM] Task Started for User: {owner_id}, Msg: {message_id}")
|
|
|
|
| 1106 |
|
| 1107 |
try:
|
| 1108 |
+
clone_client = Client(f"clone_wm_{task_id}", bot_token=clone_token, api_id=API_ID, api_hash=API_HASH, in_memory=True)
|
| 1109 |
await clone_client.start()
|
| 1110 |
print(f"▶️ [WM] Clone Client Started Successfully")
|
| 1111 |
|
|
|
|
| 1114 |
raise Exception("Video not found or deleted by user.")
|
| 1115 |
|
| 1116 |
status_msg = await clone_client.send_message(owner_id, "⏳ <b>ভিডিও প্রসেসিং শুরু হয়েছে...</b>", parse_mode=enums.ParseMode.HTML)
|
|
|
|
| 1117 |
|
| 1118 |
raw_video_path = await clone_client.download_media(msg)
|
| 1119 |
print(f"▶️ [WM] Original Video Downloaded: {raw_video_path}")
|
|
|
|
| 1128 |
raw_storage_id = raw_sent.id
|
| 1129 |
stream_link = f"{BACKEND_URL}/stream/{raw_storage_id}"
|
| 1130 |
download_link = f"{BACKEND_URL}/download/{raw_storage_id}"
|
|
|
|
| 1131 |
|
| 1132 |
await clone_client.edit_message_text(owner_id, status_msg.id, "⏳ <b>ওয়াটারমার্ক যুক্ত করা হচ্ছে... (কিছুক্ষণ সময় লাগতে পারে)</b>", parse_mode=enums.ParseMode.HTML)
|
| 1133 |
|
|
|
|
| 1140 |
"-c:a", "copy", "-movflags", "+faststart", watermarked_path
|
| 1141 |
]
|
| 1142 |
|
|
|
|
| 1143 |
process = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
| 1144 |
stdout, stderr = await process.communicate()
|
| 1145 |
|
| 1146 |
if process.returncode != 0 or not os.path.exists(watermarked_path) or os.path.getsize(watermarked_path) == 0:
|
|
|
|
| 1147 |
raise Exception("FFmpeg processing failed! The video format might be unsupported.")
|
| 1148 |
|
| 1149 |
print(f"▶️ [WM] FFmpeg Processing Complete!")
|
| 1150 |
|
|
|
|
|
|
|
|
|
|
| 1151 |
thumb_cmd = ["ffmpeg", "-y", "-i", watermarked_path, "-vframes", "1", thumb_path]
|
| 1152 |
t_proc = await asyncio.create_subprocess_exec(*thumb_cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
| 1153 |
await t_proc.communicate()
|
| 1154 |
if not os.path.exists(thumb_path) or os.path.getsize(thumb_path) == 0:
|
| 1155 |
thumb_path = None
|
|
|
|
| 1156 |
|
| 1157 |
await clone_client.edit_message_text(owner_id, status_msg.id, "⏳ <b>ফাইনাল ভিডিও আপলোড হচ্ছে...</b>", parse_mode=enums.ParseMode.HTML)
|
| 1158 |
|
|
|
|
| 1160 |
chat_id=STORAGE_CHANNEL_ID,
|
| 1161 |
video=watermarked_path,
|
| 1162 |
caption=f"Watermarked Backup for Clone Owner {owner_id}",
|
| 1163 |
+
thumb=thumb_path
|
| 1164 |
)
|
|
|
|
| 1165 |
|
| 1166 |
final_caption = (
|
| 1167 |
f"✅ <b>Watermark Successfully Added!</b>\n\n"
|
|
|
|
| 1174 |
video=watermarked_path,
|
| 1175 |
caption=final_caption,
|
| 1176 |
parse_mode=enums.ParseMode.HTML,
|
| 1177 |
+
thumb=thumb_path
|
| 1178 |
)
|
|
|
|
| 1179 |
|
| 1180 |
await clone_client.delete_messages(owner_id, status_msg.id)
|
| 1181 |
|
| 1182 |
except Exception as e:
|
| 1183 |
print(f"❌ [WM] Process Error: {e}")
|
| 1184 |
try:
|
| 1185 |
+
if clone_client and clone_client.is_connected:
|
| 1186 |
+
await clone_client.send_message(owner_id, f"❌ <b>Error processing video:</b> {e}", parse_mode=enums.ParseMode.HTML)
|
| 1187 |
except Exception as ex:
|
| 1188 |
print(f"❌ [WM] Failed to send error msg to user: {ex}")
|
| 1189 |
|
| 1190 |
finally:
|
| 1191 |
+
# ফাইল এবং লক ক্লিনআপ
|
| 1192 |
+
if raw_video_path and os.path.exists(raw_video_path): os.remove(raw_video_path)
|
| 1193 |
+
if watermarked_path and os.path.exists(watermarked_path): os.remove(watermarked_path)
|
| 1194 |
if thumb_path and os.path.exists(thumb_path): os.remove(thumb_path)
|
| 1195 |
|
| 1196 |
+
if clone_client and clone_client.is_connected:
|
| 1197 |
+
await clone_client.stop()
|
| 1198 |
+
|
| 1199 |
await redis_client.delete(f"wm_processing:{owner_id}")
|
| 1200 |
processing_owners.discard(owner_id)
|
| 1201 |
print(f"▶️ [WM] Task Cleaned Up and Client Stopped")
|