]>
git.scottworley.com Git - srec/blob - srec.py
7780e511e4b423afa0fe7adaaabfdf11ab88af18
1 # srec: A simple GUI for screen recording
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.
7 from dataclasses
import dataclass
8 from datetime
import datetime
11 from typing
import Callable
14 gi
.require_version("Gtk", "4.0")
15 from gi
.repository
import Gtk
# nopep8 pylint: disable=wrong-import-position
21 process
: subprocess
.Popen
[bytes]
24 recording
: Recording |
None = None
27 def make_filename() -> str:
28 directory
= os
.environ
.get(
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')
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', 'x11grab', '-i', ':0.0+0,0']
39 return ['-f', 'v4l2', '-i', '/dev/video0']
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(
51 process
=subprocess
.Popen(command
, stdin
=subprocess
.PIPE
))
52 stack
.set_visible_child_name("recording")
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
62 recording
.process
.wait()
64 stack
.set_visible_child_name("not_recording")
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)
78 def on_activate(app
: Gtk
.Application
) -> None:
79 win
= Gtk
.ApplicationWindow(application
=app
)
81 win
.set_icon_name('srec')
86 nr_box
.set_orientation(Gtk
.Orientation
.VERTICAL
)
87 screen
= Gtk
.CheckButton(label
='Screen')
89 nr_box
.append(Gtk
.CheckButton(label
='Webcam', active
=True, group
=screen
))
90 nr_box
.append(make_button("Start Recording", on_start_recording
, stack
))
91 stack
.add_named(nr_box
, "not_recording")
94 r_box
.set_orientation(Gtk
.Orientation
.VERTICAL
)
95 r_box
.append(make_button("Stop Recording", on_stop_recording
, stack
))
96 stack
.add_named(r_box
, "recording")
103 app
= Gtk
.Application(application_id
='net.chkno.srec')
104 app
.connect('activate', on_activate
)
108 if __name__
== '__main__':