stop-record-delete-recording-start-new.py
· 3.7 KiB · Python
Bruto
# Copyright (C) 2025 Onyx and Iris
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Special Thanks to OBS Discord user Penwywern for helping to improve this snippet.
"""
stop-record-delete-recording-start-new.py
This OBS Python script allows users to register a hotkey that, when pressed, will:
1. Stop the current recording (if active).
2. Delete the most recent recording file.
3. Start a new recording after a short delay.
Features:
- Registers a customizable hotkey in OBS under 'Hotkeys'.
- Ensures the hotkey only works when a recording is active.
- Handles OBS frontend events to safely delete the last recording after stopping.
- Provides logging for each major action (stop, delete, start).
Functions:
- script_description(): Returns a description of the script for OBS.
- script_load(settings): Registers the hotkey and loads its settings.
- callback(pressed): Hotkey callback to initiate the stop/delete/start sequence.
- on_event(event): Handles OBS events, deletes the last recording, and schedules the new recording.
- on_timer_tick(): Starts a new recording after a delay.
Requirements:
- OBS Studio with Python scripting support.
- Proper hotkey configuration in OBS settings.
License:
- GPL-3.0-or-later
Copyright (C) 2025 Onyx and Iris
"""
__version__ = "0.2.0"
from pathlib import Path
import obspython as obs
HOTKEY_ID = obs.OBS_INVALID_HOTKEY_ID
### SCRIPT SETUP ###
def script_description() -> str:
"""Registers a hotkey to delete the last recording and start a new one."""
return (
"This script registers a hotkey to delete the last recording and start a new one.\n"
"The hotkey can be set in the OBS settings under 'Hotkeys'.\n"
"Make sure to set the hotkey before using this script."
)
def script_load(settings):
"""Register a hotkey to trigger the script."""
HOTKEY_ID = obs.obs_hotkey_register_frontend(
"record_delete_start", "Delete Last Recording and Start New", callback
)
hotkey_save = obs.obs_data_get_array(settings, "record_delete_start")
obs.obs_hotkey_load(HOTKEY_ID, hotkey_save)
obs.obs_data_array_release(hotkey_save)
return HOTKEY_ID
### CALLBACKS ###
def callback(pressed):
"""Callback function for the hotkey."""
if pressed:
if not obs.obs_frontend_recording_active():
obs.script_log(obs.LOG_WARNING, "Recording is not active.")
return
obs.obs_frontend_add_event_callback(on_event)
obs.obs_frontend_recording_stop()
def on_event(event):
"""Event handler for the hotkey."""
if event == obs.OBS_FRONTEND_EVENT_RECORDING_STOPPED:
obs.obs_frontend_remove_event_callback(on_event)
obs.script_log(obs.LOG_INFO, "Recording stopped.")
resp = obs.obs_frontend_get_last_recording()
file_pn = Path(resp)
if file_pn.exists():
file_pn.unlink()
obs.script_log(obs.LOG_INFO, f"Deleted recording: {file_pn}")
obs.timer_add(on_timer_tick, 1000)
def on_timer_tick():
"""Timer callback to start a new recording."""
obs.remove_current_callback()
obs.obs_frontend_recording_start()
obs.script_log(obs.LOG_INFO, "Recording started.")
1 | # Copyright (C) 2025 Onyx and Iris |
2 | # |
3 | # This program is free software: you can redistribute it and/or modify |
4 | # it under the terms of the GNU General Public License as published by |
5 | # the Free Software Foundation, either version 3 of the License, or |
6 | # (at your option) any later version. |
7 | # |
8 | # This program is distributed in the hope that it will be useful, |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | # GNU General Public License for more details. |
12 | # |
13 | # You should have received a copy of the GNU General Public License |
14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
15 | # |
16 | # Special Thanks to OBS Discord user Penwywern for helping to improve this snippet. |
17 | |
18 | """ |
19 | stop-record-delete-recording-start-new.py |
20 | This OBS Python script allows users to register a hotkey that, when pressed, will: |
21 | 1. Stop the current recording (if active). |
22 | 2. Delete the most recent recording file. |
23 | 3. Start a new recording after a short delay. |
24 | Features: |
25 | - Registers a customizable hotkey in OBS under 'Hotkeys'. |
26 | - Ensures the hotkey only works when a recording is active. |
27 | - Handles OBS frontend events to safely delete the last recording after stopping. |
28 | - Provides logging for each major action (stop, delete, start). |
29 | Functions: |
30 | - script_description(): Returns a description of the script for OBS. |
31 | - script_load(settings): Registers the hotkey and loads its settings. |
32 | - callback(pressed): Hotkey callback to initiate the stop/delete/start sequence. |
33 | - on_event(event): Handles OBS events, deletes the last recording, and schedules the new recording. |
34 | - on_timer_tick(): Starts a new recording after a delay. |
35 | Requirements: |
36 | - OBS Studio with Python scripting support. |
37 | - Proper hotkey configuration in OBS settings. |
38 | License: |
39 | - GPL-3.0-or-later |
40 | Copyright (C) 2025 Onyx and Iris |
41 | """ |
42 | |
43 | __version__ = "0.2.0" |
44 | |
45 | from pathlib import Path |
46 | |
47 | import obspython as obs |
48 | |
49 | HOTKEY_ID = obs.OBS_INVALID_HOTKEY_ID |
50 | |
51 | |
52 | ### SCRIPT SETUP ### |
53 | def script_description() -> str: |
54 | """Registers a hotkey to delete the last recording and start a new one.""" |
55 | return ( |
56 | "This script registers a hotkey to delete the last recording and start a new one.\n" |
57 | "The hotkey can be set in the OBS settings under 'Hotkeys'.\n" |
58 | "Make sure to set the hotkey before using this script." |
59 | ) |
60 | |
61 | |
62 | def script_load(settings): |
63 | """Register a hotkey to trigger the script.""" |
64 | HOTKEY_ID = obs.obs_hotkey_register_frontend( |
65 | "record_delete_start", "Delete Last Recording and Start New", callback |
66 | ) |
67 | hotkey_save = obs.obs_data_get_array(settings, "record_delete_start") |
68 | obs.obs_hotkey_load(HOTKEY_ID, hotkey_save) |
69 | obs.obs_data_array_release(hotkey_save) |
70 | |
71 | return HOTKEY_ID |
72 | |
73 | |
74 | ### CALLBACKS ### |
75 | def callback(pressed): |
76 | """Callback function for the hotkey.""" |
77 | if pressed: |
78 | if not obs.obs_frontend_recording_active(): |
79 | obs.script_log(obs.LOG_WARNING, "Recording is not active.") |
80 | return |
81 | |
82 | obs.obs_frontend_add_event_callback(on_event) |
83 | obs.obs_frontend_recording_stop() |
84 | |
85 | |
86 | def on_event(event): |
87 | """Event handler for the hotkey.""" |
88 | if event == obs.OBS_FRONTEND_EVENT_RECORDING_STOPPED: |
89 | obs.obs_frontend_remove_event_callback(on_event) |
90 | |
91 | obs.script_log(obs.LOG_INFO, "Recording stopped.") |
92 | resp = obs.obs_frontend_get_last_recording() |
93 | file_pn = Path(resp) |
94 | if file_pn.exists(): |
95 | file_pn.unlink() |
96 | obs.script_log(obs.LOG_INFO, f"Deleted recording: {file_pn}") |
97 | |
98 | obs.timer_add(on_timer_tick, 1000) |
99 | |
100 | |
101 | def on_timer_tick(): |
102 | """Timer callback to start a new recording.""" |
103 | obs.remove_current_callback() |
104 | |
105 | obs.obs_frontend_recording_start() |
106 | obs.script_log(obs.LOG_INFO, "Recording started.") |
107 |