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