Advertisement
Najeebsk

DESKTOP_RECODER.pyw

Jun 15th, 2024
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. import sys
  2. import subprocess
  3. from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QVBoxLayout, QWidget
  4. from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot
  5.  
  6. class Recorder(QThread):
  7.     changeStatus = pyqtSignal(str)
  8.  
  9.     def __init__(self, ffmpeg_path):
  10.         super().__init__()
  11.         self.recording = False
  12.         self.process = None
  13.         self.ffmpeg_path = ffmpeg_path
  14.  
  15.     def run(self):
  16.         self.recording = True
  17.         command = [
  18.             self.ffmpeg_path,
  19.             '-y',  # Overwrite output file if it exists
  20.             '-f', 'gdigrab',
  21.             '-framerate', '20',
  22.             '-i', 'desktop',  # Capture the entire desktop
  23.             '-c:v', 'libx264',
  24.             '-preset', 'ultrafast',
  25.             'output.mp4'
  26.         ]
  27.        
  28.         try:
  29.             self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
  30.             self.changeStatus.emit("Recording started...")
  31.  
  32.             while self.recording:
  33.                 output = self.process.stderr.readline()
  34.                 if output == '' and self.process.poll() is not None:
  35.                     break
  36.                 if output:
  37.                     print(output.strip())
  38.  
  39.             if self.process.poll() is None:
  40.                 self.process.terminate()
  41.                 self.process.wait()
  42.             self.changeStatus.emit("Recording stopped and saved to output.mp4.")
  43.         except Exception as e:
  44.             self.changeStatus.emit(f"Error: {e}")
  45.             self.recording = False
  46.  
  47.     def stop_recording(self):
  48.         self.recording = False
  49.         if self.process and self.process.poll() is None:
  50.             self.process.terminate()
  51.             self.process.wait()
  52.  
  53. class App(QMainWindow):
  54.     def __init__(self, ffmpeg_path):
  55.         super().__init__()
  56.         self.setWindowTitle("Desktop Video Recorder")
  57.         self.setGeometry(100, 100, 400, 200)
  58.  
  59.         self.recorder = None
  60.         self.ffmpeg_path = ffmpeg_path
  61.  
  62.         self.initUI()
  63.  
  64.     def initUI(self):
  65.         self.status_label = QLabel("Press Start to begin recording the desktop.", self)
  66.         self.status_label.resize(400, 50)
  67.  
  68.         self.start_button = QPushButton("Start Recording", self)
  69.         self.start_button.clicked.connect(self.start_recording)
  70.  
  71.         self.stop_button = QPushButton("Stop Recording", self)
  72.         self.stop_button.clicked.connect(self.stop_recording)
  73.         self.stop_button.setEnabled(False)
  74.  
  75.         layout = QVBoxLayout()
  76.         layout.addWidget(self.status_label)
  77.         layout.addWidget(self.start_button)
  78.         layout.addWidget(self.stop_button)
  79.  
  80.         container = QWidget()
  81.         container.setLayout(layout)
  82.         self.setCentralWidget(container)
  83.  
  84.     def start_recording(self):
  85.         self.recorder = Recorder(self.ffmpeg_path)
  86.         self.recorder.changeStatus.connect(self.updateStatus)
  87.         self.recorder.start()
  88.         self.start_button.setEnabled(False)
  89.         self.stop_button.setEnabled(True)
  90.  
  91.     def stop_recording(self):
  92.         if self.recorder:
  93.             self.recorder.stop_recording()
  94.             self.recorder.wait()
  95.             self.stop_button.setEnabled(False)
  96.             self.start_button.setEnabled(True)
  97.  
  98.     @pyqtSlot(str)
  99.     def updateStatus(self, message):
  100.         self.status_label.setText(message)
  101.         print(message)  # Print to console for debugging
  102.  
  103. if __name__ == '__main__':
  104.     ffmpeg_path = r'C:\CMDER\APP\ffmpeg.exe'  # Update with your ffmpeg path
  105.     app = QApplication(sys.argv)
  106.     ex = App(ffmpeg_path)
  107.     ex.show()
  108.     sys.exit(app.exec_())
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement