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