Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import subprocess
- from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QVBoxLayout, QWidget
- from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot
- class Recorder(QThread):
- changeStatus = pyqtSignal(str)
- def __init__(self, ffmpeg_path):
- super().__init__()
- self.recording = False
- self.process = None
- self.ffmpeg_path = ffmpeg_path
- def run(self):
- self.recording = True
- command = [
- self.ffmpeg_path,
- '-y', # Overwrite output file if it exists
- '-f', 'gdigrab',
- '-framerate', '20',
- '-i', 'desktop', # Capture the entire desktop
- '-c:v', 'libx264',
- '-preset', 'ultrafast',
- 'output.mp4'
- ]
- try:
- self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
- self.changeStatus.emit("Recording started...")
- while self.recording:
- output = self.process.stderr.readline()
- if output == '' and self.process.poll() is not None:
- break
- if output:
- print(output.strip())
- if self.process.poll() is None:
- self.process.terminate()
- self.process.wait()
- self.changeStatus.emit("Recording stopped and saved to output.mp4.")
- except Exception as e:
- self.changeStatus.emit(f"Error: {e}")
- self.recording = False
- def stop_recording(self):
- self.recording = False
- if self.process and self.process.poll() is None:
- self.process.terminate()
- self.process.wait()
- class App(QMainWindow):
- def __init__(self, ffmpeg_path):
- super().__init__()
- self.setWindowTitle("Desktop Video Recorder")
- self.setGeometry(100, 100, 400, 200)
- self.recorder = None
- self.ffmpeg_path = ffmpeg_path
- self.initUI()
- def initUI(self):
- self.status_label = QLabel("Press Start to begin recording the desktop.", self)
- self.status_label.resize(400, 50)
- self.start_button = QPushButton("Start Recording", self)
- self.start_button.clicked.connect(self.start_recording)
- self.stop_button = QPushButton("Stop Recording", self)
- self.stop_button.clicked.connect(self.stop_recording)
- self.stop_button.setEnabled(False)
- layout = QVBoxLayout()
- layout.addWidget(self.status_label)
- layout.addWidget(self.start_button)
- layout.addWidget(self.stop_button)
- container = QWidget()
- container.setLayout(layout)
- self.setCentralWidget(container)
- def start_recording(self):
- self.recorder = Recorder(self.ffmpeg_path)
- self.recorder.changeStatus.connect(self.updateStatus)
- self.recorder.start()
- self.start_button.setEnabled(False)
- self.stop_button.setEnabled(True)
- def stop_recording(self):
- if self.recorder:
- self.recorder.stop_recording()
- self.recorder.wait()
- self.stop_button.setEnabled(False)
- self.start_button.setEnabled(True)
- @pyqtSlot(str)
- def updateStatus(self, message):
- self.status_label.setText(message)
- print(message) # Print to console for debugging
- if __name__ == '__main__':
- ffmpeg_path = r'C:\CMDER\APP\ffmpeg.exe' # Update with your ffmpeg path
- app = QApplication(sys.argv)
- ex = App(ffmpeg_path)
- ex.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement