]> git.scottworley.com Git - srec/blob - srec.py
50f311c14d1dd6d2ff4e4fac08a31fb9af03d645
[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 Any, Callable
12
13 import gi
14 gi.require_version("Gtk", "4.0")
15 gi.require_version("GLib", "2.0")
16
17 from gi.repository import Gtk # nopep8 pylint: disable=wrong-import-position
18 from gi.repository import GLib # nopep8 pylint: disable=wrong-import-position
19
20
21 @dataclass
22 class Recording:
23 process: subprocess.Popen[bytes]
24
25 def stop(self) -> None:
26 stdin = self.process.stdin
27 assert stdin is not None
28 stdin.write(b'q')
29 stdin.flush()
30 self.process.wait()
31
32
33 recording: Recording | None = None
34
35
36 def make_filename() -> str:
37 directory = os.environ.get(
38 'XDG_VIDEOS_DIR',
39 os.path.expanduser('~/Videos/SRec'))
40 os.makedirs(directory, exist_ok=True)
41 timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
42 return os.path.join(directory, f'srec {timestamp}.mkv')
43
44
45 def video_source(stack: Gtk.Stack) -> list[str]:
46 if stack.get_child_by_name('not_recording').get_first_child().get_active():
47 return ['-f', 'x11grab', '-i', ':0.0+0,0']
48 return ['-f', 'v4l2', '-i', '/dev/video0']
49
50
51 def find_size_display(stack: Gtk.Stack) -> Gtk.Label:
52 return stack.get_child_by_name(
53 'recording').get_first_child().get_next_sibling()
54
55
56 def summarize_size(n: int) -> str:
57 if n > 100_000_000:
58 m = int(n / (1024 * 1024))
59 return f'{m}M'
60 if n > 100_000:
61 k = int(n / 1024)
62 return f'{k}K'
63 return str(n)
64
65
66 def on_start_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
67 global recording # pylint: disable=global-statement
68 assert recording is None
69
70 filename = make_filename()
71 size_display = find_size_display(stack)
72
73 def update_size_display() -> Any:
74 try:
75 size = summarize_size(os.stat(filename).st_size)
76 except FileNotFoundError:
77 size = '--'
78 size_display.set_label(f'<big>{size}</big>')
79 return GLib.SOURCE_REMOVE if recording is None else GLib.SOURCE_CONTINUE
80 GLib.timeout_add_seconds(1, update_size_display)
81
82 command = (['ffmpeg', '-framerate', '25'] + video_source(stack) +
83 ['-f', 'pulse', '-ac', '2', '-i', 'default', filename])
84 # pylint: disable=consider-using-with
85 recording = Recording(
86 process=subprocess.Popen(command, stdin=subprocess.PIPE))
87 stack.set_visible_child_name("recording")
88
89
90 def on_stop_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
91 global recording # pylint: disable=global-statement
92 assert recording is not None
93 recording.stop()
94 recording = None
95 stack.set_visible_child_name("not_recording")
96
97
98 def make_button(label: str, action: Callable[[
99 Gtk.Button, Gtk.Stack], None], stack: Gtk.Stack) -> Gtk.Button:
100 button = Gtk.Button(label=label)
101 button.connect('clicked', action, stack)
102 button.set_margin_top(10)
103 button.set_margin_start(10)
104 button.set_margin_end(10)
105 button.set_margin_bottom(10)
106 return button
107
108
109 def make_share_control() -> Gtk.CheckButton:
110 can_share = os.path.exists('/sys/module/v4l2loopback')
111 control = Gtk.CheckButton(
112 label='Share Webcam', sensitive=can_share, active=can_share)
113 control.set_margin_start(20)
114 return control
115
116
117 def on_activate(app: Gtk.Application) -> None:
118 win = Gtk.ApplicationWindow(application=app)
119 win.set_title('SRec')
120 win.set_icon_name('srec')
121
122 stack = Gtk.Stack()
123
124 nr_box = Gtk.Box()
125 nr_box.set_orientation(Gtk.Orientation.VERTICAL)
126 screen = Gtk.CheckButton(label='Screen')
127 nr_box.append(screen)
128 nr_box.append(Gtk.CheckButton(label='Webcam', active=True, group=screen))
129 nr_box.append(make_share_control())
130 nr_box.append(make_button("Start Recording", on_start_recording, stack))
131 stack.add_named(nr_box, "not_recording")
132
133 r_box = Gtk.Box()
134 r_box.set_orientation(Gtk.Orientation.VERTICAL)
135 r_box.append(make_button("Stop Recording", on_stop_recording, stack))
136 r_box.append(Gtk.Label(use_markup=True, justify=Gtk.Justification.CENTER))
137 stack.add_named(r_box, "recording")
138
139 win.set_child(stack)
140 win.present()
141
142
143 def main() -> None:
144 app = Gtk.Application(application_id='net.chkno.srec')
145 app.connect('activate', on_activate)
146 app.run(None)
147
148
149 if __name__ == '__main__':
150 main()