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