pmrony commited on
Commit
12535e9
·
verified ·
1 Parent(s): 92b2470

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -13
app.py CHANGED
@@ -643,7 +643,6 @@ async def handle_media_upload(client, message):
643
 
644
  is_large_video = False
645
  if media_type == "video":
646
- # Safe size checks for both Videos and Animations (GIFs)
647
  media = get_media_obj(message)
648
  duration = media.duration if media and hasattr(media, 'duration') and media.duration else 0
649
  file_size = media.file_size if media and hasattr(media, 'file_size') and media.file_size else 0
@@ -683,18 +682,25 @@ async def handle_media_upload(client, message):
683
  if media_type == "video" and not is_large_video:
684
  await status_msg.edit_text("⏳ Watermarking video... (HD + Superfast Processing)")
685
  watermarked_file = f"{original_file}_wm.mp4"
 
 
 
 
686
  cmd = [
687
  "ffmpeg", "-y", "-i", original_file,
688
  "-vf", "drawtext=text='@mxvdo':x=W-tw-20:y=H-th-20:fontsize=22:fontcolor=white@0.7:shadowcolor=black@0.8:shadowx=2:shadowy=2:enable='gte(t,5)'",
689
  "-c:v", "libx264", "-preset", "superfast", "-crf", "23",
690
- "-pix_fmt", "yuv420p", "-c:a", "copy", "-movflags", "+faststart", watermarked_file
691
- ]
 
692
  process = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
693
  await process.communicate()
694
- if process.returncode == 0 and os.path.exists(watermarked_file):
 
695
  clean_upload_file = watermarked_file
696
 
697
- # ২. আপনার নিজের চ্যানেলে ফাইল আপলোড (ভিডিও বা অ্যানিমেশন)
 
698
  storage_msg_id = None
699
  if media_type in ["video", "photo"]:
700
  await status_msg.edit_text(f"⏳ Uploading {media_type} to your storage channel...")
@@ -702,7 +708,27 @@ async def handle_media_upload(client, message):
702
  if media_type == "photo":
703
  sent_to_channel = await client.send_photo(chat_id=STORAGE_CHANNEL_ID, photo=clean_upload_file, caption="Backup of photo uploaded by Admin.")
704
  else:
