]> git.scottworley.com Git - srec/blob - srec.py
Timestamped output files
[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 screen_size = '1366x768' # TODO
40 filename = make_filename()
41 command = [
42 'ffmpeg',
43 '-video_size', screen_size,
44 '-framerate', '25',
45 '-f', 'x11grab', '-i', ':0.0+0,0',
46 '-f', 'pulse', '-ac', '2', '-i', 'default',
47 filename] # nopep8
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 start_recording = make_button("Start Recording", on_start_recording, stack)
84 stack.add_named(start_recording, "not_recording")
85
86 box = Gtk.Box()
87 box.set_orientation(Gtk.Orientation.VERTICAL)
88 stop_recording = make_button("Stop Recording", on_stop_recording, stack)
89 box.append(stop_recording)
90 stack.add_named(box, "recording")
91
92 win.set_child(stack)
93 win.present()
94
95
96 def main() -> None:
97 app = Gtk.Application(application_id='net.chkno.srec')
98 app.connect('activate', on_activate)
99 app.run(None)
100
101
102 if __name__ == '__main__':
103 main()