]> git.scottworley.com Git - srec/blob - srec.py
da14735ca81d3043995afc46fd4e84e801270c0c
[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 from datetime import datetime
9 import os
10 import subprocess
11 from typing import Callable
12
13 import gi
14 gi.require_version("Gtk", "4.0")
15 from gi.repository import Gtk # nopep8 pylint: disable=wrong-import-position
16
17
18 @dataclass
19 class Recording:
20 filename: str
21 process: subprocess.Popen[bytes]
22
23
24 recording: Recording | None = None
25
26
27 def make_filename() -> str:
28 directory = os.environ.get(
29 'XDG_VIDEOS_DIR',
30 os.path.expanduser('~/Videos'))
31 os.makedirs(directory, exist_ok=True)
32 timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
33 return os.path.join(directory, f'srec {timestamp}.mkv')
34
35
36 def on_start_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
37 global recording # pylint: disable=global-statement
38 assert recording is None
39 filename = make_filename()
40 command = [
41 'ffmpeg',
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 nr_box = Gtk.Box()
82 nr_box.set_orientation(Gtk.Orientation.VERTICAL)
83 nr_box.append(make_button("Start Recording", on_start_recording, stack))
84 stack.add_named(nr_box, "not_recording")
85
86 r_box = Gtk.Box()
87 r_box.set_orientation(Gtk.Orientation.VERTICAL)
88 r_box.append(make_button("Stop Recording", on_stop_recording, stack))
89 stack.add_named(r_box, "recording")
90
91 win.set_child(stack)
92 win.present()
93
94
95 def main() -> None:
96 app = Gtk.Application(application_id='net.chkno.srec')
97 app.connect('activate', on_activate)
98 app.run(None)
99
100
101 if __name__ == '__main__':
102 main()