705
- sent_to_channel = await client.send_video(chat_id=STORAGE_CHANNEL_ID, video=clean_upload_file, caption=f"Backup of video uploaded by Admin. File: {os.path.basename(clean_upload_file)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
706
 
707
  storage_msg_id = sent_to_channel.id
708
 
@@ -735,6 +761,8 @@ async def handle_media_upload(client, message):
735
 
736
  if media_type == "photo":
737
  cmd_blur = ["ffmpeg", "-y", "-i", clean_upload_file] + ff_filter + [blurred_file]
 
 
738
  else:
739
  cmd_blur = ["ffmpeg", "-y", "-i", clean_upload_file] + ff_filter + ["-c:v", "libx264", "-preset", "superfast", "-crf", "23", "-pix_fmt", "yuv420p", "-c:a", "copy", "-movflags", "+faststart", blurred_file]
740
 
@@ -767,8 +795,9 @@ async def handle_media_upload(client, message):
767
 
768
  if media_type == "photo":
769
  sent_to_admin = await client.send_photo(message.chat.id, telegram_file, caption=admin_cap, parse_mode=enums.ParseMode.HTML)
 
 
770
  else:
771
- # Safe dimensions extraction for Videos and Animations
772
  media = get_media_obj(message)
773
  vid_duration = media.duration if media and hasattr(media, 'duration') and media.duration else 0
774
  vid_width = media.width if media and hasattr(media, 'width') and media.width else 0
@@ -818,7 +847,7 @@ async def handle_media_upload(client, message):
818
 
819
  except Exception as e: await message.reply(f"⚠️ Error occurred: {str(e)}")
820
  finally:
821
- for f in [original_file, watermarked_file, blurred_file, f"{original_file}_thumb.jpg" if original_file else None]:
822
  if f and os.path.exists(f):
823
  try: os.remove(f)
824
  except: pass
@@ -901,7 +930,7 @@ async def manual_clean_channel(client, message):
901
 
902
  for user_id in set(user_ids):
903
  try:
904
- chat_member = await client.get_chat_member(PREMIUM_CHANNEL_ID, user_id)
905
 
906
  if chat_member.status in [enums.ChatMemberStatus.MEMBER, enums.ChatMemberStatus.RESTRICTED]:
907
  checked += 1
@@ -911,21 +940,21 @@ async def manual_clean_channel(client, message):
911
  is_valid = False
912
  if res_session.data:
913
  session_string = res_session.data[0]['session_string']
914
- temp_client = Client(f"manual_chk_{user_id}", session_string=session_string, api_id=API_ID, api_hash=API_HASH, in_memory=True)
915
  try:
916
  await temp_client.connect()
917
  await temp_client.get_me()
918
  await temp_client.disconnect()
919
  is_valid = True
920
- except Exception:
921
  try: await temp_client.disconnect()
922
  except: pass
923
  await db_query(lambda: supabase.table('user_sessions').delete().eq('user_id', user_id).execute())
924
 
925
  if not is_valid:
926
  try:
927
- await client.ban_chat_member(PREMIUM_CHANNEL_ID, user_id)
928
- await client.unban_chat_member(PREMIUM_CHANNEL_ID, user_id)
929
  kicked += 1
930
  except Exception: pass
931
 
 
643
 
644
  is_large_video = False
645
  if media_type == "video":
 
646
  media = get_media_obj(message)
647
  duration = media.duration if media and hasattr(media, 'duration') and media.duration else 0
648
  file_size = media.file_size if media and hasattr(media, 'file_size') and media.file_size else 0
 
682
  if media_type == "video" and not is_large_video:
683
  await status_msg.edit_text("⏳ Watermarking video... (HD + Superfast Processing)")
684
  watermarked_file = f"{original_file}_wm.mp4"
685
+
686
+ has_audio = not bool(message.animation)
687
+ audio_opts = ["-an"] if not has_audio else ["-c:a", "copy"]
688
+
689
  cmd = [
690
  "ffmpeg", "-y", "-i", original_file,
691
  "-vf", "drawtext=text='@mxvdo':x=W-tw-20:y=H-th-20:fontsize=22:fontcolor=white@0.7:shadowcolor=black@0.8:shadowx=2:shadowy=2:enable='gte(t,5)'",
692
  "-c:v", "libx264", "-preset", "superfast", "-crf", "23",
693
+ "-pix_fmt", "yuv420p"
694
+ ] + audio_opts + ["-movflags", "+faststart", watermarked_file]
695
+
696
  process = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
697
  await process.communicate()
698
+
699
+ if process.returncode == 0 and os.path.exists(watermarked_file) and os.path.getsize(watermarked_file) > 0:
700
  clean_upload_file = watermarked_file
701
 
702
+ # ২. আপনার নিজের টেলিগ্রাম স্টোরেজ চ্যানেলে ফাইল আপলোড (ভিডিও বা ফটো)
703
+ # এখানে থাম্বনেইল, সঠিক ডিউরেশন এবং সাইজ জেনারেট করে পোস্ট করা হবে যাতে সাদা ০:০০ শো না করে
704
  storage_msg_id = None
705
  if media_type in ["video", "photo"]:
706
  await status_msg.edit_text(f"⏳ Uploading {media_type} to your storage channel...")
 
708
  if media_type == "photo":
709
  sent_to_channel = await client.send_photo(chat_id=STORAGE_CHANNEL_ID, photo=clean_upload_file, caption="Backup of photo uploaded by Admin.")
710
  else:
711
+ # ভিডিওর অরিজিনাল মেটাডাটা থাম্বনেইল বের করা হচ্ছে
712
+ thumb_path_storage = f"{original_file}_storage_thumb.jpg"
713
+ proc = await asyncio.create_subprocess_exec("ffmpeg", "-y", "-i", clean_upload_file, "-vframes", "1", thumb_path_storage, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
714
+ await proc.communicate()
715
+ if not os.path.exists(thumb_path_storage):
716
+ thumb_path_storage = None
717
+
718
+ media = get_media_obj(message)
719
+ vid_duration = media.duration if media and hasattr(media, 'duration') and media.duration else 0
720
+ vid_width = media.width if media and hasattr(media, 'width') and media.width else 0
721
+ vid_height = media.height if media and hasattr(media, 'height') and media.height else 0
722
+
723
+ sent_to_channel = await client.send_video(
724
+ chat_id=STORAGE_CHANNEL_ID,
725
+ video=clean_upload_file,
726
+ caption=f"Backup of video uploaded by Admin. File: {os.path.basename(clean_upload_file)}",
727
+ duration=vid_duration,
728
+ width=vid_width,
729
+ height=vid_height,
730
+ thumb=thumb_path_storage
731
+ )
732
 
733
  storage_msg_id = sent_to_channel.id
734
 
 
761
 
762
  if media_type == "photo":
763
  cmd_blur = ["ffmpeg", "-y", "-i", clean_upload_file] + ff_filter + [blurred_file]
764
+ elif media_type == "animation":
765
+ cmd_blur = ["ffmpeg", "-y", "-i", clean_upload_file] + ff_filter + ["-c:v", "libx264", "-preset", "superfast", "-pix_fmt", "yuv420p", blurred_file]
766
  else:
767
  cmd_blur = ["ffmpeg", "-y", "-i", clean_upload_file] + ff_filter + ["-c:v", "libx264", "-preset", "superfast", "-crf", "23", "-pix_fmt", "yuv420p", "-c:a", "copy", "-movflags", "+faststart", blurred_file]
768
 
 
795
 
796
  if media_type == "photo":
797
  sent_to_admin = await client.send_photo(message.chat.id, telegram_file, caption=admin_cap, parse_mode=enums.ParseMode.HTML)
798
+ elif media_type == "animation":
799
+ sent_to_admin = await client.send_animation(message.chat.id, telegram_file, caption=admin_cap, parse_mode=enums.ParseMode.HTML)
800
  else:
 
801
  media = get_media_obj(message)
802
  vid_duration = media.duration if media and hasattr(media, 'duration') and media.duration else 0
803
  vid_width = media.width if media and hasattr(media, 'width') and media.width else 0
 
847
 
848
  except Exception as e: await message.reply(f"⚠️ Error occurred: {str(e)}")
849
  finally:
850
+ for f in [original_file, watermarked_file, blurred_file, f"{original_file}_thumb.jpg" if original_file else None, f"{original_file}_storage_thumb.jpg" if original_file else None]:
851
  if f and os.path.exists(f):
852
  try: os.remove(f)
853
  except: pass
 
930
 
931
  for user_id in set(user_ids):
932
  try:
933
+ chat_member = await bot.get_chat_member(PREMIUM_CHANNEL_ID, user_id)
934
 
935
  if chat_member.status in [enums.ChatMemberStatus.MEMBER, enums.ChatMemberStatus.RESTRICTED]:
936
  checked += 1
 
940
  is_valid = False
941
  if res_session.data:
942
  session_string = res_session.data[0]['session_string']
943
+ temp_client = Client(f"bg_chk_{user_id}", session_string=session_string, api_id=API_ID, api_hash=API_HASH, in_memory=True)
944
  try:
945
  await temp_client.connect()
946
  await temp_client.get_me()
947
  await temp_client.disconnect()
948
  is_valid = True
949
+ except (SessionRevoked, AuthKeyUnregistered, UserDeactivated):
950
  try: await temp_client.disconnect()
951
  except: pass
952
  await db_query(lambda: supabase.table('user_sessions').delete().eq('user_id', user_id).execute())
953
 
954
  if not is_valid:
955
  try:
956
+ await bot.ban_chat_member(PREMIUM_CHANNEL_ID, user_id)
957
+ await bot.unban_chat_member(PREMIUM_CHANNEL_ID, user_id)
958
  kicked += 1
959
  except Exception: pass
960