]> git.scottworley.com Git - srec/blobdiff - srec.py
Don't hang if ffmpeg exits on its own
[srec] / srec.py
diff --git a/srec.py b/srec.py
index bacc1df5654fbffd2c8a8b4765abcc87e420f0b8..e9054838af18172c5e785162eab43c62dd7c1934 100644 (file)
--- a/srec.py
+++ b/srec.py
@@ -8,6 +8,7 @@ from dataclasses import dataclass
 from datetime import datetime
 import os
 import subprocess
+import sys
 from typing import Any, Callable
 
 import gi
@@ -20,9 +21,18 @@ from gi.repository import GLib  # nopep8 pylint: disable=wrong-import-position
 
 @dataclass
 class Recording:
-    filename: str
     process: subprocess.Popen[bytes]
 
+    def stop(self) -> None:
+        stdin = self.process.stdin
+        assert stdin is not None
+        try:
+            stdin.write(b'q')
+            stdin.flush()
+        except BrokenPipeError:
+            print("Stream already stopped?", file=sys.stderr)
+        self.process.wait()
+
 
 recording: Recording | None = None
 
@@ -77,7 +87,6 @@ def on_start_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
                ['-f', 'pulse', '-ac', '2', '-i', 'default', filename])
     # pylint: disable=consider-using-with
     recording = Recording(
-        filename=filename,
         process=subprocess.Popen(command, stdin=subprocess.PIPE))
     stack.set_visible_child_name("recording")
 
@@ -85,11 +94,7 @@ def on_start_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
 def on_stop_recording(_: Gtk.Button, stack: Gtk.Stack) -> None:
     global recording  # pylint: disable=global-statement
     assert recording is not None
-    stdin = recording.process.stdin
-    assert stdin is not None
-    stdin.write(b'q')
-    stdin.flush()
-    recording.process.wait()
+    recording.stop()
     recording = None
     stack.set_visible_child_name("not_recording")
 
@@ -106,7 +111,7 @@ def make_button(label: str, action: Callable[[
 
 
 def make_share_control() -> Gtk.CheckButton:
-    can_share = os.path.exists('/sys/module/v4l2looback')
+    can_share = os.path.exists('/sys/module/v4l2loopback')
     control = Gtk.CheckButton(
         label='Share Webcam', sensitive=can_share, active=can_share)
     control.set_margin_start(20)