]> git.scottworley.com Git - srec/blob - srec.py
Recording object
[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 from dataclasses import dataclass
8 import subprocess
9 from typing import Callable
10
11 import gi
12 gi.require_version("Gtk", "4.0")
13 from gi.repository import Gtk # nopep8 pylint: disable=wrong-import-position
14
15
16 @dataclass
17 class Recording:
18 process: subprocess.Popen[bytes]
19
20
21 recording: Recording | None = None
22
23
24 def on_start_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
25 global recording # pylint: disable=global-statement
26 assert recording is None
27 screen_size = '1366x768' # TODO
28 command = [
29 'ffmpeg',
30 '-video_size', screen_size,
31 '-framerate', '25',
32 '-f', 'x11grab', '-i', ':0.0+0,0',
33 '-f', 'pulse', '-ac', '2', '-i', 'default',
34 'screen-recording.mkv'] # nopep8
35 # pylint: disable=consider-using-with
36 recording = Recording(
37 process=subprocess.Popen(command, stdin=subprocess.PIPE))
38 stack.set_visible_child_name("recording")
39
40
41 def on_stop_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
42 global recording # pylint: disable=global-statement
43 assert recording is not None
44 stdin = recording.process.stdin
45 assert stdin is not None
46 stdin.write(b'q')
47 stdin.flush()
48 recording.process.wait()
49 recording = None
50 stack.set_visible_child_name("not_recording")
51
52
53 def make_button(label: str, action: Callable[[
54 Gtk.Button, Gtk.Stack], None], stack: Gtk.Stack) -> Gtk.Button:
55 button = Gtk.Button(label=label)
56 button.connect('clicked', action, stack)
57 button.set_margin_top(10)
58 button.set_margin_start(10)
59 button.set_margin_end(10)
60 button.set_margin_bottom(10)
61 return button
62
63
64 def on_activate(app: Gtk.Application) -> None:
65 win = Gtk.ApplicationWindow(application=app)
66 win.set_title('SRec')
67 stack = Gtk.Stack()
68
69 start_recording = make_button("Start Recording", on_start_recording, stack)
70 stack.add_named(start_recording, "not_recording")
71
72 box = Gtk.Box()
73 box.set_orientation(Gtk.Orientation.VERTICAL)
74 stop_recording = make_button("Stop Recording", on_stop_recording, stack)
75 box.append(stop_recording)
76 stack.add_named(box, "recording")
77
78 win.set_child(stack)
79 win.present()
80
81
82 def main() -> None:
83 app = Gtk.Application(application_id='net.chkno.srec')
84 app.connect('activate', on_activate)
85 app.run(None)
86
87
88 if __name__ == '__main__':
89 main()