]> git.scottworley.com Git - srec/blob - srec.py
36000c92019c20a196cd7853d412a12633c66876
[srec] / srec.py
1 # srec: A simple GUI for screen recording
2 #
3 # This program is free software: you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation, version 3.
6
7 import subprocess
8 from typing import Callable
9
10 import gi
11 gi.require_version("Gtk", "4.0")
12 from gi.repository import Gtk # nopep8 pylint: disable=wrong-import-position
13
14
15 recording = None
16
17
18 def on_start_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
19 global recording # pylint: disable=global-statement
20 assert recording is None
21 screen_size = '1366x768' # TODO
22 command = [
23 'ffmpeg',
24 '-video_size', screen_size,
25 '-framerate', '25',
26 '-f', 'x11grab', '-i', ':0.0+0,0',
27 '-f', 'pulse', '-ac', '2', '-i', 'default',
28 'screen-recording.mkv'] # nopep8
29 # pylint: disable=consider-using-with
30 recording = subprocess.Popen(command, stdin=subprocess.PIPE)
31 stack.set_visible_child_name("recording")
32
33
34 def on_stop_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
35 global recording # pylint: disable=global-statement
36 assert recording is not None
37 stdin = recording.stdin
38 assert stdin is not None
39 stdin.write(b'q')
40 stdin.flush()
41 recording.wait()
42 recording = None
43 stack.set_visible_child_name("not_recording")
44
45
46 def make_button(label: str, action: Callable[[
47 Gtk.Button, Gtk.Stack], None], stack: Gtk.Stack) -> Gtk.Button:
48 button = Gtk.Button(label=label)
49 button.connect('clicked', action, stack)
50 button.set_margin_top(10)
51 button.set_margin_start(10)
52 button.set_margin_end(10)
53 button.set_margin_bottom(10)
54 return button
55
56
57 def on_activate(app: Gtk.Application) -> None:
58 win = Gtk.ApplicationWindow(application=app)
59 win.set_title('SRec')
60 stack = Gtk.Stack()
61
62 start_recording = make_button("Start Recording", on_start_recording, stack)
63 stack.add_named(start_recording, "not_recording")
64
65 box = Gtk.Box()
66 box.set_orientation(Gtk.Orientation.VERTICAL)
67 stop_recording = make_button("Stop Recording", on_stop_recording, stack)
68 box.append(stop_recording)
69 stack.add_named(box, "recording")
70
71 win.set_child(stack)
72 win.present()
73
74
75 def main() -> None:
76 app = Gtk.Application(application_id='net.chkno.srec')
77 app.connect('activate', on_activate)
78 app.run(None)
79
80
81 if __name__ == '__main__':
82 main()