Repository navigation
Expand file tree
/
Copy pathscreen_recorder.py
More file actions
147 lines (120 loc) · 4.99 KB
/
Copy pathscreen_recorder.py
File metadata and controls
147 lines (120 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import atexit
import os
import re
import signal
import subprocess
import time
from tray_indicator import Icon
class ScreenRecorder:
def __init__(self, output_dir="./recordings", color='red'):
os.makedirs(output_dir, exist_ok=True)
files = os.listdir(output_dir)
pattern = re.compile(r"output(\d+).*\.mp4$")
n = max([int(pattern.search(item).group(1)) for item in files if pattern.search(item)], default=0)
n += 1
output_file = f"{output_dir}/output{n}.mp4"
self.process = None
self.output_file = output_file
self.display = os.environ.get('DISPLAY', ':0')
self.primary_display_info = self._get_primary_display_info()
self.icon = Icon(color)
def _get_primary_display_info(self):
"""Get information about the primary display using xrandr"""
try:
# Run xrandr and get the output
result = subprocess.run(['xrandr', '--query'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
# Parse the output to find primary display
lines = result.stdout.split('\n')
primary_pattern = r'(\S+) connected primary (\d+)x(\d+)\+(\d+)\+(\d+)'
for line in lines:
match = re.search(primary_pattern, line)
if match:
display_name = match.group(1)
width = int(match.group(2))
height = int(match.group(3))
x_offset = int(match.group(4))
y_offset = int(match.group(5))
return {
'name': display_name,
'width': width,
'height': height,
'x_offset': x_offset,
'y_offset': y_offset
}
# If no primary display found, return None
return None
except Exception as e:
print(f"Error getting display information: {e}")
return None
def start_recording(self, framerate=30):
"""Start recording the primary display"""
if not self.primary_display_info:
print("Error: Could not detect primary display information")
return False
width = self.primary_display_info['width']
height = self.primary_display_info['height']
x_offset = self.primary_display_info['x_offset']
y_offset = self.primary_display_info['y_offset']
# Build the FFmpeg command
cmd = [
'ffmpeg',
'-f', 'x11grab',
'-video_size', f'{width}x{height}',
'-framerate', str(framerate),
'-i', f'{self.display}+{x_offset},{y_offset}',
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-qp', '0',
self.output_file
]
try:
print(f"Starting recording of primary display ({width}x{height} at position {x_offset},{y_offset})")
print(f"Output file: {self.output_file}")
print("Press Ctrl+C to stop recording")
# Start the FFmpeg process
self.process = subprocess.Popen(cmd)
# Register the cleanup function to handle exit
atexit.register(self.stop_recording)
self.icon.show_icon()
return True
except Exception as e:
print(f"Error starting recording: {e}")
return False
def stop_recording(self):
"""Stop the recording by properly terminating the FFmpeg process"""
if self.process:
try:
# Send SIGTERM signal to properly finish the recording
self.process.send_signal(signal.SIGTERM)
# Wait for the process to terminate
self.process.wait(timeout=10)
print(f"\nRecording stopped. Output saved to: {self.output_file}")
# Deregister the exit handler
atexit.unregister(self.stop_recording)
self.process = None
return True
except Exception as e:
print(f"Error stopping recording: {e}")
# Force kill if termination failed
self.process.kill()
self.process = None
return False
finally:
self.icon.hide_icon()
return False
def main():
recorder = ScreenRecorder()
# Parse command line arguments
import argparse
parser = argparse.ArgumentParser(description='Record the primary screen on Linux using FFmpeg')
parser.add_argument('-o', '--output', help='Output file name (default: screen_recording.mp4)')
parser.add_argument('-f', '--framerate', type=int, default=30, help='Framerate (default: 30)')
args = parser.parse_args()
recorder.start_recording(framerate=args.framerate)
time.sleep(10)
recorder.stop_recording()
if __name__ == "__main__":
main()