]> git.scottworley.com Git - srec/blob - srec.py
4abe5817ac2be6826201dbfbddf0aab77c2c8ba4
[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/SRec'))
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 video_source(stack: Gtk.Stack) -> list[str]:
37 if stack.get_child_by_name('not_recording').get_first_child().get_active():
38 return ['-f', 'v4l2', '-i', '/dev/video0']
39 return ['-f', 'x11grab', '-i', ':0.0+0,0']
40
41
42 def on_start_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
43 global recording # pylint: disable=global-statement
44 assert recording is None
45 filename = make_filename()
46 command = (['ffmpeg', '-framerate', '25'] + video_source(stack) +
47 ['-f', 'pulse', '-ac', '2', '-i', 'default', filename])
48 # pylint: disable=consider-using-with
49 recording = Recording(
50 filename=filename,
51 process=subprocess.Popen(command, stdin=subprocess.PIPE))
52 stack.set_visible_child_name("recording")
53
54
55 def on_stop_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
56 global recording # pylint: disable=global-statement
57 assert recording is not None
58 stdin = recording.process.stdin
59 assert stdin is not None
60 stdin.write(b'q')
61 stdin.flush()
62 recording.process.wait()
63 recording = None
64 stack.set_visible_child_name("not_recording")
65
66
67 def make_button(label: str, action: Callable[[
68 Gtk.Button, Gtk.Stack], None], stack: Gtk.Stack) -> Gtk.Button:
69 button = Gtk.Button(label=label)
70 button.connect('clicked', action, stack)
71 button.set_margin_top(10)
72 button.set_margin_start(10)
73 button.set_margin_end(10)
74 button.set_margin_bottom(10)
75 return button
76
77
78 def on_activate(app: Gtk.Application) -> None:
79 win = Gtk.ApplicationWindow(application=app)
80 win.set_title('SRec')
81 stack = Gtk.Stack()
82
83 nr_box = Gtk.Box()
84 nr_box.set_orientation(Gtk.Orientation.VERTICAL)
85 webcam = Gtk.CheckButton(label='Webcam', active=True)
86 nr_box.append(webcam)
87 nr_box.append(Gtk.CheckButton(label='Screen', group=webcam))
88 nr_box.append(make_button("Start Recording", on_start_recording, stack))
89 stack.add_named(nr_box, "not_recording")
90
91 r_box = Gtk.Box()
92 r_box.set_orientation(Gtk.Orientation.VERTICAL)
93 r_box.append(make_button("Stop Recording", on_stop_recording, stack))
94 stack.add_named(r_box, "recording")
95
96 win.set_child(stack)
97 win.present()
98
99
100 def main() -> None:
101 app = Gtk.Application(application_id='net.chkno.srec')
102 app.connect('activate', on_activate)
103 app.run(None)
104
105
106 if __name__ == '__main__':
107 main()